Form and its Load-event

K

Kimmo Laine

Hi!

Is there a way to generate Load-event for form without showing it?

My problem is that if i try to set control states before Load is raised,
control states may or may not work. Here is some code:

Lets say that i have 2 form - form1 and form2. In form1 i have a button.
When this button is pressed i will display the other form:

void button1_Click(object sender, EventArgs e) {

Form2 f = new Form2();
f.DoSomething();
f.Show();

}

In form2 i have 2 controls, button and a checkbox. If i do like this in
DoSomething-method...


public void DoSomething() {

button1.Visible = true;
checkBox1.Visible = button1.Visible;

}

....checkbox1 will be hidden! Is there a way to solve this?


tthx
-Kimmo
 
I

Ido Samuelson

You can do

form.Show();
form.Hide();

before the call to the DoSomething method. However it is better if you
explain more what you are doing, it looks like you have a design issue.

Best,

Ido Samuelson
 
K

Kimmo Laine

Hi Ido!

Well i have several (20+) views with a custom base class. The base class
contains all kind of localiztion, security database and navigation related
virtual methods. The code which is displaying views looks something like
this

UIViewBase v = GetView(...);
// . . .
v.OnBeforeShow(...);
v.Show();
v.OnAfterShow(...);
// . . .

My problem is that if i place code in the virtual OnBeforeShow-method which
is setting control states (like the Visibility e.g.) and then im relying it,
the code doesn´t work when the view is displayed for the first time. Of
course i can code something that doesn´t use control states, lets say in
if-statements e.g., but i would rather not.

-kimmo
 
I

Ido Samuelson

The best approach (and the right way) will be to separate between your logic
and the views. You should then use binding to change how the view looks and
display data.
the second approach you can take (to make it work asap) is move the code
that is not working (aka the code that changes the user controls visibility
or something else) and move it to the following :

void Form1_VisibleChanged(object sender, EventArgs e)
{
if (Visible)
{
myControl.Visible = true;
}
}
 
B

Ben Voigt [C++ MVP]

Kimmo Laine said:
Hi!

Is there a way to generate Load-event for form without showing it?

My problem is that if i try to set control states before Load is raised,
control states may or may not work. Here is some code:

Lets say that i have 2 form - form1 and form2. In form1 i have a button.
When this button is pressed i will display the other form:

void button1_Click(object sender, EventArgs e) {

Form2 f = new Form2();
f.DoSomething();
f.Show();

}

In form2 i have 2 controls, button and a checkbox. If i do like this in
DoSomething-method...


public void DoSomething() {

button1.Visible = true;
checkBox1.Visible = button1.Visible;

}

...checkbox1 will be hidden! Is there a way to solve this?

This has nothing to do with "control states" and nothing to do with the Load
event.

The simple truth is that setting Visible sets an internal flag, but reading
Visible doesn't use the flag. It also checks whether the parent is visible.
MSDN says Visible is "true if the control and all its parent controls are
displayed". I can't find any public or protected member that lets you read
the internal flag. Most likely you'll have to maintain your own list of
which controls are visible, trigger from the VisibleChanged event so that
checkBox1's Visible stays in sync with button1, or base your decision on a
property other than Visible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top