Picturebox tick event behaving inconsistently - c#

So recently i have been trying to get into C# programming after spending alot of time in C.
Naturally i jumped right into learning a few new things, in this case i wanted to try some forms, classes and events - simple right?
Well apparently not, i am rather dumbfounded, i have been running the program, analyzing step by step, the tick event triggers as its supposed to, but the picture in the picturebox is not updated.
And here is the kicker, i made a button that runs almost identical code, and that works, what gives? i can barely get my head around it as it is.
http://pastebin.com/psYzQSLE - here is the code i am running currently.
And here is the specific segment of code i cant get to behave.
private void timer1_Tick(object sender, EventArgs e)
{
if(swapper)
{
swapper = false;
pictureBox1.Image = ima1;
pictureBox1.Refresh();
}
else
{
swapper = true;
pictureBox1.Image = ima2;
pictureBox1.Refresh();
}
}

Your code looks fine, and is working for me. I suspect the Form1_Load method is not hooked up to the form's Loaded event; you can check that in the designer.

Okay, i figured it out!
Being unfamilar with the design document, the timer was added to the tick in there and i also added it myself in my code, the result being a timer that triggers twice immediately, and thus i saw no result.
Problem is solved now, thanks for the warm welcome :)

Related

Visual studio crashes when opening forms inheriting from a specific form in the project

I know this can be a duplicate, but I already searched the web and tried literally ALL and EVERY solution I found, but to no avail...
I have a custom form in my project called ParentForm it is the parent of most of my other forms in the project, it has some standard elements on it, like panels and timers.
I have no problem with showing this form, editing its design or code.
but whenever I open a form that is inheriting from it, it opens for a moment (not responding thou) and then visual studio crashes immediately, I think it was only the designer first, but now, even when I open the form's code, the same happens!
I tweaked some of the parent form's code and now it stopped crashing.. but now, whenever I open a child form of it, I get this:
ContaierPanel and panel2 are two panels I have on the parent form, and as I see no problem with them, they are used as containers in the child forms, their access modifiers are public too.
The problems seems to be related to the parent form itself, but I already removed every single line of code in it, that didn't help at all, now I'm stuck with the error you see in the image above.
I also uploaded the complete project to Github in case someone wanted to see the code, here's the link.
I'm using the Material Skin in the project.
Your help is so appreciated, I have no much time before the deadline and there is still much to do... Thanks
Edit: After missing around the code for a few extra hours, I finally discovered the code part that is causing the problem:
As I said before I have a timer on the parent form called tmrCheckConnection, here's the code of the timer:
private void tmrCheckConnection_Tick(object sender, EventArgs e)
{
if (!Program.dbConnection.IsConnect())
{
if (!controlsAreDisabled)
{
disabledControls.Clear();
foreach (Control c in this.Controls)
{
if (c.Enabled)
{
disabledControls.Add(c);
c.Enabled = false;
}
}
controlsAreDisabled = true;
}
lblNoDBConnection.Enabled = true;
lblNoDBConnection.Visible = true;
this.Controls.SetChildIndex(lblNoDBConnection, 0);
} else {
if (controlsAreDisabled)
{
foreach (Control c in disabledControls)
c.Enabled = true;
lblNoDBConnection.Visible = false;
}
}
}
Obviously, the error happens here >> Program.dbConnection.IsConnect() << because that is not an instantiated class yet, and the IsConnect() functions uses some of its un-instatiated members.
So the solution is simply to prevent the timer code from running at design time, so I tried putting this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
return;
at the beginning of the timer's code, it didn't change anything.
Next, I tried to remove the event handler from the timer, and reassign it in the Form_Load like this:
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Runtime)
tmrCheckConnection.Tick += tmrCheckConnection_Tick;
And again, it didn't help.
Any ideas?
Oh, and I removed the github repository.

SharpGL - Entering Active Rendering Loop

I've successfully setup my frame with the OpenGLControl and delegated my render method to the OpenGLDraw event of it. However, I'd like to do game programming so I want to enter into an active rendering loop where I'd update the game state and render, both from another thread but can't figure it to do how.
Not positive, but if it works the same as most other systems you will need to make sure that all the OpenGL calls come from the thread, past that you can put your logic and everything else is however mean and whichever threads you want. Hope that helps!
Got it working by not calling Application.Run and instead using P/Invoke to create a C++ styled MessagePump.
Have you tried using the Winforms Application.Idle event?
Example (assuming you have a standard static class Program):
In Main - Application.Idle += Tick
static void Tick(object sender, EventArgs e)
{
//Insert rendering/updating code here
}

Buttons Enabled Property not Working Properly

I am creating an Windows Application. I have two buttons.
I have written the following code snippet.
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.BringToFront();
obj.Focus();
}
The above coding does not generate any error.
All the statements are working properly but the following statement is not working properly:
obj.btnRtn.Enabled = true;
is not executed.
The frmrb forms is bring to front and it is focussed but btnRtn is not Enabled that is the statement obj.btnRtn.Enabled = true; is not working.
By default I have set the property of btnRtn Enabled to false.
And Please note the Modifier property of btnRtn button is set to PUBLIC.
Now how should I change the coding so that I can get this statement executed.
obj.btnRtn.Enabled = true;
Can anybody help me out?
Thanks in Advance!!
SOLUTION
You should never disable a button, or change it´s visibility before it is initialized, otherwise you won't be able to enable it again, or turn it visible again.
Instead, you should disable it on it's own "Initialized" event, and then it will work properly!
I had the same problem.
Is the button placed inside a panel or any container. If yes then please check the enabled status of the container also.
You not mention that where you obj(which is the instance of frmRb) show. because it is very important point.
from your coding it seem that frmRb is already visible. so u never called the
obj.Show() ;
instead you call the
obj.BringToFront();
so the problem is that you never show the frmRb object. which is you create u in 1st line. each time u write the line
frmRb obj = new frmrb();
new instance of frmrb is created. So u must again show it, with the line obj.Show() ;
Now u rewrite ur code as ::
frmRb obj = new frmrb();
private void btnPd_Click(object sender, EventArgs e)
{
btnCancel.Enabled = true;
obj.btnRtn.Enabled = true;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
obj.Show();
obj.BringToFront();
obj.Focus();
}
I hope it is helpful for u and solve ur problem.
I've been using VB rather than C#, but the languages are very similiar. in VB, you have to add "handles SomeButton.Click" to make it handle the Click events.
According to google, the equivalent in C# is to go into the Designer.cs file, find where the controls are, and manually change the click event hookup to point to your new event handler.
As mentioned by the previous poster, use a breakpoint (F9) and the debugger to see if that method is ever called when you execute the event. If it is not called, then the problem is probably not with the enabled property, but the wiring of the method so that it is invoked when the event occurs.
Here's a reference:
http://www.tek-tips.com/viewthread.cfm?qid=1442702&page=5
I strongly suspect that either the click handler isn't being called or you're not looking at the form you think you are.
Where are you setting up the click handler for btnPd_Click? Check that's still wired up correctly.
Then put a breakpoint on the first line of the handler and run it in the debugger - if you don't hit the breakpoint when you click the button, that's the problem.
EDIT: Okay, next steps:
Check that you're looking at the right button. Change its text as well as enabling it.
Check that its container is enabled, as suggested by phoenix. Not just its direct parent, but all the way up.
Check what else your UI thread is doing afterwards - are you blocking it for some reason? Does the rest of the UI work at that point?
I would try just switching the sequence of statements to:
private void btnPd_Click(object sender, EventArgs e)
{ obj.btnRtn.Enabled = true;
btnCancel.Enabled = true; }
and see if that helps you debug
Your code really should produce an error... C# is case sensitive, meaning frmRb is not the same as frmrb. Anyway, I copied it, created 2 forms and 3 buttons, and set up the handler, and it worked fine.
private void InitializeComponent()
{
this.btnPd = new System.Windows.Forms.Button();
this.btnPd.Location = new System.Drawing.Point(90, 116);
this.btnPd.Name = "btnPd";
this.btnPd.Size = new System.Drawing.Size(75, 23);
this.btnPd.TabIndex = 1;
this.btnPd.Text = "button1";
this.btnPd.UseVisualStyleBackColor = true;
this.btnPd.Click += new System.EventHandler(this.btnPd_Click);
}
public System.Windows.Forms.Button btnRtn;
Are you sure you were handling btnPd? Perhaps you may have locked your enabling code inside a disabled button? Hopefully this small working sample helps you find the problem. As for the rest of the code, All I changed was the frmRb to frmrb so they match.
If the form that your are trying to show on the screen is properly shown, may be you can try this:
Create a public method in the form that will set the button property enabled = true;
After creating the form, and showing, you can call that public method;
frmRb obj = new frmrb();
obj.EnableButton
It's been a while since anyone commented or answered I thought I would pose this answer/comment as it may be be helpful to others who stumble on it.
I recently had some CheckBoxes and NumericUpDowns not changing enabled state but it was simply due to the fact they were in a GroupBox that had not been enabled. A forehead slapping moment for me, but took me 20 min to figure out why those controls weren't responding!
Just a hunch. May be the 'Locked' property of button is 'true'
Pl check btnRtn situation if btnRtn is in panel or open ,if it in the panel pl check the panel enabled property and your coding is ok

What is the purpose of a Paint() method vs the _Paint() event in a Win Form?

I am working with a WinForm application that was designed by the previous, now unreachable, develeper. In this app farms are embedded in TabControls through some custom code. My question is, Can anyone help to try and explain why there is a custom _Paint() function in each form that is called from the Load event for that form.
This Paint() method is not actually tied to the form outside of the previously stated daisy chaining. What purpose could this serve? In the code below you will notice That I created a Paint() event and moved part of the code there and everything still seems "Peachy."
Can anyone help me to understand this? Is it simply because of the Public declaration on the custom one?
private void frmWWCModuleHost_Load(object sender, EventArgs e)
{
FormPaint();
}
public void FormPaint()
{
WinFormCustomHandling.ShowFormInContainerControl(tpgCaseNotes, new FrmCaseNotes());
WinFormCustomHandling.ShowFormInContainerControl(tpgMCP, _frmWWCMCPHost);
WinFormCustomHandling.ShowFormInContainerControl(tpgMember, _frmWWCMemberHost);
WinFormCustomHandling.ShowFormInContainerControl(tpgEnrollment, _frmWWCEnrollmentHost);
WinFormCustomHandling.ShowFormInContainerControl(tpgWWCSearch,_frmWWCSearch);
WinFormCustomHandling.ShowFormInContainerControl(tpgAudit, FrmAudit);
// Call each top-Level (visible) tabpage's form FormPaint()
_frmWWCMCPHost.FormPaint();
}
private void FrmModuleHost_Paint(object sender, PaintEventArgs e)
{
new psTabRenderer(tclWWCModuleHost, Color.LightSteelBlue, Color.Tomato, Color.Black, Color.Black);
}
The code looks weird, for sure, but I think you are reading more into the name of the "FormPaint" method than you should. To me, it appears to be just an "initialization" routine, having essentially nothing to do with the Paint event (other than the name).
Also, it appears that any code inside FormPaint is called one time per form, versus any code inside the Paint event handler being called...a lot.
I'm not sure you posted enough code for it to be understandable. But in general, if you feel the code is unnatural, go ahead and refactor it... one step at the time.

Forced Redraw in Silverlight 3 application

I have a function in my silverlight app, that takes one or two seconds to finish. While executing this function, I want to show my "just loading" UC to the user:
private void ComboBoxGranularity_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
WaitScreenObject.Visibility = Visibility.Visible;
OperationThatTakesALittleTime();
WaitScreenObject.Visibility = Visibility.Collapsed;
}
Problem is, that Silverlight doesn't redraw while the function is executed, so my WaitScreen doesn't show up. I tried the trick from this question:
this.Visibility = Visibility.Visible;
but it didn't work. I wanted to avoid the Backgroundworker-Overhead, so is there any possibilty to make the WaitScreenObject visible?
Thanks in advance,
Frank
Your method runs on the UIThread since you mentioned that you are not using a background thread. The thread only redraws the screen when it is not busy with something else, since the queue is filled with the other instructions, and those are considered more important than a redraw.
I tried what Andrew suggested, but I could not find the InvalidateVisual() method on the UIElement. Maybe I was just being daft.
The reason why the linked example did not work for you, is because the other question just dealt with an element not being invalidated because it did not have focus. However, the UIThread was available for a refresh at that time.
I also tested the dispatcher.BeginInvoke() on a delegate, and it did not work either. I am afraid that from my point of view, you might just have to use a separate thread.
I could be wrong because I simulated my "work" by making the thread sleep instead, however, I cannot see what the difference will be.
Hope this helps.
Have you tried invalidating the visual for the root object to try convince it to redraw? (Havn't checked if it would work, just comes to mind as a hack)
Edit : InvaldiateVisual is the method in WPF on the UIelement, in Silverlight you use a diff call, such as InvalidateArrange
myCanvasRoot.InvalidateArrange();
Bit hacky but might convince it to perform the update of the screen.

Categories

Resources