How to invoke a form inside another form task - c#

In my Windows Form application I have created 2 Forms. In form 1 when I click button1, a new task will start. Inside the task I have created an instance of the form2 and show form2. I am calling the showData Method of Form2.
//Form1
public event TickHandler Tick;
public EventArgs e = null;
public delegate void TickHandler(int a1, EventArgs e);
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
Form2 form2 = new Form2();
form2.Show();
}
}
//Form2
public void showData(Form1 m)
{
m.Tick += new Form1.TickHandler(test);
}
public void test(int a1,EventArgs e)
{
Task.Factory.StartNew(() =>
{
for (int i = a1; i < 1000; i++)
{
label1.Invoke(new MethodInvoker(delegate { label1.Text = i.ToString(); }));
}
});
}

As kenny suggested i have modified the code. now it running how i am expected.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
Action act1 = (() =>
{
Form2 form2 = new Form2();
form2.StartPosition = FormStartPosition.CenterParent;
form2.Show();
});
this.BeginInvoke(act1);
});
}
}
// FORM2
private void Form2_Load(object sender, EventArgs e)
{
test(1);
}
public void test(int a1)
{
Task.Factory.StartNew(() =>
{
for (int i = a1; i < 1000; i++)
{
label1.Invoke(new MethodInvoker(delegate { label1.Text = i.ToString(); }));
}
});
}
Once again thanks Kenny

Related

How to make a label change its text every some time in C# windows forms

I have a label that I want to update every 5 seconds. It should change from 1921 to 1922 onward till 1992. I have tried using a timer but it gave me an error about being accessed on the wrong thread. The code I used was:
public partial class Form1 : Form
{
int x = 1921;
public Form1()
{
InitializeComponent();
}
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
private void UpdateLabel(object sender, ElapsedEventArgs e)
{
label1.Text = x.ToString();
x += 1;
}
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Elapsed += UpdateLabel;
myTimer.Start();
}
}
Try this:
private void UpdateLabel(object sender, ElapsedEventArgs e)
{
//Invoke makes the UI thread call the delegate.
Invoke((MethodInvoker)delegate {label1.Text = x.ToString(); });
x += 1;
}
try this
private readonly object y = new object();
int x = 1921;
public Form1()
{
InitializeComponent();
}
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
private void UpdateLabel(object sender, ElapsedEventArgs e)
{
Invoke((MethodInvoker)(() => { lock (y) { label1.Text = x.ToString(); x++; } }));
}
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Elapsed += UpdateLabel;
myTimer.Start();
}

winform textBox not updating

i've two winforms form1 and form2 as the following:
public partial class Form1 : Form
{
Form2 frm2;
int count = 0;
public bool fromForm2;
public Form1(bool fromForm2 = false)
{
InitializeComponent();
this.fromForm2 = fromForm2;
MessageBox.Show(fromForm2.ToString());
if (fromForm2 == true) {
test();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}
frm2.Show(this); //Show Form assigning this form as the forms owner
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
public void test ()
{
textBox1.Text = "ABCDFGHIJKLM";
MessageBox.Show(textBox1.Text);
}
}
Form 2
public partial class Form2 : Form
{
Form1 f1;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objj = new Form1(true);
objj = (Form1)Application.OpenForms["Form1"];
objj.fromForm2 = true;
objj = null;
}
}
i want click the button in form2, will run test(), but i've found that, if i use if (fromForm2 == true) { } the if statement, the test() function will not update (redraw?) the text value to display to the textBox1, anyone know what is the reason of this?

Passing a dynamic String from one form to another in c#

I have 2 forms Form1 and Form2.
In Form1 i have a backgroundWorker.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
Form2 frm2 = new Form2();
frm2.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int k = 0;bool b=true;
while (b==true)
{
Thread.Sleep(100);
k++;
backgroundWorker1.ReportProgress(0, "data");
if (k >= 100)
b = false;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
}
}
In Form2 I have one label label2.
How can i display the same content of label1 in form1 on label2 in form 2 dynamically.
Please Help with an example.
You can have an Action that you call when you update the label on Form1
public Action<string> UpdateFormAction {get; set;}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
UpdateForm2(label1.Text);
}
private void UpdateForm2(string text)
{
Action<string> handler = UpdateFormAction;
if (handler != null)
handler(text);
}
Then depending on where you instantiate your forms, you can hook up the Action

A new form in a main form

in C# forms I need code to add a second form to my existing. this is what I've tried:
First form:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
}
And this is Second form:
public class frm2 : Form
{
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}
What am I missing?
EDIT: forget all this for a moment. Even if I do it as suggested down there I get an error when I press the key.
An unhandled exception of type 'System.InvalidCastException' occurred in Project 09.exe
Additional information: Specified cast is not valid.
You could do this:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
this.private void MMForm(object sender, KeyPressEventArgs e)
}
private void MMForm(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(((int)Keys.Escape)))
{
frm2 fM2 = new frm2(); fm2.Height=200; fm2.Width=200; fM2.Show();
}
}
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
}
}
frm2 does not use InitializeComponent() command. so add it to your code.
secondly you try to add frm2 object to itself, so it will not work.
you should use the code belove for your exiting form (and set please its weight from properties, if you will not resize the form.
public class frm2 : Form
{
public frm2()
{
InitializeComponent(); ,
this.Width = 200; this.Height = 200;
}
}
And after a special key if you want to display frm2 :
frm2 secondFrom = new frm2();
frm2.Show(); // frm2.ShowDialog(); works too but they are working differently.
private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
Replace with this:
private void frmMain_Load(object sender, EventArgs e)
{
this.KeyPress += new KeyPressEventHandler(MMForm);
}
Or you can just register to your own KeyPress via designer, directly to MMForm...
And also, it is unclear what you are trying to do here:
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
It should probably look more like this:
public frm2()
{
InitializeComponents();
this.Height = 200;
this.Width = 200;
}
Even if you don't want to InitializeComponents, you should edit your own (this) properties, not a new frm2 properties.
You've had the same problem in frmMain_Load, when you created a new frmMain, and subscribed to it's KeyPress, when really you should've subscribed to your own KeyPress.
Also, you can shorten your MMForm just to beautify, like so:
private void MMForm(object sender, KeyPressEventArgs e)
{
if ((Keys)sender == Keys.Escape)
{
new frm2().Show();
}
}
If you are trying to open frm2 when escape key is pressed on the main form do the following:
public frmMain()
{
InitializeComponent();
this.KeyPress += new KeyPressEventHandler(MMForm);
}
//You don't need to put anything in form load
private void frmMain_Load(object sender, EventArgs e)
{
}
//This is fine
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
In frm2 do:
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
this.Height = 200; this.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}

Close Form when Thread has finished

Everybody.
I've two form as follow by
From1 has a button, when click this Form2 will appear.
From2 has a Progressbar, It's counting and update Progressbar from Maximun value until It has finished, Form2 will close.
This below code of Form2
public delegate void ProgressbarHandler(int value);
public partial class Form2 : Form
{
public event WaitCallback CloseThreadEvent;
private Thread t;
public void OnCloseEvent(ThreadState state)
{
if (CloseThreadEvent != null)
CloseThreadEvent(state);
}
public Form2()
{
InitializeComponent();
progressBar1.Minimum = 0;
progressBar1.Maximum = 20000;
}
private void Form2_Load(object sender, EventArgs e)
{
InitThread();
}
private void InitThread()
{
t = new Thread(new ThreadStart(RunThread));
t.Start();
CloseThreadEvent += new WaitCallback(CloseForm);
Thread tt = new Thread(ThreadObserver);
tt.IsBackground = true;
tt.Start();
}
private void RunThread()
{
for (int i = 0; i < progressBar1.Maximum; i++)
{
progressBar1.Invoke(new ProgressbarHandler(UpdateProgressbar), i);
}
}
private void UpdateProgressbar(int value)
{
progressBar1.Value = value + 1;
}
private void ThreadObserver()
{
while (t.IsAlive)
{
OnCloseEvent(t.ThreadState);
}
}
private void CloseForm(Object state)
{
if ((ThreadState)state == ThreadState.Stopped)
this.Close();
}
}
From my code, It has a "Cross-thread operation not valid" error on
this.Close();
Please give suggestion, How to coding follow by my purpose.
Thank you.
You can only access controls from the thread they were created on. A form is also a control.
Have a look at Control.Invoke.
I use a class similar to this to handle these scenarios:
public static class ControlExtensions
{
public static void Invoke(this Control control, Action action)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(action), null);
}
else
{
action.Invoke();
}
}
}
Then you would be able to call this.Invoke(() => Close()); to close your form.
Pretty simple fix.
this.Invoke(new MethodInvoker(delegate { this.Close(); }));
I don't know why you didn't think of it? Isn't it obvious? :P
Thank you, Everybody
It works!!
public partial class Form2 : Form
{
private Thread tstart, trun;
public Form2()
{
InitializeComponent();
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}
private void Form2_Load(object sender, EventArgs e)
{
tstart = new Thread(InitThread);
tstart.Start();
}
private void InitThread()
{
trun = new Thread(new ThreadStart(RunThread));
trun.Start();
trun.Join();
CloseForm(trun.ThreadState);
}
private void RunThread()
{
for (int i = 0; i < progressBar1.Maximum; i++)
{
Thread.Sleep(10);
progressBar1.Invoke(new MethodInvoker(delegate { progressBar1.Increment(1); }));
}
}
private void CloseForm(Object state)
{
if ((ThreadState)state != ThreadState.Stopped)
return;
else
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(delegate { this.Close(); }), null);
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (tstart.IsAlive)
tstart.Abort();
if (trun.IsAlive)
trun.Abort();
}
}

Categories

Resources