data transfers between two forms
-
In Form1
public partial class Form1: Form
{
internal static string textboxdata = string.Emtpy;
public Form1()
{
InitializeComponent();
}private void btnEkle_Click(object sender, EventArgs e)
{
textboxdata = txtGsm1.Text ;}
}in Form2 call this way
txtGsm1.Text = Form1.textboxdata ;
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
Why suggest a static solution? :wtf: Do you really think that is a good idea? If so, then perhaps you ought to re-read your C# books / lecture notes and brush up on basic OOP.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Look at your code. See that keyword on the first line of each method? The
new
keyword? Now, what do you think that does?- Create a totally new variable, unconnected to any previous versions.
or
- Access the correct old version of the variable that you had kicking around somewhere else in your program, but not to worry, the compiler will sort out which one your mean to use when it gets to it.
Now, do you see what is wrong?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Why suggest a static solution? :wtf: Do you really think that is a good idea? If so, then perhaps you ought to re-read your C# books / lecture notes and brush up on basic OOP.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
:zzz:
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
Use the existing instance.
private void btnRehber_Click(object sender, EventArgs e)
{
Form2 rhbForm = new Form2();
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
rhbForm.ShowDialog();
txtGsm1.Text = rhbForm.txtGsm1.Text;
txtGsm2.Text = rhbForm.txtGsm2.Text;
txtNumara.Text = rhbForm.txtNumara.Text;
}Except that won't work as it is because the TextBoxes are (correctly) private to Form2. So instead add them as properties to Form2:
public class Form2 : Form
{
...
public string Gsm1
{
get { return txtGsm1.Text; }
}
...
}Reapeat for Gsm2 and Numara. Then access Gsm1 in Form1:
private void btnRehber_Click(object sender, EventArgs e)
{
Form2 rhbForm = new Form2();
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
rhbForm.ShowDialog();
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
txtNumara.Text = rhbForm.Numara;
}It would also be a good idea to make "yetkili" a property as well, this time with a setter as well as a getter. The idea is that Form1 does not need to know how Form2 works - just that it can get Gsm data for it. You can then change the internals of Form2 as much as you like, without having to alter Form1
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
hey guys.. i have a problem..actually i saw many document about it and i applied on my project but i couldnt solve it..i want to transfer datas btw two forms..for example i open Form2 with a button in Form1 and i want to get the text of the textbox in Form2 as a text of textbox in Form1 to do that i wrote that codes below but still i have nothing here i opened Form2 private void btnRehber_Click(object sender, EventArgs e) { Form2 rhbForm = new Form2(); rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text; rhbForm.ShowDialog(); } and here i filled the textboxes in Form2 and submit the values to the textboxes in Form1 private void btnEkle_Click(object sender, EventArgs e) { frmFirma = new Form1(); frmFirma.txtGsm1.Text = txtGsm1.Text; frmFirma.txtGsm2.Text = txtGsm2.Text; frmFirma.txtNumara.Text = txtNumara.Text; } do i make something wrong ? or is there another way ? thanks guys for help
It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:
private void btnRehber_Click(object sender, EventArgs e)
{
using (Form2 rhbForm = new Form2()) // Please rename Form2.
{
// It's good practice to Dispose of forms. By wrapping
// this in a using block, the form will be disposed of
// automatically.
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
if (rhbForm.ShowDialog() == DialogResult.OK)
{
// Only update the fields if the user clicked OK.
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
frmFirma.txtNumara.Text = rhbForm.Numara;
}
}
}Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
Use the existing instance.
private void btnRehber_Click(object sender, EventArgs e)
{
Form2 rhbForm = new Form2();
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
rhbForm.ShowDialog();
txtGsm1.Text = rhbForm.txtGsm1.Text;
txtGsm2.Text = rhbForm.txtGsm2.Text;
txtNumara.Text = rhbForm.txtNumara.Text;
}Except that won't work as it is because the TextBoxes are (correctly) private to Form2. So instead add them as properties to Form2:
public class Form2 : Form
{
...
public string Gsm1
{
get { return txtGsm1.Text; }
}
...
}Reapeat for Gsm2 and Numara. Then access Gsm1 in Form1:
private void btnRehber_Click(object sender, EventArgs e)
{
Form2 rhbForm = new Form2();
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
rhbForm.ShowDialog();
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
txtNumara.Text = rhbForm.Numara;
}It would also be a good idea to make "yetkili" a property as well, this time with a setter as well as a getter. The idea is that Form1 does not need to know how Form2 works - just that it can get Gsm data for it. You can then change the internals of Form2 as much as you like, without having to alter Form1
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:
private void btnRehber_Click(object sender, EventArgs e)
{
using (Form2 rhbForm = new Form2()) // Please rename Form2.
{
// It's good practice to Dispose of forms. By wrapping
// this in a using block, the form will be disposed of
// automatically.
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
if (rhbForm.ShowDialog() == DialogResult.OK)
{
// Only update the fields if the user clicked OK.
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
frmFirma.txtNumara.Text = rhbForm.Numara;
}
}
}Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
You're welcome.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:
private void btnRehber_Click(object sender, EventArgs e)
{
using (Form2 rhbForm = new Form2()) // Please rename Form2.
{
// It's good practice to Dispose of forms. By wrapping
// this in a using block, the form will be disposed of
// automatically.
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
if (rhbForm.ShowDialog() == DialogResult.OK)
{
// Only update the fields if the user clicked OK.
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
frmFirma.txtNumara.Text = rhbForm.Numara;
}
}
}Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Got to agree with Pete here. Simple, elegant solutions will always work better than convoluted logic.
-
It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:
private void btnRehber_Click(object sender, EventArgs e)
{
using (Form2 rhbForm = new Form2()) // Please rename Form2.
{
// It's good practice to Dispose of forms. By wrapping
// this in a using block, the form will be disposed of
// automatically.
rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
if (rhbForm.ShowDialog() == DialogResult.OK)
{
// Only update the fields if the user clicked OK.
txtGsm1.Text = rhbForm.Gsm1;
txtGsm2.Text = rhbForm.Gsm2;
frmFirma.txtNumara.Text = rhbForm.Numara;
}
}
}Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
I agree this is the proper way of communicating between 2 forms. I would also add that you could overload the constructor of the form to pass in a value. And, you only need to dispose a form if you use ShowDialog[^].
"Make everything as simple as possible, but not simpler." - Albert Einstein
Jason Henderson
-
hey guys.. i have a problem..actually i saw many document about it and i applied on my project but i couldnt solve it..i want to transfer datas btw two forms..for example i open Form2 with a button in Form1 and i want to get the text of the textbox in Form2 as a text of textbox in Form1 to do that i wrote that codes below but still i have nothing here i opened Form2 private void btnRehber_Click(object sender, EventArgs e) { Form2 rhbForm = new Form2(); rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text; rhbForm.ShowDialog(); } and here i filled the textboxes in Form2 and submit the values to the textboxes in Form1 private void btnEkle_Click(object sender, EventArgs e) { frmFirma = new Form1(); frmFirma.txtGsm1.Text = txtGsm1.Text; frmFirma.txtGsm2.Text = txtGsm2.Text; frmFirma.txtNumara.Text = txtNumara.Text; } do i make something wrong ? or is there another way ? thanks guys for help
hi... instead of using "txtGsm1.Text" better u declare a property in form2 . Then using that property u can do this. :)