Hello, this is my 1st post here,:)
I have a problem With C#.net 2008,
I can't find the way to send variables from form to form, Could you post here all the ways to do so?
+ But my problem really is this.:icon_eek:
I have 2 Forms, 1-Form1, 2-SP(name of form "sp").
And form sp has a ListBox with text, I want to pass the selected text in that list box into form1's label(form 1 has a label called label2).
I can't really find the way to do so, please if you give me an answer please explain it to me so i would understand it too.
Thanks In advance.

Recommended Answers

All 16 Replies

There are several ways to do this. Does form1 call form SP?

What do you mean call?
Could you give me an example code?(for my 1st question..)

In other words, inside of Form1's class, you create form "SP", such as:

SP sp = new SP();
sp.ShowDialog();

In other words, inside of Form1's class, you create form "SP", such as:

SP sp = new SP();
sp.ShowDialog();

I know how to open a form but I asked how could I transfer from From2 that has a list box the selected item from the list box , and when button that transfers is pressed to show the value of the selected listbox item in the label in form1.

What do you mean call?
Could you give me an example code?(for my 1st question..)

There are soooo many ways to do this. Here is one technique:

public class MyVars
        {
            public string text1;
            public string text2;
        }

    public class Form1 : Form
    {
        MyVars myVars = new MyVars();

        SP sp = new SP (myVars);
    }

The above will pass a MyVars object to your SP form, which can be modified and the changes will be available to Form1.

I know how to open a form but I asked how could I transfer from From2 that has a list box the selected item from the list box , and when button that transfers is pressed to show the value of the selected listbox item in the label in form1.

If you understand the MyVars code I gave you, try to make it work and show your code with questions or problems you have. It sounds like you want someone else to write the code for you, but we don't do that.

No I don't want anyone to write to me but I am searching for a solution a lot of time...
ill check if what you gave works with list box ..

OK, 1st how could I actually change the label?
2 Where to put that code you gave me?

OK, 1st how could I actually change the label?
2 Where to put that code you gave me?

To change a label's text:

label1.Text = "some new text";

or,

label1.Text = myVars.text1;

As far as where to put that other code, I thought it was self-explanatory, but I would need to see your actual code to be more specific. If you post your code, I'll try to help with that.

Ok , I will post my code but later, Thanks for the help untill now.

DdoubleD's post, i think is what you really needed.

But I think this is what you are looking for

//SP Form
   public string x;
   public string y;

    public class SP : Form
    {
        Form1 form = new Form1(x,y);
        form.Show();
    }

//Form1

   string x,y;
   public Form1(string x, string y)
  {
     this.x = x;
     this.y = y;
  }

correct me if I'm wrong

Thank you but how would I pass the vars of the Numericupdown(in form sp+ it needs to be the selected string in there) to label2 (in form1),and I want to show that selected string from numeric' in label 2 when button on sp is pressed, I can't find how to do it.

DdoubleD's post, i think is what you really needed.

But I think this is what you are looking for

//SP Form
   public string x;
   public string y;

    public class SP : Form
    {
        Form1 form = new Form1(x,y);
        form.Show();
    }

//Form1

   string x,y;
   public Form1(string x, string y)
  {
     this.x = x;
     this.y = y;
  }

correct me if I'm wrong

That will work as long as second form does not modify the variables because when the strings get assigned they will get assigned new strings and thus a different reference from the starting variables.
This is why I placed the variables inside class MyVars.

Thank you but how would I pass the vars of the Numericupdown(in form sp+ it needs to be the selected string in there) to label2 (in form1),and I want to show that selected string from numeric' in label 2 when button on sp is pressed, I can't find how to do it.

Because NumericUpDown.Value is type 'decimal', you need to convert to and from type 'string' to store and retrieve from labels, textboxes, etc.

numericUpDown1.Value = Convert.ToDecimal(myString);
//and convert a decimal to string:
            myString = Convert.ToString(numericUpDown1.Value);

So, you can decide how you want to pass the value between the forms because the conversion is quite easy.

Be sure to consider my recommendation for putting your shared vars in a class (e.g. class MyVars or something). Otherwise, you are eventually going to find the vars are not referencing the same data on both forms.

Hello, this is my 1st post here,:)
I have a problem With C#.net 2008,
I can't find the way to send variables from form to form, Could you post here all the ways to do so?
+ But my problem really is this.:icon_eek:
I have 2 Forms, 1-Form1, 2-SP(name of form "sp").
And form sp has a ListBox with text, I want to pass the selected text in that list box into form1's label(form 1 has a label called label2).
I can't really find the way to do so, please if you give me an answer please explain it to me so i would understand it too.
Thanks In advance.

http://windowsclient.net/learn/video.aspx?v=108089
or
http://windowsclient.net/learn/video.aspx?v=105861

Hi , (don't know if good way but works)
what you can do is:
1.create a static variable for Form1

public partial class Form1 : Form
 {
        public static string selectedItemValue = "";
 }

2.
In form sp , when you click on a listbox item write this code

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form1.selectedItemValue = listBox1.SelectedItem.ToString();
        }

3.After that you can set your label's text to the static variable's value

Note:
1.You could improve it by using properties but I haven't tested it
2.I don't know if you will select multiple items

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.