Click here to Skip to main content
12,896,108 members (50,395 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
i am makinng a windows form project and facing difficulty in passing the dnamically generated control value to the other form's normal control value.
my code is like
 int c = 0;
        int p = 0;
        private void button1_Click(object sender, EventArgs e)
            panel1.VerticalScroll.Value = VerticalScroll.Minimum;
           // panel1.HorizontalScroll.Value = HorizontalScroll.Minimum;
            ComboBox txtRun3 = new ComboBox();
            txtRun3.Name = "txtDynamic" + c++ ;
            txtRun3.Location = new Point(30, 18 + (30 * c))
        private void button2_Click(object sender, EventArgs e)
            Form4 f4 = new Form4();
            ComboBox cb1 = sender as ComboBox;
            Button bs = cb1.Tag as Button;
            f4.comboBox1.Text = bs.Text;

i am getting error as
"Object reference not set to an instance of an object."
Posted 11-Dec-12 2:24am
Updated 11-Dec-12 2:35am
Rai Pawan 11-Dec-12 8:29am
Hi dear, from your code it seems that the button2_Click is a button click event then you should not be getting cb1 in the sender parameter. Please check and clarify.
shaikh-adil 11-Dec-12 8:32am
what can i do here?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^][^]

It can be done by either passing parameterized constructor or define one method in 1st form and take reference of that form in 2nd and call the method...
shaikh-adil 11-Dec-12 8:36am
sir i am doing here dynamic generated control. so that example have almost dozens of code which is mine?
Krunal Rohit 11-Dec-12 8:39am
public string _CmbValue
return comboBox1.Text;
comboBox1.Text = value; }
shaikh-adil 11-Dec-12 12:19pm
i have to write this code in the button click event of the form which contains dynamic comboboxes?? or i have to write this code in the form4?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Do you remember what I told you this morning, about casting controls?
"If a Control is a ComboBox, then it can't also be a Label! So when you execute
ComboBox cb = sender as ComboBox;
Label lb = sender as Label;
You are guaranteed that one of the two variables will be null - so when you try to use both, you are certain to get the error."

That applies to Buttons and ComboBoxes as well:
Button btnh = sender as Button;
ComboBox cb1 = sender as ComboBox;

Any by the way - don't transfer data like this:
Form4 f4 = new Form4();
f4.comboBox1.Text = bs.Text;
Make the ComboBox in Form4 private again, and use a property instead:
In Form4:
public string WhateverTheComboBoxIsMeantToDoUseYourOwnNameHere
   get { return comboBox1.Text; }
   set { comboBox1.Text = value; }
That way, you can change the design of Form4 later without it affecting forms outside it. (Remember the OOPs fundimentals!)

[edit]"Labels" changed to "ComboBoxes" - OriginalGriff[/edit]
shaikh-adil 11-Dec-12 12:18pm
i have to write this code
public string WhateverTheComboBoxIsMeantToDoUseYourOwnNameHere
get { return comboBox1.Text; }
set { comboBox1.Text = value; }
in the public section of form4 and what i have to write in the button click event? of the form which contains dynamic comboboxes
OriginalGriff 11-Dec-12 12:27pm
Pretty much what you have already:
private void button2_Click(object sender, EventArgs e)
Form4 f4 = new Form4();
ComboBox cb1 = sender as ComboBox; //******
Button bs = cb1.Tag as Button;
f4.WhateverTheComboBoxIsMeantToDoUseYourOwnNameHere = bs.Text;
Except I'm pretty sure that you should be converting the sender to a Button, not a ComboBox, then checking it is not null, and using the Tag property (cast as a CheckBox and tested) to provide the Text...(Look at the last thread - we covered this :laugh:)
shaikh-adil 11-Dec-12 12:40pm
Form4 f4 = new Form4();
ComboBox cb1 = sender as ComboBox;
Button bs = cb1.Tag as Button;
f4.comboBox1.Text = cb1.Text;
this both of them are not happening
Form4 f4 = new Form4();
Button bt = sender as Button;
ComboBox cb2 = bt.Tag as ComboBox;
f4.comboBox1.Text = cb2.Text;
OriginalGriff 11-Dec-12 14:03pm
It's a Button Click event handler. So, what would you expect sender to contain? It's a Button, not a ComboBox, so the first version is never going to work.
The second is better (but you need to get into the habit of checking for nulls - it will bite you on the ass if you don't)
So, use the debugger. Put a breakpoint on the "Button bt" line, and see what you have in the various variables. It's easy to do, and it gives you a lot of information. When the breakpoint hits, look at sender - what is it? Then single step the line. Look at bt - and at it's content, such as the Tag field.
Then think about it, and let me know what you think.
shaikh-adil 12-Dec-12 0:05am
here bt = null and cb2 is null, and bt.tag is null
i observed, i dont know i am correct or not cz i am using breakpoint first in my life i have googled it and found how to use it
OriginalGriff 12-Dec-12 4:11am
bt will be null when teh breakpoint hits and your program stops running. That is the whole point.
Breakpoints are the first tool in a (very big and powerfull) set of helpers called "debugging tools" or "the debugger".
They allow you to specify "I want to know what it going on here" and your program will stop and wait while you work it all out. When you have stopped at a breakpoint you can do many things, but the important ones to start with are:
1) looking at variables: loads of ways to do this, the simplest is to put the mouse over a variable name and it will show you the content. (Also look at the "auto" pane, but ignore the "watch" pane for the moment - it does a lot of useful things!)
2) Change your code. You can make changes as see what will happen without re-starting your program.
3) Say which line will execute next (right click a line in the method, and look at the context menu)
4) Single step your code. This means to run it a line at a time - either stepping into methods, or over them. This is eriously useful, as it lets you follow exactly what you program is doing as it does it.
5) Continue from that point. When you want to go on with the program.

Get used to teh debugger - it is probably the best friend your coding will ever have! :laugh:

For teh moment though, the red line is the one that will be executed next - so bt will be null, and so will cb2. Look at sender - what type is it (hold the mouse over and it will tell you). It's a button - so single step (there is a button on a toolbar which will have an appropriate tooltip - look for it and press it once) the red line has moved, and now bt is no null - have a look at it, and it's content.
shaikh-adil 12-Dec-12 4:24am
there is a button on a toolbar which will have an appropriate tooltip - look for it and press it once
where is this??
and what are you teaching me. just tell me sir where i have been wrong, i learn from examples its m habit
OriginalGriff 12-Dec-12 4:30am
Break the habit! Learn by thinking, it's much more fun, and it means you understand it better when you come to use it properly!
Run your program until it hits the breakpoint. Then look at your toolbars in the debugger - if you slowly move the mouse over them a tooltip will appear to tell you what they do. You are looking for "step into " and "step over" - at the moment it doesn't matter which one you use (you'll work out the difference later). I can't describe the buttons!
shaikh-adil 12-Dec-12 4:35am
okay. i think i will atleast a day for that.
thank you sir
OriginalGriff 12-Dec-12 4:37am
A day! Good grief, you are a student, aren't you? :laugh:

You're welcome!
shaikh-adil 12-Dec-12 4:40am
yess i am a student. thats why it will take atleast a day.
i am planning to come in whales and to have a teacher, named griff
OriginalGriff 12-Dec-12 4:54am might want to rephrase that.
A Whale is a marine mammal that weighs up to 120 tonnes.
And to "come in" "whales" has a totally different meaning than you think it does...:blush:
"Wales" is a country, and you would probably hate it - it's bloody freezing here! :laugh:
shaikh-adil 12-Dec-12 5:10am
i was kidding, i am poor guy which dont have money to study also, so how can i travel wales, dont worry at a time i will come and meet the old guy there
OriginalGriff 12-Dec-12 5:18am
I never worry! (And it is bloody freezing this morning - I had to scrape the ice off the car so I could see to take the wife to work)
shaikh-adil 12-Dec-12 5:30am
OriginalGriff 12-Dec-12 5:33am
Never worry about anything you can't control! It's just a waste of your time.
And if you can control it, do something about it instead of worrying.

Works for me...:)
shaikh-adil 12-Dec-12 5:34am
same thing i do
shaikh-adil 12-Dec-12 4:38am
can you tell me one more thing, i have a label in the form which is keeping track of generated textbox, i use this code in the
click event where the textbox is generated
label1.Text = c.ToString();
andi ave this code where the button will remove the textbox the i want it to update the label
but i dont know how to do that. substraction is causing problem.
sariqkhan 12-Dec-12 0:13am
shaikh-adil 12-Dec-12 0:18am
hey bro do you know how to use breakpoint?
see my upper comment it is correct?
Prasad_Kulkarni 12-Dec-12 1:32am
For break point; just press F9 on line where you want to use it. I think OG already solved your problem.
shaikh-adil 12-Dec-12 1:34am
yess but i am not getting the output. the text is not passing , sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100