locked
How to pass string data between two button clicks RRS feed

  • Question

  • Hi,

    I am c# newbie. I double clicked a button and VS created a "private void" for the click event of the button. I have a string on that private void section. I want to pass the string value to another buttons click event. How can I do that? I made a search on the internet but the entries are not really helpful.

    I'll be glad if you post detailed explanation on  the example codes you send considering that I am a newbie.

    Kindest Regards

    Thursday, March 15, 2012 10:21 PM

Answers

  •   Hi telmessosc,

      Presume that you want to deliver string data or other custom structure data between the button event handler,it seems that you only define this variable first before form construct class,and you'd better setting it as static modifier.Then if you are running this specific form next time ,you would see the varible using the same memory space. And every time you enter this form ,this variable is always the same to the previous results of change. The following code snippet is its detailed implementation.

    namespace DeliverDataBetweenButton { public partial class Form1 : Form { static string a = "initialize"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { a = "First modifies"; } private void button2_Click(object sender, EventArgs e) { a = "Second modifies"; } } }

     

    Hope it helps you.

    Sincerely,

    Jason Wang


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, March 16, 2012 2:17 AM
  • Hello,

    you can use a global String variable and initialize it when first button click and after it use this in second button

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace connetivity { public partial class Form3 : Form { public Form3() { InitializeComponent(); } string aa; private void button1_Click(object sender, EventArgs e) { aa = textBox1.Text; } private void button2_Click(object sender, EventArgs e) { label1.Text = aa.ToString(); } } }




    Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights

    Friday, March 16, 2012 7:25 AM

All replies

  •   Hi telmessosc,

      Presume that you want to deliver string data or other custom structure data between the button event handler,it seems that you only define this variable first before form construct class,and you'd better setting it as static modifier.Then if you are running this specific form next time ,you would see the varible using the same memory space. And every time you enter this form ,this variable is always the same to the previous results of change. The following code snippet is its detailed implementation.

    namespace DeliverDataBetweenButton { public partial class Form1 : Form { static string a = "initialize"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { a = "First modifies"; } private void button2_Click(object sender, EventArgs e) { a = "Second modifies"; } } }

     

    Hope it helps you.

    Sincerely,

    Jason Wang


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, March 16, 2012 2:17 AM
  • Hello,

    you can use a global String variable and initialize it when first button click and after it use this in second button

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace connetivity { public partial class Form3 : Form { public Form3() { InitializeComponent(); } string aa; private void button1_Click(object sender, EventArgs e) { aa = textBox1.Text; } private void button2_Click(object sender, EventArgs e) { label1.Text = aa.ToString(); } } }




    Tarun singh Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights

    Friday, March 16, 2012 7:25 AM
  • orichsonic and Tarun00007 thank you both for your help. It works like a charm. 

    Many thanks

    Friday, March 16, 2012 9:05 AM