1

I create a form that contains a public field and a button. The button creates another form and shows it as dialog. Can I access to the public fields on main form? the main form run in main method like this:

static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

public partial class MainForm : Form
    {
        public int number;

        public MainForm()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            (new SecondForm()).ShowDialog();
        }
    }

public partial class SecondForm : Form
    {
        public SecondForm()
        {
            InitializeComponent();
        }
        void Method()
        {
            //How can I access to number?
        }
    }

I don't want use constructor because there are a lot of variables in mainform.

mohammad
  • 1,248
  • 3
  • 15
  • 27

6 Answers6

2

You can use the ShowDialog Method that sets the owner of the created form.

secondForm.ShowDialog(this);

usage in your second form:

var temp = ((MainForm)this.Owner).number;
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
0

Create an object of the MainForm class and you can use it access all the public variables in the class

MainForm f = new MainForm();
int a = f.number;

This might not really work as it will create a new instance of the MainForm class and you might not get the desired result, so a better will be to make number static.

public static int number;

Then you can use

int a = MainForm.number;
  • 2
    That will be a totally different instance than the one that created the second form. – Mark Hall Dec 23 '13 at 15:55
  • @Downvoter, I agree this is not the best answer but does it make it worthy of a downvote? –  Dec 23 '13 at 16:14
  • Can someone please explain the downvotes. I would love to improve?? –  Dec 23 '13 at 16:26
0

Sure, just do:

var form = new SecondForm();

form.<property> = <value>
// ...

DialogResult result = form.ShowDialog(); 

if (result == DialogResult.OK)
{
    // ...
Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
0

You said you don't want to use the SecondForm constructor to pass all of those variables from MainForm. Why not just create a class to hold the values and pass the class instance between the two?

public class FormData
{
    public int number;
    public int anotherInt;
    public FormData(int num)
    {
        number = num;
    }
}



public class SecondForm : Form
{
    private FormData myData;
    public SecondForm(FormData data)
    {
        myData = data;
    }
}



public void AMethodInMainForm()
{
    FormData d = new FormData(this.number);
    d.anotherInt = 26;
    SecondForm frm = new SecondForm(d);
}
Brandon
  • 4,491
  • 6
  • 38
  • 59
0

Define a public number in SecondForm

public int number;

Then before you show SecondForm try this:

SecondForm f2 = new SecondForm();
f2.number = this.number;
f2.ShowDialog();

Then you can access your number inside of your SecondForm.This works if you call number variable several times from your methods inside of your form2.But if you want access number in Form1 whenever you want,you should define it as Static and Public

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

Declare your number as public static int number and then:

public partial class SecondForm : Form
{
    public SecondForm()
    {
        InitializeComponent();
    }
    void Method()
    {
        MainForm.number = // something
    }
}
r.mirzojonov
  • 1,209
  • 10
  • 18