0

I have been stuck on this for hours and i just cant get my head around it... im pretty new to coding.

I have 2 forms

Form1 has a blank string variable

Form1 has a open button which opens form2

Form2 has multiple check boxes

Form2 has an ok button

When form2 ok button is clicked it sets the variable i want to pass from form2 to form1. The variable is the checkBox. Checked which is set with an if statment.

After ok is pressed and form2 is closed.

Question is how do i get the string in form2 passed to form1.

A working example with some explanation would be great.

I have tried get and set but don't really understand it.

Nezz
  • 41
  • 2
  • 11

2 Answers2

0

Create a public static variable in form1

You can use static vars, which would be the easiest solution to archive your goal, but there are other ways like constructors, containers, events etc.

public static string var1
{
    get { return En; }
    set { En = value; }
}

public static string var2
{
    get { return var2; }
    set { var2 = value; }
}

And in the other form

private void button1_Click(object sender, EventArgs e)
{
    Form1 F1 = new Form1();
    Form1.var1 = textBox1.Text;
    Form1.var2 = textBox2.Text;
}

Please be advised that a static variable exists only once for a class. So if you have multiple instances and you change the static variable in one, the change also affects all other instances.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

Ok, so i got it sorted at last.

I was getting problems using get and set, I will leave that for another time when I have a little more experience, though, for now, this is what I did incase anyone else needs to do this.

Again, what I am doing is this:

Select a checkbox on form2, then press OK and an if statment runs and checks which checkbox was checked and sets a string varaible based on that for a validation check, then form2 closes.

On form1, I just loaded the checkBox.Checked Text property and set it as a variable. This is my code.

day in form2 is a global string variable and so is runDay in form1.

All the checkbox modifiers are set to public on form2 to enable form1 to read them, im not sure if that is good practise though, im still a newb.

Form2 (Sending the variable from here)

    //
    //OK BUTTON
    //
    public void okBtn_Click(object sender, EventArgs e)
    {
        if (cbMonday.Checked)
        {
            day = "Monday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbTuesday.Checked)
        {
            day = "Tuesday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbWednesday.Checked)
        {
            day = "Wednesday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbThursday.Checked)
        {
            day = "Thursday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbFriday.Checked)
        {
            day = "Friday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbSaturday.Checked)
        {
            day = "Saturday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (cbSunday.Checked)
        {
            day = "Sunday";
            MessageBox.Show("BackUp Will Run Every " + day + " ", "Scheduled", MessageBoxButtons.OK);
            this.Close();
        }
        else if (string.IsNullOrWhiteSpace(day))
        {
            MessageBox.Show("You have not selected any days", "Woops");
        }

    }

Form1 (Setting the variable here)

               private void scheduleBtn_Click(object sender, EventArgs e)
    {
        Schedule frm = new Schedule();
        frm.ShowDialog();
        if(frm.cbMonday.Checked)
        {
            runDay = "Monday";
        }
        else if(frm.cbTuesday.Checked)
        {
            runDay = "Tuesday";
        }
        else if(frm.cbWednesday.Checked)
        {
            runDay = "Wednesday";
        }
        else if(frm.cbThursday.Checked)
        {
            runDay = "Thursday";
        }
        else if(frm.cbFriday.Checked)
        {
            runDay = "Friday";
        }
        else if(frm.cbSaturday.Checked)
        {
            runDay = "Saturday";
        }
        else if(frm.cbSunday.Checked)
        {
            runDay = "Sunday";
        }
        dayLbl.Text = runDay;
    }
Nezz
  • 41
  • 2
  • 11