I know how to switch form to form, but my problem is that when switching from Form B to Form A. It always create a new Instance of Form A.
How can I avoid this behaviour?
What you are looking for is called Singleton.
For a very basic approach you can take this:
public partial class Form1 : Form
{
public static Form1 Instance { get; set; } //Create an Instance Object of your Window
public Form1()
{
InitializeComponent();
}
//Your call to open the Window
private void OpenForm2()
{
if (Form2.Instance == null)//Check if Form2 has already been created
{
//if not: go create a new one !
Form2.Instance = new Form2();
}
//Instance of Form2 is already created => open that one
Form2.Instance.Show();
}
}
public partial class Form2 : Form
{
public static Form2 Instance { get; set; }
public Form2()
{
InitializeComponent();
}
}
Related
Can I make like
Form1 form1 = new Form1();
Without losing data
I mean like there is 2 textboxes which is filled and this form was closed and opened another Form2
And I want access data from this 2 textboxes but when I made
Form1 form1 = new Form1();
I'm losing data (like is empty)
// LogInForm id returner
public partial class LogInForm : Form
{
public int IdReturner()
{
var idFinder = (from x in context.Users where x.EmailAddress == txtBoxEmail.Text select x.Id).FirstOrDefault();
LoginViewer.UserReturner(Convert.ToString(idFinder));
return idFinder;
}
}
// Here is where I need store id
public partial class ProductAddToDatabase : UserControl
{
private void btnSave_Click_1(object sender, EventArgs e)
{
model.ProductName = txtBoxProductName.Text;
model.ProductPrice = Convert.ToDouble(txtBoxProductPrice.Text);
model.ProductAmount = Convert.ToInt32(txtBoxProductQuantity.Text);
var id = loginForm.IdReturner();
using (var context = new ProductContext())
{
context.Products.Add(model);
context.SaveChanges();
MessageBox.Show("Product added sucesfully "+ id, "Information", MessageBoxButtons.OK);
Clear();
}
}
}```
new Form1();, creates a new instance of type Form1 it's not returning an existing one.
If you are trying to access information from an existing Form1 in Form2 you would want to cache the instance of Form1, you are trying to access, somewhere e.g.:
// Keep in mind that this is just an example!
public class Form1 : Form
{
public Form1()
{
Instance = this;
}
// trying to access the text in this textbox from Form2
public TextBox textbox1;
public static Form1 Instance;
}
public class Form2 : Form
{
public string TextInForm1Textbox1
{
get
{
return Form1.Instance.textbox1.Text;
}
}
}
well, before you open new forms you need persist your data in another variable like this:
var form1 = new Form();
var form1Data = form1;
form1 = new Form();
this is not a good practice and your question is not well structured, but basically you would need to do this.
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'm trying to save a pointer to Form1 in my Painter class. My attempt here:
class Painter {
private Form _form;
public void setFormPtr( Form form ) {
_form = form;
}
}
public partial class Form1 : Form {
Painter painter;
public Form1() {
InitializeComponent();
painter.setFormPtr( this );
}
}
but it doesn't work (it says NullPointerException near this). What's wrong with this code? I really need a pointer to Form1 in my Painter class. How can I get it?
You need to create a painter with painter = new Painter() first.
But I would add a constructor to the Painter class accepting a form as parameter
public class Painter
{
private readonly Form _form;
public Painter(Form form)
{
_form = form;
}
}
and then create the painter in the Form1 constructor like this:
public Form1()
{
InitializeComponent();
painter = new Painter(this);
}
This ensures that the form is assigned in the painter. There is no setFormPtr method that you can forget to call.
I have an array of strings in Form1 and I want to display it's items in a treeview of Form2. How can I use Form1's variables in Form2? What would be the simplest solution here?
You could pass it thru the constructor.
PSEUDO
public partial class Form1 : Form
{
private string[] _myArray;
private Form2 _form2;
public Form1()
{
_myArray = new string[3];
_myArray[0] = "Hi";
_myArray[1] = "There";
_myArray[2] = "Test";
_form2 = new Form2(_myArray);
_form2.Show();
}
}
public partial class Form2 : Form
{
private string[] _myArrayOfForm2;
public Form2(string[] myArr)
{
_myArrayOfForm2 = myArr;
BuildTree();
}
}
The most flexible yet easy way to do this that comes to my mind would be to set the variables you need in Form1 (in this case the string array) as public and then pass the entire Form as an argument in the constuctor, like this:
public partial class Form2 : Form
public Form2(Form1 _form) {
// code goes here
}
}
I am trying to return some data from Form2 to Form1, everything seems fine, I got the data and so, but, when I try to pass my data to a textbox, it doesn't changes the text. Only if I open a new instance of Form1, on Form2 it works. Why this happen? Can't I send the text to the old instance?
I'm using this code;
Form1 (Main Form)
public void updateText(string data)
{
MessageBox.Show(data);
txtGood.Text = data;
}
Form2 SecondaryForm = new Form2();
SecondaryForm.ShowDialog();
Form2 (Second Form with user data)
Form1 MainForm = new Form1();
MainForm.updateText(data);
MainForm.ShowDialog();
this.Close();
So, my question is, how can I pass the data values to the old instance of the main form? without having to create a new instance and show a new instance. Is there a way to do this?
This is because you're creating a instance of Form1 in your Form2 code. What you want to do is setup Form2's parentForm to be the instance of the Form1 that created it.
public partial class Form1 : Form
{
public void CreateForm2()
{
Form2 form2 = new Form2(this);
form2.ShowDialog();
}
public string MyTextboxText
{
get { return txtMyTextbox.Text; }
set { txtMyTextbox.Text = value; }
}
}
public partial class Form2 : Form
{
private Form1 parentForm;
public Form2(Form1 parentForm)
{
this.parentForm = parentForm;
}
public void myButtonClick()
{
parentForm.MyTextboxText = "Hello";
}
}
This code is just an example, probably wont compile as-is.
What you can do is pass the reference of MainForm(Form1) to second Form(Form2). Then instead of creating MainForm again use the reference to update the textbox.
//pass reference to form2
Form2 SecondaryForm = new Form2(mainForm);
SecondaryForm.ShowDialog();
//in the constructor of Form2 save the reference of Form1
Form1 form1 = null
Form2(Form1 mainForm)
{
form1 = mainForm;
}
//then instead of creating a new MainForm again just use reference of Form1
form1.updateText(data);
this.Close()
main form:
private void Button2_Click(object sender, EventArgs e) {
frmCustomersRecord rec = new frmCustomersRecord(this);
rec.ShowDialog();
rec.GetData();
}
child form:
public partial class frmCustomersRecord : Form
{
public frmCustomersRecord()
{
//blank contructor (Instance of an class)
}
frmCustomerDetails cd;
public frmCustomersRecord(frmCustomerDetails parentForm) : this()
{
this.cd = parentForm;
}
//call the methods using child form object
}