Form1 has a label.
Form2 has an if statement inside a while loop. When the if statement evaluates as true I need to change the BackColor of a label on Form1.
Thanks in advance for your help.
Class MyClass
{
public delegate void ChangeColorDelegate();
public event ChangeColorDelegate ChangeColor;
While(true)
if (something == true)
(HAVING TROUBLE HERE)
if (ChangeColor != null)
ChangeColor();
}
Class MyOtherClass
{
MyClass test = new MyClass();
test.ChangeColor += (THIS IS WHERE I AM HAVING DIFFICULTY)
Danatas GerviPosted Sep 29, 2009, 5:09 AM
Possible reason is that you gave the delegate of one instanse of from, but show other. Form – this is simple class, it may be invisible, but existing, if you keep the reference to it…
Take this code:
namespace ChangeLabelBackground
{
public partial class Form1 : Form
{
Diagnose m_DiagForm;
public delegate void ChangeColorDelegate();
public event ChangeColorDelegate ChangeColor;
public Form1()
{
InitializeComponent();
m_DiagForm = new Diagnose();
ChangeColor += new ChangeColorDelegate(m_DiagForm.ChangeColor);
m_DiagForm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
if (ChangeColor != null)
{
ChangeColor();
}
}
}
}
And, Diagnose form (I have added it to project with one label on it)
namespace ChangeLabelBackground
{
public partial class Diagnose : Form
{
public Diagnose()
{
InitializeComponent();
}
public void ChangeColor()
{
//id2_7.BackColor = Color.Green;
label1.BackColor = Color.Green;
}
}
}
kylePosted Sep 29, 2009, 4:01 PM
namespace ChangeLabelBackground
{
public partial class Form1 : Form
{
Diagnose m_DiagForm; XXXXXXX
public delegate void ChangeColorDelegate();
public event ChangeColorDelegate ChangeColor;
public Form1()
{
InitializeComponent();
m_DiagForm = new Diagnose();XXXXXXXXX
ChangeColor += new ChangeColorDelegate(m_DiagForm.ChangeColor);
m_DiagForm.Show();
Why did you declare m_DiagForm in 2 parts? What would the difference be if you wrote:
Diagnose m_DiagForm = new Diagnose(); in the Form1 constructor instead of breaking it into 2 parts as you did above?
kylePosted Sep 29, 2009, 7:11 AM
Thanks again for your help. You have been quite helpful.
kylePosted Sep 24, 2009, 9:02 PM
My code is executing correctly now. My ChangeColor metod is being called from the working form (class) but the color is not changing.
The working form with the while loop and if statements:
public delegate void ChangeColorDelegate();
public event ChangeColorDelegate ChangeColor;
ChangeColor += new ChangeColorDelegate(diagnostics.ChangeColor);
if (variable == 1)
{
ChangeColor();
}
The other form with the labels:
{
id2_7.BackColor = Color.Green;
}
I set a breakpoint at this method and the statement inside the method and it is being called but the label id2_7 is not changing color. I added a statement in the variable id2_7.Refresh() but this was not effective.
Again, thank you for your time and help.
Danatas GerviPosted Sep 24, 2009, 9:09 AM
No. This is not correct.
To subscribe to event you must to call and execute the code of subscription (test.ChangeColor += HandlerOfChangeColor;)
This can be in constructor, additional function, but before you use this event.
Without handler (instance of delegate, "pointer to function" ) event == null.
kylePosted Sep 24, 2009, 8:02 AM
Danatas GerviPosted Sep 24, 2009, 7:42 AM
Ok, ok….
First – put a break point at
test.ChangeColor += HandlerOfChangeColor;
row…
This row must be executed before you will try to rise event.
kylePosted Sep 23, 2009, 12:02 PM
Danatas GerviPosted Sep 22, 2009, 3:22 AM
Your still need to subscribe to event an "event handler".
Like:
MyClass test = new MyClass();
test.ChangeColor += HandlerOfChangeColor;
And, pay attention – the instance of "event riser" - MyClass test – should be existing all time, should be "class level variable"
Example:
{test = new MyClass();
test.ChangeColor += HandlerOfChangeColor;
}}kylePosted Sep 21, 2009, 7:56 PM
My code stops here:
Danatas GerviPosted Sep 21, 2009, 3:04 AM
Replace "(THIS IS WHERE I AM HAVING DIFFICULTY)"
By method name, with any name, but with same returning type and number of parameters, for your case it will be
private void HandlerOfChangeColor() // same as your delegate!
{
Label1.Color = ………;
}
This method will be called each time when
code will executed.Tip – use standard delegate EventHandler for your events.