You are viewing our Forum Archives. To view or take place in current topics click here.
[C#] Setting string on main from from subform
Posted:

[C#] Setting string on main from from subformPosted:

Z61
  • TTG Fanatic
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
Status: Offline
Joined: Apr 16, 201014Year Member
Posts: 4,309
Reputation Power: 179
I have 2 forms
the main form is open at all times.
the second form is only opened at the start.
I need form2 to pass a string that the user inputs back to the main form but I can't figure it out.

       private void button1_Click(object sender, EventArgs e)
       {
           Form1 frm1;
           Form1.Option = textBox1.Text;
       }

I tried this, it obviously has faults.
Any ideas?
#2. Posted:
RDCA
  • TTG Contender
Status: Offline
Joined: Jul 12, 201014Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201014Year Member
Posts: 3,612
Reputation Power: 173
public TextBox MyTextbox

        {
            get { return MyTextBox; }
       
        }


Basically, it is returning an textbox obj, so you can get the text property from it. Just create an object for the form, like you where, and use MyTextBox in a retrieval operation, although you can easily add a set option so you can use it in assignment as well.
#3. Posted:
Imp
  • Blind Luck
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
The way I do this is

I have called the form open all the time MainForm and the form open at the start SubForm

On the MainForm do this


        public SubForm MySubForm;
        public String MySubFormData;

        private void MainForm_Load(object sender, EventArgs e)
        {
            MySubForm = new SubForm();
            MySubForm.myParentForm = this;
            MySubForm.ShowDialog();
           
        }


And on the SubForm do this


        public MainForm myParentForm;

        private void button1_Click(object sender, EventArgs e)
        {
            myParentForm.MySubFormData = "This is the Data";
            this.Dispose();
        }


Make sure the project opens MainForm when it starts.

This will, when it loads, open the SubForm and when you click the button, populate your mySubFormData on your MainForm. Which will stay there until the MainForm is destroyed.

Alternative to RDCA but works for me
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.