Hi, i am using this code to show form2 in my program,

Form2 form = new Form2();
                form.Show();

however when i want to use the on load event to change a labels name nothing happens, i suspect because it isnt loading just showing, does anyone have a solution thanks.

Recommended Answers

All 23 Replies

The load event has nothing to do with how the form is show, or even if it is shown at all. when the form is created as an object, done in your first line of code, the form "loads" and its load event is called then. It of course cannot be shown before it is loaded.

there must be some other problem, because you can easily change the name property of a label in a forms load event.

if you could post your code from "form 2" where the load event takes place. we could help further.

hi, its just something really simple

private void Form2_Load_1(object sender, EventArgs e)
        {
             textBox1.Text = Area.Room1;
        }

I'm not sure what this Area.Room1 object is, but that is most likey the problem. try changing it to a hard coded string and you will see that it works fine.

The problem most likely is that this object is not initialized with a value at the time the onload event is called. the onload event is called immediately after the constructor, as soon as it has finished creating all the controls and objects inside the constructor. so unless this area.room1 object is created inside the constructor, or static, then its going to fail.

the area.room1 is a custom static class that i'm using to store a string in and i just need a label to say what the string say when i show the form.

Hi i am using this code to show a form

Form2 form2 = new Form2();
form2.Show();

and am trying to use this to change a label

label1.Text = variable;

and it dosen't work... any ideas??

If I do this it works fine with me:
label1.Text = "12345";
Perhaps your variable is empty?

tat was just a mock, the actual code is this

private void Form2_Load(object sender, EventArgs e)
        {
            label7.Text = Area.Room1;
        }

the variable is definetly not empty, i tested it by having a button and a textbox, i think its something to do with the load order because its in form2.

What is actually happening? Is the label not updated or do you get an exception or something else?
If I were you, I should set a breakpoint and see what happens in the debugger.

i think that the label is not updating because it doesnt even go blank just stays at label7, whats a breakpoint?

Please go throw this to know more about breakpoints.

i inserted a breakpoint in the code and nothings happening at all

>>whats a breakpoint?

Well, that's rather basic to know what a breakpoint is.
I'll try to explain in short.
The left side of a code window is left blank. Click in it and you see a sort of brown ball appear. To remove it click again. This is called a breakpoint, you can set it anywhere you want. Set one in your Load event for instance. Now when you start your program in debug mode by clicking the green triangle or F5.
The flow of your program will stop at a breakpoint and in the debugger you can now watch the value of all your variables and step through your program line by line and see how the variables change. Very handy, believe me!

i inserted the breakpoint at the code and nothing happens in debugmode, ill attatch my project so far, you enter areas in the textboxes before clicking next and the area should display in the label in form2.

Downloaded your project, will let you know more.
What I can tell you right now is that the load event of form2 never gets executed else I should fall into the debugger because I have a breakpoint set there(one use of a breakpoint! this one tells me I never come there!)

The problem is this:

private void Form2_Load(object sender, EventArgs e)
        {
            label7.Text = Area.Room1; //this never gets executed
        }

        private void Form2_Load_1(object sender, EventArgs e)
        {
        // this will be executed!!!!!
        }

Also watching your code : try to give meaningfull names to your controls. Name your Next button NextBtn for instance. Now it is named button2 in your code and you have another button named button2 somewhere. This can become very confusing, believe me!

ok, it seems to load because the label text goes blank even tho the variable is being stored because i tested it with button and textbox, any clues?

Open the design of your Form2.
Select the lightning bolt button in the properties windows.
Select the Load event.
Instead of Form2_Load_1 rename it Form2_Load
Delete the Form2_Load_1 method in your code.
That way one unneeded Load event handler is removed!
Now when I set set a breakpoint in the remaining load event I see in the debugger that Area.Room1 is equal to null, so your label shows no text! You somehow have forgotten to init or set this variable.

The forms load event fires when the form becomes visible and the window handle is created. Please post your project so we can review. As Diamonddrake indicated you have not given us enough code to show the true problem

Check what method is being called for onload event. private void Form2_Load_1 makes me think, there might be a different method associated with the onload event.

You are quite right lighthead!
I did not notice until now but check this parallel thread http://www.daniweb.com/forums/thread207020.html
@Scott: In this thread he posted his code.
Maybe these threads could be merged?

merged the two threads

commented: Thanks. +7

A small incantation and AD is always there when you need him.:)

thanks for all your replies, the problem was very simple, on form1 the next button checked for empty boxes then opened form2 then stored the variables so when form2 opened it had already loaded the label at which point the variable was 0; so i just had the button store the variable first then open it... THANK YOU!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.