Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>1- (Not recommended if there's too many parameters) Passing data through the constructor.</p> <pre><code> private void ShowForm(int a, string b, double c) { Form2 frm = new Form2(a, b, c); frm.ShowDialog(); } </code></pre> <p>2- Using public fields of target class. (NOT RECOMMENDED AT ALL)</p> <pre><code> private void ShowForm(int a, string b, double c) { Form2 frm = new Form2(); frm.intval = a; frm.strval = b; frm.doubleval = c; frm.ShowDialog(); } </code></pre> <p>3- Using properties.</p> <pre><code> private void ShowForm(int a, string b, double c) { Form2 frm = new Form2(); frm.IntValue = a; frm.StringValue = b; frm.DoubleValue = c; frm.ShowDialog(); } </code></pre> <p>4- Using tags.</p> <pre><code>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(); } </code></pre> <p>5- Using delegates. (This one is a little bit tricky).</p> <pre><code> //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) { } </code></pre> <p>My personal favorite ones are the properties, delegates and in some rare cases constructors.</p> <p>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.</p> <h2>Update</h2> <p>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:</p> <h3>For this particualr case:</h3> <ol> <li>Open form designer in the visual studio.</li> <li>Double click on the button1. (This will generate an event handler for button1's click event).</li> <li><p>Fill the function like this:</p> <pre><code>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; } </code></pre></li> </ol> <p>Here the <code>ChangeString</code> function is essentially doing what you want the <code>Program.cs</code> 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.</p> <h3>What I recommend for you in general:</h3> <p>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.</p> <p>That said, I should quickly say that the <code>Program.cs</code> 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.</p> <p>I hope this helps you get started.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload