T
traafat
Hello guys,
i am having a problem using Delegates, i am pretty new to C# , i am
starting to fall in love with it its really good language (coming from
Java and Delphi),
now i am working on an application where i have a label on a form and
then i have a separate class in separate CS file (ie., Form1.cs and
myClass.cs)
what i am trying to do : i wanted to update label1.Text every tick of
a timer with the format 00:00 next tick 00:01, then next tick 00:02
and so on
so in my Class there is a timer(System.Timers.Timer/
System.Threading.Timer/System.Windows.Forms.Timer) and i am trying to
update the label1 in form1 using the following :
Form1 :
-----------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void setlabeltext(Label lbl, String strValue)
{
lbl.Text = strValue;
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 myClass = new Class1();
myClass.startCall();
}
}
}
myClass:
--------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Timers = System.Windows.Forms.Timer;
namespace WindowsApplication2
{
class Class1
{
Form1 form1 = new Form1();
Timers timoz = new Timers();
public void timoz_Tick(object sender, EventArgs e)
{
ThreadSafeSetLable(form1.label1,
DateTime.Now.Subtract(DateTime.Now).ToString());
}
private void ThreadSafeSetLable(Label lbl, string value)
{
if (form1.InvokeRequired)
{
form1.Invoke(new SetString(form1.setlabeltext), new
object[] { lbl, value });
}
else
{
form1.setlabeltext(lbl, value);
}
}
private delegate void SetString(Label txtBox, string
strValue);
public void startCall()
{
timoz.Interval = 1000;
timoz.Start();
timoz.Tick += new EventHandler(timoz_Tick);
}
}
}
this is not working i dont know why , it compiles no problem , in the
debugging the label1.text is changing to 00:00:00 but runtime i dont
see that , plus the form1.InvokeRequired never gets to "true" , i do
realize that i am missing something but i dont know what , can i have
help on that ? thanx
i am having a problem using Delegates, i am pretty new to C# , i am
starting to fall in love with it its really good language (coming from
Java and Delphi),
now i am working on an application where i have a label on a form and
then i have a separate class in separate CS file (ie., Form1.cs and
myClass.cs)
what i am trying to do : i wanted to update label1.Text every tick of
a timer with the format 00:00 next tick 00:01, then next tick 00:02
and so on
so in my Class there is a timer(System.Timers.Timer/
System.Threading.Timer/System.Windows.Forms.Timer) and i am trying to
update the label1 in form1 using the following :
Form1 :
-----------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void setlabeltext(Label lbl, String strValue)
{
lbl.Text = strValue;
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 myClass = new Class1();
myClass.startCall();
}
}
}
myClass:
--------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Timers = System.Windows.Forms.Timer;
namespace WindowsApplication2
{
class Class1
{
Form1 form1 = new Form1();
Timers timoz = new Timers();
public void timoz_Tick(object sender, EventArgs e)
{
ThreadSafeSetLable(form1.label1,
DateTime.Now.Subtract(DateTime.Now).ToString());
}
private void ThreadSafeSetLable(Label lbl, string value)
{
if (form1.InvokeRequired)
{
form1.Invoke(new SetString(form1.setlabeltext), new
object[] { lbl, value });
}
else
{
form1.setlabeltext(lbl, value);
}
}
private delegate void SetString(Label txtBox, string
strValue);
public void startCall()
{
timoz.Interval = 1000;
timoz.Start();
timoz.Tick += new EventHandler(timoz_Tick);
}
}
}
this is not working i dont know why , it compiles no problem , in the
debugging the label1.text is changing to 00:00:00 but runtime i dont
see that , plus the form1.InvokeRequired never gets to "true" , i do
realize that i am missing something but i dont know what , can i have
help on that ? thanx