Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have 2 forms (form1 and Form2)
I want to write line in form 1(textbox) and show it in list box in form2 after I click on button.

see this:in form 2

C#
private void button1_Click(object sender, EventArgs e)
        {
form2 form2_load=new form2();
form2.showdialog();



in form2:
C#
private void Reault_Load(object sender, EventArgs e)
        {
form1 form1_load=new form1();
listbox1.items.add("result: "+form1_load.textbox1.text):

        }



But when I click on button1 I see form2 with only "result" and I dont see textbox1's text.

Could you help me?
Thanks
Posted
Updated 4-Sep-10 3:03am
v2
Comments
BillWoodruff 16-Oct-11 17:56pm    
I find it hard to understand your current problem description; why would you create a new form inside another Form invoked by 'ShowDialog ? Why would you have a button that every time you clicked it created this new Form ? Why would you ever use a ListBox that will only have one entry. I've added an answer, with code, here that makes a 'wild guess' at what you might be trying to do :) Hope it's helpful. If not, well, "koy bhat nahin" ?
Raven Saint 12-Jun-15 9:46am    
I have a similar issue. Though in the reverse.
I have form1 where I have a listbox that populates from a load command. No worries there. It saves a created *.txt file that my data is written to.
Now I have a form 2 which needs to write that data to the Form1 Listbox and save to the *.txt file without overwriting the original file already there. I have the form 1 working. Can callup the Form 2, but a similar problem as the OP is what I have. I can write more information if needed.
Could possibly be a function or query, but have no idea on how this can be done.

Form2 opens > Enter text into Text Box "txtTextForm2" > on click of "addInventForm2" send text to lstTextForm1 and save to *.txt File (writeline Method) > Form2 Box closes once coding has run adding text to file > Form1Listbox1 refreshes and now has text added to lstTextForm1 in addition to the text that was already present.

In form2 in the Reault_Load event you instantiating a new instance of Form1 that does not have the value set.

For passing a simple string you could try something like this

C#
public Form2(string txt)
{
...
}

private void button1_Click(object sender, EventArgs e)
{
 form2 form2_load=new form2(textbox1.text);
 form2.showdialog();
}
 
Share this answer
 
The best way for this problem are delegates.
 
Share this answer
 
Comments
Abhishek Sur 4-Sep-10 12:18pm    
Why delegates? I think delegates are intended to pass methods rather than passing the variable.
Just declare the variable to access Form 1 and Form 2 globally so that you can have access to both of them at a time.

In your form1 you create a method that will add the value to Listbox.

public void AddToListBox(string item)
{
   ListBox.Items.Add(item);
}


From your form2 you use

form1obj.AddToListBox(textBox1.Text);

:rose:
 
Share this answer
 
Hi roham_06,

Just Pass the value to create new constructor in your form2.

Example:

In Form1:
     
    private void button1_Click(object sender, EventArgs e)
    {
        form2 frm=new form2(textbox1.Text);
        frm.Show();    
    }

 In Form2:

    Public form2(string sValue)
    {
       IntializeComponent();
       listbox.Items.Add(sValue);
    }


But you have maintain the previous value of Listbox.Because if you call form2 means that form newly intialize the controls.So listbox items comes empty.

Another way is create one static string object (or) create properties in one class.Set the value in that class and then to get the value from that class to add the value into listbox.

Cheers :)
 
Share this answer
 
C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(textBox1.Text);
            frm2.Show();
        }



------------------------------------------------
C#
public Form2(string qs)
   {
       InitializeComponent();
       textBox1.Text = qs;

   }
 
Share this answer
 
This is one of the most frequently asked and answered questions here on this forum.

The scenario states that a button will be pressed ... we assume on Form1 ... and that the TextBox.Text in Form1 will be added to the ListBox.Items in Form2.

That there is a button to be pressed implies this update will be performed more than once.

So: Form1 needs access ... repeated access ... to the ListBox on Form2.

How do we accomplish that: well, there are several ways, and, because I am bored with doing it via a Public Property, this time, for variety's sake, and with a 'wing and a prayer' the OP just might be constructively challenged, we'll implement it using a static variable in Form2 that holds a reference to Form2's Listbox.
C#
public partial class Form2 : Form
{
    // assume the ListBox is named 'listBox1
    public static ListBox f2ListBox;

    public Form2()
    {
        InitializeComponent();
        //
        f2ListBox = listBox1;
    }
}
Now how do we use this from Form1:
public partial class Form1 : Form
{
    // note we do not bother to keep a reference
    // to the instance of Form2 here
    // all we want is access to the ListBox on Form2

    public Form1()
    {
        InitializeComponent();
    }

    private ListBox targetListBoxOnForm2;

    private void Form1_Load(object sender, System.EventArgs e)
    {
        new Form2().Show();

        // note we access the ListBox from
        // the Class Form2, not the instance of Form2
        targetListBoxOnForm2 = Form2.f2ListBox;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        // ignore white space
        if (string.IsNullOrWhiteSpace(textBox1.Text)) return;

        // add the item to the ListBox on Form2
        targetListBoxOnForm2.Items.Add(textBox1.Text);
    }
}
 
Share this answer
 

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