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();
}
}
Related
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.
Hello I hope you can help me, I've been trying like more than 10 days to solve this problem.
I have a Form Application with 1 usercontrol and 1 Class and need use my class instance created in Form1 inside the UserControl1. (With Form1 to Form2 it's works fine)
Class CMensaje:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public class CMensajes
{
private string mensaje;
public CMensajes()
{
}
public string Mensaje { get => mensaje; set => mensaje = value; }
}
}
UserControl1:
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class UserControl1 : UserControl
{
CMensajes mensajito;
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = mensa;
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = mensajito.Mensaje;
}
}
}
Form1
namespace WindowsFormsAppInstanciarClaseEnControl
{
public partial class Form1 : Form
{
CMensajes mensajito = new CMensajes();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
mensajito.Mensaje = textBox1.Text;
UserControl1 usercontrol1 = new UserControl1(mensajito);
}
}
}
The problem is that it's works but intermediately I start to get
The variable mensajito is either undeclared or was never assigned. When I open Form1 Design. When I do the same code with Form1 to Form2 everything perfect!!
Really I need to pass a instance of my class serialport but is the same Here I write only a test code to understand what could I do?
Thanks.
Error Screen when I try to open Form1.designer.cs
For me you should do this inside the User Control constructor:
public UserControl1(CMensajes mensa)
{
InitializeComponent();
mensajito = new CMensajes();
mensajito = mensa;
}
Haven't got a machine in front of me to test this but at the risk of sounding silly, try adding a default constructor to your user control.
public UserControl1()
{
InitializeComponent();
}
Or try moving the instantiation of your class inside the Form constructor.
I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
Here is my code.
Main class
namespace winapp
{
public partial class Main_Form : Form
{
//Here is textfield1
public Main_Form()
{
InitializeComponent();
}
}
the other class
namespace winapp
{
public partial class second: Form
{
static Main_Form main_form = new Main_Form();
string sss = "12345";
public second()
{
InitializeComponent();
}
private void but_Click(object sender, EventArgs e)
{
//I want to change the text from here
main_form.textbox1.text = this.sss;
}
Like above, I want to change the text of a textField in the main class with second class.
But my the text of textfiled does not change.
Regurd if anyone can help me.
The issue is that you're creating a brand new Main_Form. Instead you can pass your instance of Main_Form to second:
public partial class Second : Form
{
private readonly Main_Form _mainForm;
public Second(Main_Form mainForm) {
_mainForm = mainForm;
}
}
I would then create a property on MainForm to access the textbox:
public ... Main_Form : Form
{
public string MyText {
get { return textbox1.Text; }
set { textbox1.Text = value; }
}
}
I'm not sure where you create your second form but it would now look like this as long as you're creating it in Main_Form:
Second secondForm = new Second(this);
And then second can access Main_Form's MyText property which will set the textbox.
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
int port; // I declared a variable and I wanna use this in another form like
}
// ------------------------------------------------------- //
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SagTikMenuOlustur();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
MessageBox.Show("{0} server is online ",port); //How to I declare ????
}
}
Set the field as public
or
Create property for that field.
This is the way you can use
Refer this link: How to access a form control for another form?
Best thing would be to create a property for it.
Try this
public partial class Form3 : Form
{
int _port;
public int Port
{
get { return _port; }
set { _port = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void menu1_Click(object sender, EventArgs e)
{
Form2 frq = new Form2();
frq.Show();
Form3 frm3 = new Form3();
frm3.Port = 8080;
MessageBox.Show("{0} server is online ", frm3.Port);
}
}
You have to change port to public.
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public int port; <<== Change to public
or public int port {get;set;}