Hi
I need change variables in form from class
I have event on button.
private void listenClick(object sender, EventArgs e)
{
if (button == false)
{
button = true;
listen.Text = "Close";
listen.BackColor = Color.Red;
textLp.Enabled = false;
textRp.Enabled = false;
textIp.Enabled = false;
textSend.Enabled = true;
}
else
{
button = false;
listen.Text = "Listen";
listen.BackColor = Color.Lime;
textLp.Enabled = true;
textRp.Enabled = true;
textIp.Enabled = true;
textSend.Enabled = false;
}
}
I need change all this variables in class.
Loading

Iftikar HussainRank #423 · 5.9K reputationPosted Aug 16, 2013, 1:14 AM EDT
Not clear about your query. Can you bit explain more?
Regards,
Iftikar
VulpesRank #55 · 29.2K reputationPosted Aug 16, 2013, 1:36 AM EDT
In the Properties Box, change the Modifiers property of the controls you want to access from private to internal.
In your class, get a reference to the Form (say Form1) which contains those controls as follows:
Form1 f1 = Application.OpenForms["Form1"] as Form1;
You can now access the controls like this:
if (f1 != null)
{
f1.listen.Text = "Close";
f1.textRp.Enabled = false;
// etc.
}
If listenClick is an event handler in Form1, you can also fire it from the class without actually clicking the button as follows:
f1.listen.PerformClick();
You'll need to make sure that your class file has the following 'using' directive as it may not be added by default:
using System.Windows.Forms;
Pavol22 reputationPosted Aug 18, 2013, 12:57 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 18, 2013, 10:06 AM EDT
Pavol22 reputationPosted Aug 20, 2013, 3:10 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 20, 2013, 3:25 AM EDT
The bool 'button' variable does not need to be changed.
Pavol22 reputationPosted Aug 20, 2013, 4:10 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 20, 2013, 4:19 AM EDT
f1.listen.BackColor = Color.Blue; // or whatever