Reputation: 2445
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();
}
Upvotes: 6
Views: 24623
Reputation: 15275
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.
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:
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.
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.
Upvotes: 2
Reputation: 549
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;
}
Upvotes: -1
Reputation: 203829
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.).
Upvotes: 4