How to check from the beginning in Windows Forms App - c#

_Changed events check it if it is changed. How can I check it from the beginning.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
if (dateTimePicker1.Value.Month == 2)
{
pictureBox1.Visible = true;
}
else
{
pictureBox1.Visible = false;
}
}

You can call the event handler from the constructor, or sometimes better, the Load of the form:
private void MyForm_Load(object sender, EventArgs e)
{
dateTimePicker1_ValueChanged(dateTimePicker1, EventArgs.Empty);
}
There is also the Shown event called after the window first appears on the desktop.
Thus the control will be initialized as expected.
Here is the chain of calls:
Constructor : prefered place to instantiate objects and initialize UI instances.
Load : prefered place to create and initialize non designer UI objects or complete them like populating combobox or dataset.
Activated : prefered place to do things each time the form is activated and get focus.
Shown : prefered place to do things after the form is showned like open another form or show a info box.
The difference between using constructor, load or shown is mainly to have a clean code design and a smooth UI behavior.
There is no real difference between putting code in the constructor or in the load, else the reference and window handle are created.
Order of Events in Windows Forms (MS Doc)

Related

C# - Method is not being called upon .Load or .Shown [duplicate]

I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:
AdministrationView wel = new AdministrationView();
public ProgramViwer()
{
InitializeComponent();
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
Please note I added the load event based on what I read in this article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Load from the dropdown.
A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!
You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:
public class ProgramViwer : Form{
public ProgramViwer()
{
InitializeComponent();
Load += new EventHandler(ProgramViwer_Load);
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
}

Shown handler in Form's base class in the way of designer

I have a base class that inherits from Form, that registers a event handler on Shown:
class BaseClass : Form {
public BaseClass() : base() {
Shown += new EventHandler(BaseClass_Shown);
}
void BaseClass_Shown(object sender, EventArgs e) {
Close();
MessageBox.Show("This cannot be opened.");
}
}
Now, when I subclass this Form and open it in the designer, I actually get that messsage and it closes the form in the designer making is impossible for me to visually edit it.
Is there perhaps some boolean I can use to prevent the Close() and MessageBox to happen?
(Little background: the Close is not always called, but depends on some runtime settings and data)
Some events are fired in the designer as well, it is what gives the Winforms designer its wysiwyg ability. Notably Paint, Shown is also fired, etcetera. The DesignMode property is provided to allow you to tell whether your event handler is running at design-time. Fix:
void BaseClass_Shown(object sender, EventArgs e) {
if (!this.DesignMode) {
Close();
MessageBox.Show("This cannot be opened.");
}
}
Do note the flaw in your approach, this event also fires for a derived form. You might be helping too much.

"Cross-thread operation not valid" if ComboBox.DropDownStyle == Simple

I have a Forms that is shown in a method called through Invoke because that method is called from a different thread. In the form I need to open I have a UserControl with a ComboBox in it. If the ComboBox.DropDownStyle is Simple the form.Show explodes throwing
InvalidOperationException: Cross-thread operation not valid: Control
'comboBox1' accessed from a thread other than the thread it was
created on.
If I set ComoBox.DropDownStyle in the default value (DropDown) I have no problem.
I now this is kind of hard to understand (even believe) so here there is a simplified example to reproduce it:
Create a new winforms project.
Create two forms and a user control.
In the user control create a ComboBox.
In the Form2 put an instance of the user control.
In the Form1 code put this:
private Form form;
private delegate void ShowDelegate();
private ShowDelegate showDelegate;
private void Form1_Load(object sender, EventArgs e)
{
showDelegate = Show;
new Thread(Run).Start();
}
private void Run()
{
form = new Form2();
Invoke(showDelegate);
}
private void Show()
{
form.Show();
}
Remember to set the event to Form1_Load.
Run it and see it working.
Change the ComboBox.DropDownStyle to Simple and see it not working!
Any help with this issue please?
By simply moving
form = new Form2();
To your Show() method, it will work then.
The Form gets initialized on the thread you start, it works then. But you might want to check the behavior in the long term ...

How to add a form load event (currently not working)

I have a Windows Forms form where I am trying to show a user control when the form loads. Unfortunately, it is not showing anything. What am I doing wrong? Please see the code below:
AdministrationView wel = new AdministrationView();
public ProgramViwer()
{
InitializeComponent();
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
Please note I added the load event based on what I read in this article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
Three ways you can do this - from the form designer, select the form, and where you normally see the list of properties, just above it there should be a little lightning symbol - this shows you all the events of the form. Find the form load event in the list, and you should be able to pick ProgramViwer_Load from the dropdown.
A second way to do it is programmatically - somewhere (constructor maybe) you'd need to add it, something like: ProgramViwer.Load += new EventHandler(ProgramViwer_Load);
A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!
You got half of the answer! Now that you created the event handler, you need to hook it to the form so that it actually gets called when the form is loading. You can achieve that by doing the following:
public class ProgramViwer : Form{
public ProgramViwer()
{
InitializeComponent();
Load += new EventHandler(ProgramViwer_Load);
}
private void ProgramViwer_Load(object sender, System.EventArgs e)
{
formPanel.Controls.Clear();
formPanel.Controls.Add(wel);
}
}

Execute 'Form_Shown' event handler more than once?

I am currently developing a Windows app with several forms. I use Form_Shown in one of those forms to execute some code to initialize (refresh) the form before showing it.
In Form.Shown Event on MSDN, it states that the event is raised only the first time the form is shown. However, I want to be able to execute code to initialize my form every time that I call Form.Show() in some of the forms. Here's an example.
From a form named Game. Contains an event handler Game_Shown and a button that when clicked shows a form named Menu:
private void btnMenu_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formMenu.Show();
}
private void Game_Shown(object sender, EventArgs e)
{
Code here...
this.Refresh();
}
From the form named Menu. Contains a button that when clicked shows the form named Game:
private void lblGame_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formGame.Show();
}
It is behaving by design.
From the docs:
The Shown event occurs whenever the form is first shown.
Also, you should not handle the Shown event in your class, rather you should override OnShown.
To achieve what you want, you might try overriding the OnVisibleChanged method. Inside the method, if the form is visible, then execute your code.
Like the Shown event, you should not handle it in your form class, instead override the appropriate method:
From the docs:
The OnVisibleChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
What you want requires some detailed knowledge about which event happens when in the WinForm lifecycle. That may be documented somewhere, I don't know.
This is how I would find out:
create a small test project with 2 forms (Main and helper)
add the show and hide buttons and make sure it works.
Add Debug.Print("EventName") to all the candidate events of the helper form.
Look at the log in the output window and pick your event.
Candidate events would be FormClosing, FormClosed, (De)Activated, Enter, Leave, Load, ... go through the list.
When you find the right one, please post it here in an answer.

Categories