6

I know this is really basic, but I couldn't find any guides/tutorials on how to do this between MSDN, Google searches, and stackoverflow.

I created a new Windows Form Application and here I have Program and Form1 where Form1 owns 2 text boxes and buttons.

Button1 is supposed to take the string from Text1 and send it to program.cs where the string gets edited and sent back to Form1. Then Button2 reveals the new string in Text2.

I got as far as obtaining the string from the Text1 (with Button1), but cannot figure out how to send it to program.cs so it can be processed. What exactly am I supposed to do to pass data between the two?

I'm not sure if this is the right start, but I created a myForm in attempts to get the string sent over. 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();

}
0

3 Answers 3

4

You effectively can't. Application.Run(myForm); will block until your form is closed, so it's no longer appropriate to be getting data from it, or giving data to it.

In virtually all winform programs you shouldn't ever be modifying program.cs. While you can occasionally get it to work, it's rarely desirable from a design perspective.

If you want to do processing based on the result of your from, you should most likely be creating an entirely new class, separate from either of these. Call a method from that class (creating an instance of it if appropriate) when appropriate (this may be when a submit button is clicked, in the form closed event handler, etc.).

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, I think I've been misunderstanding the purpose of Program.cs. So all it does is just run the form. In that case, should I just process all the strings in Form1 after the InitializeComponent(); ?
That depends on where the strings come from, when you want to do the processing, what you plan to do with the results, etc. We know none of that. You may need to do this processing on form load, or form close, or no a button click, or any number of other options based on the specifics of your program.
In my scenario, the user inputs a string in the first text box and presses an update button. The data then goes into the program and alters the string by taking a few characters off. Then the user can press a display button which displays the modified string in text box 2. For a fairly simple procedure, I'm hoping this can all be done in Form1.
@krikara What you want to have is a method in a class that accepts a string and returns a string. In your button click handler get the text of one textbox, pass it as the parameter to this method in some new class, then take the return value of the method and assign it to the text of your second textbox. The processing of that thread can now be done in this new class without any knowledge of the UI that uses it. This is a model that you'll be able to use a lot, and is quite easy to do.
2

A little while ago I've answered an old question which asked how to pass data between two different forms. The answer applies for your situation too. Just pick the one that fits your needs.

1- (Not recommended if there's too many parameters) Passing data through the constructor.

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

2- Using public fields of target class. (NOT RECOMMENDED AT ALL)

 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- Using properties.

 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- Using tags.

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- Using delegates. (This one is a little bit tricky).

 //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)
{
} 

My personal favorite ones are the properties, delegates and in some rare cases constructors.

Alternatively, you can create a static class , put some properties in it, then use it in other forms. This is really helpful if all of your forms need to share some information. Since this is not a way to Pass data between the forms, I did not mention this method in those above.

Update

The above lists the methods to pass data between two classes (in this case forms). However, after reading your question and your comments I can recommend this:

For this particualr case:

  1. Open form designer in the visual studio.
  2. Double click on the button1. (This will generate an event handler for button1's click event).
  3. Fill the function like this:

    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;
    }
    

Here the ChangeString function is essentially doing what you want the Program.cs to do. Just add your functionality to it. If what you want it to do is more complex, you could create a class and add the functionality to that class.

What I recommend for you in general:

I think you should take some OO (Object Oriented) classes or read some books. You should get familiar with the concepts of OO and plan what you're supposed to do, before actually doing it. Also, you should read about the inner layers of .Net and C# and know what is the purpose of each part of the programs. I'd go with Console apps first and then go to the Winforms and other UI principles.

That said, I should quickly say that the Program.cs is just behaving as an entry point for your program. You shouldn't try to add logic to that class and try to keep the implementation of each class to itself. Try to separate the things each class can do to avoid complicated designs.

I hope this helps you get started.

Comments

-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 must contain:

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

your form2 must contain:

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

4 Comments

This might look promising, but it's a complete dead end of a thought process. Application.Run(myForm); blocks until the thread is closed, so by the time you get back to the main method you can no longer interact with the form to "give back" anything.
Yeah I have been trying stuff like this, but I can never send back anything to Form1.
i have added some explanation
@user1942754 I know this answer is general-purpose but seeing that many my strings in names, just hurts my eyes. Please try to pick meaningful names for your code, even for education purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.