Data transfer between form1.cs and program.cs

I know this is really basic, but I could not find manuals / guides on how to do this between MSDN, Google searches, and stackoverflow.

I created a new Windows Form application, and here I have a program and form1, where Form1 owns 2 text fields and buttons.

Button1 should take the line from Text1 and send it to program.cs, where the line will be edited and sent back to Form1. Button2 then shows the new line in Text2.

I got it before receiving a line from Text1 (with Button1), but I can’t figure out how to send it to program.cs so that it can be processed. What exactly should I do to transfer data between the two?

I'm not sure if this is the right start, but I created myForm in an attempt to get the passed string. But how do I send it back?

In Program.cs:

static Form1 myForm;
[STAThread]
static void Main()
{

    string s1, s2;
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    myForm = new Form1();
    Application.Run(myForm);
    s1 = myForm.sendOver();

}
+5
source share
3 answers

You effectively cannot. Application.Run(myForm);will be blocked until your form is closed, so you no longer need to receive data from it or give data to it.

In all winform programs, you should never modify program.cs. Although you can sometimes make it work, it is rarely desirable in terms of design.

, , , , . ( ) ( , , ..).

+4
, , . . , .

1- ( , ) .

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2(a, b, c);
    frm.ShowDialog();
}

2- . ( )

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.intval = a;
    frm.strval = b;
    frm.doubleval = c;
    frm.ShowDialog();
} 

3- .

 private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.IntValue = a;
    frm.StringValue = b;
    frm.DoubleValue = c;
    frm.ShowDialog();
} 

4- .

private void ShowForm(int a, string b, double c)
{
        Form2 frm = new Form2();
        frm.SomeTextBox.Tag = a;
        frm.SomeTextBox2.Tag = b;
        frm.SomeTextBox3.Tag = c;
        frm.ShowDialog();
} 

5- . ( ).

 //in Form2
public delegate void PassValues(int a, string b, double c);
public PassValues passVals;

private void PassDataThroughDelegate(int a, string b, double c)
{
    if(passVals != null)
        passVals(a,b,c);
}

//in Form1
private void ShowForm(int a, string b, double c)
{
    Form2 frm = new Form2();
    frm.passVals = new Form2.PassValues(UseData);
    frm.ShowDialog();
}

private void UseData(int a, string b, double c)
{
} 

- , .

, , .   , . , .

Update

( - ). , , :

:

  • .
  • 1. ( click1 ).
  • :

    private void button1_Click(object sender, EventArgs e)
    {
        textBox2.Text = ChangeString(textBox1.Text);
    }
    
    private string ChangeString(string str)
    {
        string result = "do some stuff to " + str;
        return result;
    }
    

ChangeString , Program.cs. . , , .

:

, OO (Object Oriented) . OO , , . , .Net # , . , Winforms .

, , Program.cs . . , , .

, .

+1
static Form1 myForm;
[STAThread]

//we need a holder
//you can access static classes from your forms
public static class myCls {
    public static string myStr;
    static void myFunc(string str) {
        myStr = str; //or whatever
    }
}

static void Main()
{

    //string s1, s2;  //you don't need these
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    myForm = new Form1();
    Application.Run(myForm);
    //s1 = myForm.sendOver(); //you won't use this

}

your form1 should contain:

//change your holder content whereever you want
void Button1_Click() {
    myCls.myFunc("string");
}

your form2 should contain:

//then call your holder content whereever you want
void Button1_Click() {
    Text1.Text = myCls.myStr;
}
-1
source

All Articles