Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
HI 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))
           panel1.Controls.Add(txtRun3);
          txtRun3.Focus();
       }

       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;
           f4.Show();
       }

i am getting error as
"Object reference not set to an instance of an object."
Posted
Updated 11-Dec-12 2:35am
v2
Comments
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?

Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^]
http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application[^]

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...
 
Share this answer
 
v2
Comments
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?
[no name] 11-Dec-12 8:39am    
public string _CmbValue
{
get
{
return comboBox1.Text;
}
set
{
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?
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
C#
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:
C#
Button btnh = sender as Button;
ComboBox cb1 = sender as ComboBox;


Any by the way - don't transfer data like this:
C#
Form4 f4 = new Form4();
f4.comboBox1.Text = bs.Text;
Make the ComboBox in Form4 private again, and use a property instead:
In Form4:
C#
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]
 
Share this answer
 
v2
Comments
shaikh-adil 11-Dec-12 12:18pm    
sir
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;
f4.Show();
}
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;
f4.Show();
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;
f4.Show();
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900