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
Related
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
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?
I'm new at C#.
I want to add '#' to HALLO (in the textBox) each time when you hover your mouse over the button.
This is what i have:
public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
if (Q.Length > 20)
{
tB1.Clear();
}
lBkarakters.Text = Convert.ToString(tB1.Text.Length);
}
}
}
It does add the '#', but HALLO is gone.
Initialize your textbox somewhere (I'd recommend on the Load event handler):
tB1.Text = "HALLO";
Register an event handler for the MouseHover event on the button:
this.yourButton.MouseHover += new System.EventHandler(this.yourButton_MouseHover);
// ...
private void yourButton_MouseHover(object sender, System.EventArgs e)
{
tB1.Text += "#";
}
public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
}
}
or
public partial class Form1 : Form
{
string Q = "HALLO";
string hashtag = "#";
public Form1()
{
InitializeComponent();
tB1.Text = Q;
}
private void bT1_MouseHover(object sender, EventArgs e)
{
tB1.Text += hashtag;
}
}
Make sure you're event is registered:
I am new to c#. I have the following in my project in windows forms:
Form1 with button and textbox.
User control with a buttton.
Form2 with button and textBox.
As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.
In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.
My question is: How can I send text from form2 to textbox in form1 via user control1?
I just need to know some steps to follow or some code if it is possible to achieve this.
Please help me. Thank you
Form1:
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Controls.Add(UC1); //add a userControl
UC1.Visible = true;
}
}
User Control1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// I want to send text to form1 when this button is clicked
}
}
You can do it by trigger event and event handler.
In Form2,
public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
public event SendTextF2 UISendTextHandlerF2;
public Form2(TextBox s)
{/*unchange*/}
private void button1_Click(object sender, EventArgs e)
{
if(UISendTextHandlerF2!=null)
UISendTextHandlerF2(textBox1.Text);
}
}
In UserControl1,
//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
//New
public event SendTextUC UISendTextHandlerUC;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
//Add event handler
frm2.UISendTextHandlerF2 += SendText123;
}
//Event Handler for the event trigger in Form2
void SendText123(string YourStringFromTextBox)
{
//Trigger Event
if(UISendTextHandlerUC!=null)
UISendTextHandlerUC(YourStringFromTextBox);
}
}
In Form1,
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
//Add event handler
UC1.UISendTextHandlerUC += FinallyWeGetTheString;
}
Controls.Add(UC1);
UC1.Visible = true;
}
//New
void FinallyWeGetTheString(string YourStringFromTextBox)
{
textBox1.Text = YouStringFromTextBox;
}
}
Add those line to your code:
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
}
Controls.Add(UC1);
UC1.Visible = true;
}
}
User Control:
public partial class UserControl1 : UserControl
{
TextBox r;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
}
}
And Form2:
public partial class Form2 : Form
{
TextBox s;
public Form2(TextBox s)
{
InitializeComponent();
this.s = s;
}
private void button1_Click(object sender, EventArgs e)
{
String str = textBox1.Text;
s.Text = str;
}
}
How can I passing values from a Form to another one directly? A receiver Form will be showing itself on screen and listening passing values which will be sending from a main form.
I know a way to do that with delegate and event but mine is not my desired one.
I need that with opposite way. Below is what I can do those code lines. This able to do only Form2 passes value to Form1 (main form). I need this approach's opposite. So, Form1 will be sender Form2 will be receiver, and transmit in real-time while they are showing itself on the screen.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.IdentityUpdated += new Form2.IdentityUpdateHandler(Form2_ButtonClicked);
f.Show();
}
private void Form2_ButtonClicked(object sender, IdentityUpdateEventArgs e)
{
textBox1.Text = e.FirstName;
}
}
public partial class Form2 : Form
{
public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string sFirstName = txtFirstName.Text;
IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sFirstName);
IdentityUpdated(this, args);
}
}
public class IdentityUpdateEventArgs : System.EventArgs
{
private string mFirstName;
public IdentityUpdateEventArgs(string sFirstName)
{
this.mFirstName = sFirstName;
}
public string FirstName
{
get { return mFirstName; }
}
}
try this way
public partial class Form1 : Form
{
public delegate void IdentityUpdateHandler(object sender, EventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
IdentityUpdated(this, new EventArgs());
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.IdentityUpdated += Form1OnIdentityUpdated;
}
private void Form1OnIdentityUpdated(object sender, EventArgs eventArgs)
{
MessageBox.Show("received");
}
}