I have a WinForms application with two forms - Form1 and Form2.
I have a loop in Form1 that sends emails.
I would like to send the status messages from the loop in Form1
to a RichTextBox in Form2.
How can I accomplish this?
Any feedback is appreciated.
The solution depends on the relation between the two forms: does Form1 know about Form2, for instance because Form1 created Form2?
class Form1 : Form
{
private Form2 Form2 {get; set;}
private void CreateForm2()
{
this.Form2 = new Form2;
}
private void SendStatusMessage(StatusMessage message)
{
this.Form2.ProcessStatusMessage(message);
}
}
If Form1 does not know about Form2, but Form2 knows about Form1, then you should change Form1 such, that it sends events whenever a Status message is available.
public class EventStatusMessageArgs : EventArgs
{
public StatusMessage Message { get; set; }
}
class Form1 : Form
{
public Event EventHandler<EventStatusMessageArgs> StatusMessageCreated;
protected virtual void OnStatusMessageCreated(StatusMessage statusMessage)
{
var eventHandler = new EventStatusMessageArgs
{
SatusMessage = statusMessage;
};
this.StatusMessageCreated?.Invoke(this, eventHandler);
}
private void CreateStatusMessage(...)
{
StatusMessage statusMessage = ...
this.OnStatusMessageCreated(statusMessage);
}
}
class Form2 : Form
{
// Form2 somehow knows about existence Form1
public Form1 Form1 {get; set;}
public void OnFormLoading (object sender, ...)
{
// subscribe to events from Form1:
this.Form1.StatusMessageCreated += this.OnForm1CreatedStatusMessage;
}
public void OnForm1CreatedStatusMessage(object sender, EventStatusMessageArgs args)
{
StatusMessage createdStatusMessage = args.StatusMessage;
this.ProcessCreatedStatusMessage(createdStatusMessage);
}
}
Third scenario: Form1 and Form2 don't know about each others existence. Luckily mainForm who created Form1 and Form2 does:
class MainForm : Form
{
private readonly Form1 form1;
private readonly Form2 form2;
public MainForm()
{
InitializeComponents();
this.form1 = new Form1() {...};
this.form2 = new Form2() {...};
// make sure that the StatusMessage events from form1 go to form2:
this.form1.StatusMessageCreated += this.form2.OnForm1CreatedStatusMessage;
}
}
By the way: always unsubscribe from the events when the item is Disposed.
Related
In Form1 I have one DataGridView and multiple textboxes. When I click A button in Form2 I need to save the data from DataGridView and multiple textboxes to Database. How to Implement in C sharp Windows Application
Form1 Button Click event. I opened Form2
public sealed partial class form1 : Form
{
private static form1 instance = null;
public static form1 Instance
{
get
{
if (instance == null)
{
instance = new form1();
}
return instance;
}
}
private void button1_Click(object sender, EventArgs e)
{
textbox2.Text=100;
form2 CO = new form2();
CO.Show();
}
}
I want to attach textboxes data and Datagridview content to
object SO and Call InsertSale function .textboxes and datagridview are in form1
This is Button Click Event in Form 2
private void button1_Click(object sender, EventArgs e)
{
clsSale SO = new clsSale();
SO.Totamount = Convert.ToDecimal(form1.Instance.textBox2.Text);
SO.InserSale(SO);
}
If Form2 wants to access the Form1 properties.
Pass ParentForm instance to the ChildForm constructor. Add a public method in the parent form to update its properties from child form.
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void SetTextBoxValue(string val)
{
this.textBox1.Text = val;
}
private void CreateForm2()
{
var form2 = new Form2(this);
form2.Show();
}
}
public partial class Form2: Form
{
private Form1 form1;
public Form2(Form1 frm1)
{
InitializeComponent();
form1= frm1;
form1.SetTextBoxValue("Value from Form2");
}
}
Create a global class, such as Global.cs, in the project. Then declare the following variables:
public static Form1 frm1
public static Form2 frm2
Declare a variable of the Form class - Form frm1 or Form frm2 etc.
Now access the variables from any form as follows:
Global.frm1 = new Form1() // - for the Home Form1
Global.frm1.ShowDialog();
Global.frm2 = new Form2() // - for the Home Form1
Global.frm2.ShowDialog();
If you want to access a control in any form, just extend them as follows:
frm1.txtBox1.Text
frm2.button1.Click() // etc.
I wanted to pass data to form1 from another class but I fail to do so.
What I have tried is
class other_class {
public a_function() {
Form1 form1 = new form1();
form1.something("Lorem Ipsum");
}
}
While on the form1 is simply
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
other_class a = new other_class();
a.a_function();
}
public void something(string str) {
//Pass to Another generic function
another_function(str);
}
public void another_function(string str) {
//Do update Textbox and RichtextBox Fields
}
}
However, it seemed that the functions within form1 is not callable from another class. What's the best fix or an alternative for this?
I solved my own question thanks to: https://social.msdn.microsoft.com/Forums/vstudio/en...
class other_class {
public Form1 form;
public other_class(Form1 frm) {
form = frm;
}
public void a_function() {
form.something("Lorem Ipsum");
}
}
and
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
other_class a = new other_class(this);
a.a_function();
}
I realized that I don't have to reinitiate the form1 since it's running already
Following code will be helpful to you,
other_class.cs :
class other_class
{
//Create a constructor in order to initialise the class
public other_class()
{
}
public void a_function()
{
Form1 form1 = new Form1();
form1.something("Lorem Ipsum");
form1.Show();
}
}
Form1.cs :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void something(string str)
{
another_function(str);
}
public void another_function(string str)
{
//Added a label to Form1.cs
//Set its text here
label1.Text = str;
}
}
and I have created another form Form2 in order to initialize other_class and call other_class's a_function() method from there as follows,
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//Added a button to Form2.cs
//Set a button click event
private void button1_Click(object sender, EventArgs e)
{
other_class obj = new other_class();
obj.a_function();
}
}
I have 2 forms. Form 1 which shows a ListView and Form 2 a button called button1.
What I'm trying to do is when the button on form 2 is clicked. I want it to fill the Listview on form1.
The listview has 3 columns;
Flavour
Quantity
Sub-Total
When the button1 is pressed, it should show Vanilla, 1, £1.00 in the listview on form1.
I'm able to do this if the listview is on the same form as the button, but not if it's on different forms.
Form1
public partial class form1: Form
{
public form1()
{
InitializeComponent();
}
Form2
public partial class form2: Form
{
public form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("Vanilla");
lvi.SubItems.Add("1");
lvi.SubItems.Add("£1.00");
listView1.Items.Add(lvi);
}
Create a reference of form1 in form2 like this:
class Program {
static void Main() {
var form1 = new Form1();
var form2 = new Form2(form1);
}
}
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void DoStuff(ListViewItem lvi) {
// TODO: Stuff
}
}
public partial class Form2: Form
{
private Form1 _form1;
public form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("Vanilla");
lvi.SubItems.Add("1");
lvi.SubItems.Add("£1.00");
listView1.Items.Add(lvi);
_form1.DoStuff(lvi);
}
}
How do i to access or update the controls of non-parent or non-mdi form from another form in C#.
IF yes, the I have a form - Form1 and button on it. When i click button, new form - Form**2 opens which is not child form.What i want to try is when i do some activity on **Form2, some information should be displayed on status-bar of Form1.
If sample available it will be good.
Thanks,
You could setup a event in Form2 to notify others that certain action is triggered:
public partial class Form2 : Form
{
public class StatusChangedArgs : EventArgs
{
// Put useful information here which would be retrieved from Form1
}
public event EventHandler<StatusChangedArgs> StatusChanged;
private void OnStatusChanged()
{
var handler = StatusChanged;
if (handler != null)
handler(this, new StatusChangedArgs());
}
// Call OnStatusChanged in other Form2's functions, e.g. button click ...
}
Then when Form1 creates Form2, you can register Form1 as a listener to the Form2's event:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.StatusChanged += form2_StatusChanged;
}
void form2_StatusChanged(object sender, Form2.StatusChangedArgs e)
{
// Update Form1's status bar here
}
}
You can use a mediator pattern basically you have to create both form sharing the same mediator object.
Update sample code
public class SimpleMediator{
public Form1 MainForm {get; set;}
public void DisplayStatus(string message){
MainForm.StatusBar.Text = message;
}
}
public class Form2 : Form{
public SimpleMediator Mediator {get; set;}
//...
}
then when you open Form2
public void OpenSubForm(){
var mediator = new Mediator{
MainForm = this;
};
var f2 = new Form2(){
Mediator = mediator;
};
f2.Show();
}
Now you can access Mediator.DisplayStatus() method inside Form2
Edit the Form2's constructor and add a new the Form1's reference:
public Form2(Form1 form1Para)
{
InitializeComponent();
// This is the Form1 reference
Form1 form1 = form1Para;
}
Then edit Form1, where you have the method opening the new Form2:
// Give Form1 in the constructor (this <- the Form1)
Form2 form2 = new Form2(this);
form2.Show();
I would suggest that Form2 raises an event that Form1 handles. The event args can contain the data or Form1 can get the data from a property of Form2.
http://jmcilhinney.blogspot.com.au/2013/10/managing-data-among-multiple-forms-part.html
I need help, what I'm trying to do is make it that as soon as the button is clicked on form2, it sets the variable for the flash shockwave object on form1.
form1:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.Show();
}
}
}
form2:
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
axShockwaveFlash1.SetVariable("_level1.shellContainer.ENGINE.my_room_movieclips.block_mc._x", "-5000");
}
}
}
You can use a static variable like this:
public partial class Form1 : Form
{
VARIABLETYPE axShockwaveFlash1;
...
}
This will allow you to access the shockwave object from any object, but is global at this point and potentially dangerous.
Alternatively, you could use the ParentForm
Form1.cs
var form2 = new Form2();
form2.Show(this);
Form2.cs
(ParentForm as Form1).axShockwaveFlash1....