Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one form(mainform) with a listbox on it and another form(form1) with two TextBoxes. Upon the click of a button I want the data from the two textboxes to be read and displayed on he ListBox in form mainform.
How can I achieve that?

Thanks in advance!
Posted
Updated 17-Feb-11 22:12pm
v3

It will depend on how you want the form1 to work:

If form1 closes as well when you press the button, then set up two properties which provide access to the TextBox content and read them from mainForm. I would suggest using form1.ShowDialog in this case, rather than form1.Show.

If form1 doesn't close, then the best way is to set up an event in form1, which mainForm handles.
form1 throws the event, and provides the data via the EventArgs:

In form1:

public partial class form1 : Form
   {
   // Signal file change happened
   public event EventHandler Changed;

   protected virtual void OnChanged(EventArgs e)
      {
      EventHandler eh = Changed;
      if (eh != null)
         {
         eh(this, e);
         }
      }

   private void DoSomethingToChangeData()
      {
      OnChanged(null);
      }
   }

----- The assign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)

----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)

In mainForm:
public mainForm()
    {
    frmChild.Change += new frmChange.ChangeHandler(Changed);
    }

private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.ShowDialog();
    }

//
// Fired when the file is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    }

***************************************
You can pass data in this way via the EventArgs parameter:
form1:
public partial class form1 : Form
    {
    public event EventHandler<ChangedArgs> Changed;

    private void butGo_Click(object sender, EventArgs e)
        {
       EventHandler ch = Changed;
       if (ch != null)
          {
          ch(this, new ChangedArgs(tbData.Text));
          }
        }
    }

public partial class ChangedArgs : EventArgs
    {
    public string strData;
    public ChangedArgs(string str)
        {
        strData = str;
        }
    }


mainForm:
public partial class mainFrom : Form
    {
    form1 otherForm = new form1();
    private void mainForm_Load(object sender, EventArgs e)
        {
        otherForm.Changed += new EventHandler<ChangedArgs>(Changed);
        otherForm.Show();
        }

    private void Changed(object sender, ChangedArgs e)
        {
        if (e != null)
            {
            tbData.Text = e.strData;
            }
        }
    }
 
Share this answer
 
Comments
_Maxxx_ 20-Feb-11 19:19pm    
This is a good way to do it assuming the button mentioned by the OP is on Form1 not on the MainForm
Have a look at this[^]
 
Share this answer
 
After instantiating form1 pass a reference of mainform to form1. You can achieve that by defining a public property, field or by a method or even in the constructor of Form1 (then it would be during instantiation and not after). The type of mainform needs to have a public property that exposes the ListBox.

Cheers!
 
Share this answer
 
Comments
Manas Bhardwaj 18-Feb-11 7:03am    
not sure if this answer deserves to get 1. the only reason could be that you did not give COOKED code. :)
nyways, have my 5.
Manfred Rudolf Bihy 18-Feb-11 7:17am    
Some people are like that :)
Thanks for the vote.
fjdiewornncalwe 18-Feb-11 14:45pm    
Agreed.
_Maxxx_ 20-Feb-11 19:22pm    
I think this does deserve a 1, frankly.
Passing a reference to a Form, and allowing the 2nd form to have public access to a control on that form, just ties the two together too tightly, requiring changes to both forms should anything change - for example if the list box becomes a grid.
Trying to keep individual classes somewhat self-contained is a good thing - and while your solution certainly works, with anything bigger than the simple 2 form scenario it can soon lead to all sorts of maintenance issues.
 
Share this answer
 
You got 2 Forms:
Mainform
and
Form1

Controling controls from Form1 inside Mainform is not a problem You just need to set all controls and values to public... to achieve backwards simply You need to create Class which keeps all controls You want to control in Mainform, then push the object of that class in a constructor of Form1. This is only one of many ways... simple and without Delegate, Events stuffs :)


For example:
(both forms got 2x textbox, and 1x button)

public partial class MainForm : Form
{

    Form1 f1;
    ControlsKeeper ckObj;
    public MainForm()
    {

        InitializeComponent();

    }

    void MainFormLoad(object sender, EventArgs e)
    {

        ckObj = new ControlsKeeper(textBox2, button1);
        f1 = new Form1(ckObj);
        f1.Show();

    }

    void Button1Click(object sender, EventArgs e)
    {
        f1.textBox2.Text = this.textBox1.Text;
    }
}



public class ControlsKeeper
{

    public TextBox tb1;
    public Button bt1;
    public ControlsKeeper(TextBox tb1, Button bt1)
    {

        this.tb1 = tb1;
        this.bt1 = bt1;

    }

}


C#
public partial class Form1 : Form
{
    ControlsKeeper ck;
    public Form1(ControlsKeeper ck)
    {
        this.ck = ck;
        InitializeComponent();
    }
    void Button1Click(object sender, EventArgs e)
    {
        ck.tb1.Text = this.textBox1.Text;
    }
}
 
Share this answer
 
v2
Comments
_Maxxx_ 20-Feb-11 19:17pm    
While it is true that this will work, it's certainly not a good way of doing it - your 2 forms are now tightly coupled - any changes to one form will almost certainly require changes in the other. For example - if one of the text boxes changes to a drop-down then code in both forms will need to change.
I'm a bit horrified by some of the answers here, so here's my 2c. Sorry no code but I am on the mobile...

Assuming that it is MainForm that instantiates the other form, and that the button to which you refer is on MainForm, then all you need to do is add two properties in your Form1 - TextBox1Value and TextBox2Vue for example. In the Getter for these, return the appropriate text box.Text value.
In the button click event of MainForm you then just do something like...

ListBox1.Items.Add(form1.TextBox1Value)

If the Button is on Form1, on the other hand, it is more complex because Form1 won't have a reference to MainForm (and probably shouldn't have)

If Form1 is modal the you can do as my previous suggestion.

If not, then you should think about reversing it - I'd have properties on mainform that are art by the button click event on form1. Form1 then also needs a property of type MainForm which will be set by MInForm
 
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