0

I have a C# windows app that has two forms Form1 which is the main form and Form2. Form1 has a combobox on it and Form2 has a textbox.

I want to put the value selected in the Form1.ComboBox1 into Form2.TextBox1.

I am trying this:

        Form1 Form1Object = new  Form1();
        string fff = Form1Object.ComboBox1.SelectedItem.ToString(); //not working
        TextBox1.Text = fff;

Problem is that when I run this Form1 is reinitialized and i don't want that. (I have a splash screen that runt when the application starts so when i run my code the splashscreen starts all over again.

Is there a way to read ComboBox1 value without restarting the first form? If I try it directly it does not work, it sees the Form1 as calss instead of object.

    Form1.ComboBox1.SelectedItem.ToString(); //does not work

I am also trying to add the value to the textbox when opening the second form:

        Form2 form2 = new Form2();
        form2.TextBox1.Text = ComboBox1.SelectedValue.ToString();
        form2.Show();  

This gives me the following error: "Object reference not set to an instant of an object."

EDIT: It works using this code:

        Form2 form2 = new Form2();
        form2.TextBox1.Text = ComboBox1.Text;
        form2.Show();

Now my question still remains: If i am in Form2 can i still get the value from form1? If not, that is ok. I will post this as a solution.

Danut
  • 83
  • 3
  • 15
  • As long as your´re "in" yout Form1-class you may use the this-reference to the current instance. Thus you may reuse the same instance. – MakePeaceGreatAgain Nov 24 '14 at 15:36
  • You can use event. [Here is an example](http://stackoverflow.com/a/6382869/754438). But in your case use combobox selected item changed event instead of textBox1_TextChanged in example. – Renatas M. Nov 24 '14 at 15:39
  • "Object refference not set to an instant of an object." means that SelectedValue in your ComboBox is null. Debug and check what is in SelectedValue, SelectedItem and SelectedIndex properties of your combobox when this exception is thrown. – Arie Nov 24 '14 at 15:45
  • It looks like it works using ComboBox1.Text from Form1, but not working with SelectedValue. – Danut Nov 24 '14 at 15:51
  • really not being an asshole but ..... `I have a splash screen that runt when the application starts`. That line made my day. – DidIReallyWriteThat Nov 24 '14 at 20:04

3 Answers3

1

While this is not the most proper answer, it is one way to solve the problem.

Form1

Add a method to get value

  public string TransmitSelectedValue()
  {
    return ComboBox1.SelectedItem.ToString();
  }

Form2

 var myvalue = ((Form1)ParentForm.Controls.Find(Form1Name,true)).TransmitSelectedValue();
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
0

This type of question has been asked and answered many times, and in different versions.

I would suggest looking at a few of the following I have posted in the past...

This example shows two forms where second form is passed as a parameter the first form's instance. Then, from public methods exposed on the first, the second can call them to get the values. It is your discretion on if you want to allow setting from alternate source, or just allowing a get method... could be done as a property public get; protected set;

This stackoverflow search will show several links to posts I've done in the past with slightly altering versions between different forms.

FEEDBACK FROM COMMENT

There has to be something done in your FIRST form to call the second.. is it from a click button, or based on the actual combobox selection being changed. Whatever it is, the first Example I provided SHOULD be what you need. You are not having the second form call the first.

Without a full copy\paste of the first example, all you would need to really do is in the form 2 constructor is set your text as pulled from the first...

public Form2(Form1 viaParameters) : this()
{
    this.textBox1.Text = viaParameters.Combobox1.SelectedItem;
}

however, I don't know how your items are defined.. dictionary, list, array, whatever.. so you may need to typecast to get the selected item via

if( viaParameters.Combobox1.SelectedIndex > -1 )
  this.textBox1.Text = viaParameters.Combobox1.Items[ viaParameters.Combobox1.SelectedIndex ].WhateverStringValue;

This way, the start of form 2 from form 1 can grab the value directly.

If you expose the method from the first form via a property or method, your text value could be as simple as

this.textBox1.Text = viaParameters.YourForm1sMethodToGetStringFromCombobox();
Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • Hello, i have searched the forums and found examples, but on all examples the second form was initialized as frm2 and them from form 1 the user accessed frm2.object. What I needed is the other way around. I cannot initialize Form1 as frm1 and then use frm1 as the object since Form1 or opened first time when the app runs. – Danut Nov 24 '14 at 16:53
  • @Danut, review revision to my answer for clarification... Also, Try to do the entire first example to see how things DO work, then change via a sample of a combobox as per your scenario. – DRapp Nov 24 '14 at 17:37
0

i am not sure where the problem is

while starting/opening form2

like

        Form2 f2 = new Form2();
        f2.Show(this);

you have a reference to form1 as 'owner'

on form2 you can just put this on any event you want or on a button or whatever

        Form1 f1 = Owner as Form1;
        textBox1.Text = f1.comboBox1.SelectedItem.ToString();

converted to C# ...

Nocturnal
  • 386
  • 2
  • 8