Muhammad Ali
Muhammad Ali

Reputation: 3536

How to send a TextBox value to a variable in the parent form in C#?

I have an aboutBox control ("settings") that has a textBox in it. When the user presses the save button on this settings I want it to save the string from the textBox, pass it to a variable in the main form, close the settings box and then press a button in the main form.

How would this be done?

Thank you!

Upvotes: 0

Views: 270

Answers (5)

faljbour
faljbour

Reputation: 1377

this is a sample code on how you would do it, you can add an event handler for the save button when your main start or if you use VisualStudio, double click on the Save button from your designer and it will create the event handler for you, but to add it manually at the start of the form,

In your main form,

private void settingsButton_Click(object sender, EventArgs e) 
{ 
    settingsBox box = new settingsBox(); 
    DialogResult  result = box.ShowDialog(); 
    //* check if the result to see if you did not cancel the save.
    //* if the user clicked on save

    String textVar = box.GetMyAboutText();
}

This is in your settings form

    String textVar = null;  //* define global in your settingsBox;
    public settingsBox()//* constructor
   {
      settingForm.Click += new System.EventHandler(myAboutSaveButton_Click);
   }

//*add the myAboutSaveButton_Click to handle the event 

    private String myAboutCtlSaveButton_Click(object sender, EventArgs e)
    {
      textVar = myTextBox.Text;
      myAboutCtl.Dispose();

    }
       private void GetMyAboutText()
       {
          return textVar;
       }

Upvotes: 1

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13059

A solution using public methods. I have used controls with default names, see the comments for what these controls are related to.

Your Main.cs form :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new About(this).ShowDialog(); // pass main form to about form and show it a showdialog
        }

        // Public textbox updating method 
        public void textupdatert(string input)
        {
            textBox1.Text = input; // textBox1 is the textbox we need to update in Main form
        }
    }
}

Your About.cs form :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class About : Form
    {
       private Mainform mymainform; // Holds main form instance

       // Contructor is updated to take the instance of Main Form
       public About(Mainform mainform)
       {
         InitializeComponent();
         mymainform = mainform;
       }

        private void button1_Click(object sender, EventArgs e)
        {
            mymainform.textupdatert(textBox1.Text); // Update Parent form's'textBox1

            this.Close(); // Close about form and Exit 
        }

    }
}

This should work :)

Upvotes: 1

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

You can try as below:

string txtBoxVal=TextBox.txt;
you can pass the textbox value using parameterized constructor of parent form.

call the parameterized constructor of parent form like

parentForm pf=new ParentForm(txtBoxVal)

In the parent form write a constructor as below:

string val; //globalVariable in the parent class
public ParentForm(string txtVal){
val=txtVal; // textbox value is assigned to val that you can use as your needs                 // in the class
}

Hope this helps, Thanks

Upvotes: 0

denys-vega
denys-vega

Reputation: 3697

Add a property in FormSettings to return the TextBox value.

public string SomeText
{
    get { return textBox1.Text; }
}

Also, set the DialogResult = OK property to the Apply button, it will close the form dialog after press the button.

Usage:

using (var form = new FormSettings())
{
    if (form.ShowDialog() == DialogResult.OK)
    {
        var text = form.SomeText;
        //do something
    }
}

Upvotes: 0

nwnebel
nwnebel

Reputation: 33

First thought is to create an event in the form where you press the save button and raise this event when this button is clicked. You can listen to this event from your parent form and perform needed actions from there

Upvotes: -1

Related Questions