How to update the text shown on a status strip's label - c#

I created a status strip on my main form and a label within it. And I am opening another form within this (MdiContainer is true). For opening I am passing the object of main form in the constructor i.e. this. There I am updating the text of the label but its not getting updated.
I tried invalidate function also.

I was having this problem too. The fix is to add a line of code that fires events (see the second line below)
MainForm.mainStatusLabel.Text = "Importing data file" //see next line
Application.DoEvents()
I don't completely understand why this works, but my guess is that VB does not handle Application-wide events in the middle of a method unless you tell it to. When you change the text property, this is an event. So VB will (Im guessing) wait until the end of the method before handling the event--unless you tell it specifically to handle all events with Application.DoEvents()

Instead of passing the instance of the MainForm to the ChildForm, define a custom event in the ChildForm, raise the event with the message that you want to show (as argument), then subscribe the event in MainForm, and in the eventhandler update the control text with the message. You may also have to check Control.InvokeRequired.

I also had the problem, in my case because the text was too long to be displayed (Contained an exception message.) I set the SizingGrip property to 'False', so my text was shown (but trunkated).

Related

VisibleChanged don't raise when not visible

I'm working on c# WinForm.
I have an custom UserControl : MyControl : UserControl, INotifyPropertyChanged. I attached a method on event on event VisibleChanged : this.VisibleChanged += new System.EventHandler(this.MyControl_VisibleChanged);
My application have some pages, each page is a control like MyControl. Top of MainWindows contains Button, used to switch tab.
My problem is that my function MyControl_VisibleChanged is called only when Visible is changing to true. I added a test in a tab to check MyControl.Visible, when I select the other tab, MyControl.Visible is false but no event is raised.
I've try to define a new property Visible for this control but value is never set, only the base value is modify.
Can you help me to find a solution ?
This is a quirk in the way Visible works, explained here. His solution was to use properties that he has complete control over, but you could instead have a method allowing the tab switches to tell their children to raise their VisibleChanged event that extra time.
The first two answers to this question may also be useful.

Why does GotFocus event keep deleting itself from Designer?

I wanted to add a GotFocus event to a Windows Forms Textbox, so I used the method described in this question; it works, but after I run my application a couple of times the piece of code deletes itself and I don't know why.
This is the code that keeps deleting itself:
txtID.GotFocus += txtID_GotFocus;
It disappears because you don't use conventions that are used by WinForms designer when you add event handlers.
It doesn't matter whether you use GotFocus or Enter event. If you (in your Designer.cs) manually add event handler this way:
txtID.Enter += txtID_Enter;
then it would always disappear from designer next time you move control on designer surface.
You must add event handlers this way:
txtID.GotFocus += new System.EventHandler(txtID_Focus);
txtID.Enter += new System.EventHandler(txtID_Enter);
and nothing would disappear because it's the way designer expects code to be.
Surely it's another evidence about why you should not touch designer generated code and should pay attention to this warning: do not modify the contents of this method with the code editor.
As a workaround use Enter event instead (which is recommended). Also you can assign the handler in your Load event of form.
EDIT
The reason is correctly mentioned by nikita, it's because you didn't use designer conventions. For more information see his answer.

On leaving a control, how can I give that control focus again?

I've got TextBoxes in a C# form. The user enters data, and then when they leave the control (almost always by hitting Tab), I check the data to make sure it's valid. If it is invalid, I want to highlight their text so they can immediately fix it rather than having to click it.
Right now, on Control.Leave, I validate their entry. This works just fine. However, since they hit Tab, right after they dismiss the error message, it goes on to the next object, even though I've got ((TextBox)sender).Focus();
How can I have the above line fire after the form Tabs to the next control.
You may want to look into Control.CausesValidation property
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx
You can validate the control prior to the user leaving focus rather than waiting on Focus moving itself.
And here's MSDN documentation for Control.Validating event, does a good job at laying out the sequence of events when gaining / losing focus of a Control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx
Notice how Control.Validating and Control.Validated are launched prior to Control.LostFocus. You can perform your validation step prior to allowing the user to lose focus of your Textbox.
There's also a pretty good previous answer on stackoverflow.com which outlines how to do this: C# Validating input for textbox on winforms
If you handle the Control.Validating event, setting e.Cancel to true will stop the change of focus from occurring.
Note that this method will also stop buttons from working, so you may need to set Control.CausesValidation to false on certain buttons.
You will also need the following snippet on the main form to allow the close button to work:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
Try using the LostFocus event on the TextBox to Focus it again

C# - are there any events fired right after loading a form?

I want to give the user the option to use a tutorial, the first time he uses the program. I tried adding it in the Form.Load event, but the forms shows up after the Messageboxes have popped up.
That's why I would like to know, are there any events fired right after loading a form?
If not, is there a way to perform actions right after loading?
You should try the shown event, which fires after the form is shown for the first time.
Load happens before the form is shown.
You could try using the Shown event but that might be a bit early too based on what you are doing but it does occur after the Load.
If you have any controls on the page you could trigger it off the controls GotFocus event. Just make sure to put in checks to only do it once if using the GotFocus method.
MSDN Form.Shown
MSDN Control.GotFocus
MSDN Reference to order of events
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
The Shown event should do this for you.

How to handle complete change of the Textbox in Windows Forms?

What event I should handle to react to the completed change of the TextBox (i.e. when the user is
finished editing the content of the TextBox)?
There are several methods that you can use: Leave event, or a manual "typing stopped" event.
The Leave method is the most straight forward way of doing it, although as the event name suggests, it only happens when they TextBox looses focus, not when the user stops typing.
The TypingStopped event is something you would need to create yourself, but the basic idea of it is a short duration timer (say 500ms, but you would need to test it), which you restart on every KeyDown event of the TextBox. The timer would fire its own event and disable itself if it ever hits the end of it's timeout.
Edit: Updated to Leave event as per Hans' recommendation.
The Leave event is generally a good one for processing user input (for validation for example) as they move on to another part of the form. Just make sure that the event fires if they go from the textbox to any other UI element on your form - you may need to force a focus on the new element.

Categories