Hello guys,

I have a question. In one form i have a treeview. I get the value by doing treeview.selectednode.text; But in the same project i have another form. I want to send the value of the selectednode to the other form.

How can i do this?

Hi,

One way to do that is to pass the value as a parameter when you create the "other form":

//This is where you create the "Other form"
OtherForm otherForm = new OtherForm(treeview.selectednode.text);
otherForm.Show();

//This is in the "Other form"
public class OtherForm : Form
{
  string value;
  public OtherForm(string theValueYouJustPassed)
  {
     value = theValueYouJustPassed;
  }
}

So when you create the other form the value will be passed in the constructor, and your "value" field will contain the value you passed.

I hope you understood and it helped!

This one works, but this was not the problem. I have a form otherForm.Show();
I have a string in this form i have to get back to the main form.

I hope you can help me with this.

Okay, now in that case you can create a property in the "otherForm"

public partial class otherForm : Form
    {
        public otherForm()
        {
            InitializeComponent();
            getvalue = "value";

        }

        public string getvalue { get; set; }
    }

And when you call the Show() or ShowDialog() in the main form, you can access that property when you need to, for instance:

public partial class Form1 : Form
    {
        public string s;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            otherForm of = new otherForm();
            of.Show();
            s = of.getvalue;
        }
    }

I hope it works ;)

it works. Thnx for youre help

If you are using Show() rather than ShowDialog() to display the second form then you have the problem of the first form not knowing when to access the property of the second.
I posted a tutorial that shows how to pass a reference to Form1 into Form2 allowing Form2 to assign the value back to Form1. Let me know if it helps, or if you need anything explained.

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.