Refreshing/Reload From form 1 to form 2 - c#

private void Form1_Load(object sender, EventArgs e)
{
f2 = new Form2();
f2.Show();
}
I have that code on my Form1. So that will automatically run Form2_Load. Is it possible that when I click a button on Form1. Form2_Load will run again without having to close Form2.

In response to your comment:
When a button on Form1 is clicked it will send a message update to form2. And a textbox in Form2 will have the message.
(First of all, this isn't the Observer Pattern. But that's ok, it doesn't need to be.)
First, Form1 needs to retain its reference to the instance of Form2:
public class Form1
{
private Form2 Form2Instance { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
Form2Instance = new Form2();
Form2Instance.Show();
}
// the rest of your code
}
Now other code on Form1 can reference that instance. This is because you don't want to actually reload all of Form2 any time anything changes. You just want to "send a message update to form2".
Now, how will Form2 receive that message? It can expose a method. Something like this:
public class Form2
{
public void Update(string newValue)
{
someTextBox.Text = newValue;
}
// the rest of the code
}
Now code on Form1 can call that method on the instance of Form2 that it references. So when you click a button on Form1, you might do this:
Form2Instance.Update("some value");
This would "send a message" to the instance of Form2.

Related

Detect when a child form is closed

I have he following Form:
An Initialize Function that is called when the Form1 is created.
A button that opens another Form (Form2)
What I need is to call Initialize() not only when Form1 is created, but whenever Form2 is closed, since Form2 might have modified some stuff that makes Initialize need to be called again.
How can I detect when the form2 is closed?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Initialize();
}
void Initialize()
{
// Read a config file and initialize some stuff
}
// Clicking this button will open a Form2
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2().Show();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
// some stuff that Form2 does which involves modifying the config file
}
}
You just need to add an event handler for the FormClosing event, this handler could be in your first form class and here you can call every internal method of that class
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.FormClosing += childClosing;
form2.Show();
}
private void childClosing(object sender, FormClosingEventArgs e)
{
Initialize();
....
}
In addition to Steve's excellent answer, you may consider displaying Form2 as a modal dialog. This would mean code execution in Form1 STOPS until Form2 is dismissed. This may or may not work well with your application, we have no idea what it does.
At any rate, you'd use ShowDialog() instead of Show() like this:
// Clicking this button will open a Form2
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2().ShowDialog(); // <-- code STOPS here until form2 is closed
Initialize(); // form2 was closed, update everything
}

Modifying textbox value after selection on second form?

In FORM1, I have a textbox that has placeholder text. On click, a second form (FORM2) will pop up with a list of items to choose from. They choose their selection and press on SELECT and FORM2 closes and FORM1's textbox will populate with their selection from FORM2.
Here's what I have:
FORM1 -
// Variables
public string ID{ get; set; }
private void openF2_Click(object sender, EventArgs e)
{
FORM2 f2 = new FORM2();
f2.Show();
// updateText(); <-- when called through here, ID is still empty.
}
public void updateText()
{
textBox1.Text = ID;
// textBox1.Refresh(); <-- I thought this would work
}
// Check if it did change
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.BackColor = Color.White;
}
FORM2 -
private void btnSelect_Click(object sender, EventArgs e)
{
FORM1 f1= new FORM1();
f1.ID = listOfChoices.SelectedItems[0].SubItems[0].ToString();
f1.updateText();
this.Close();
}
When I walk-through the code, I see that ID is being set correctly and when FORM2 calls "updateText()" it sets the textbox1 value to ID and the textChanged function also runs, yet on the GUI, it's still empty.
The problem is that you're creating a new instance of Form1 from Form2 and setting the property on it, but as soon as your Form2 goes away, so does the instance that it created.
In your case, the easiest way to solve this problem is to show your Form2 as a dialog - which means all execution on Form1 pauses while it's active. Then, when Form2 closes, you can still access the properties of it from Form1 because execution on Form1 picks up again right where it left off. This means that if we create a public property on Form2 and set it to the value that the user chooses, we can access it later.
Here's one way to do it:
Form2
Let's create a public property called ChosenItem and we'll set it to the item that the user chooses:
public partial class Form2 : Form
{
public string ChosenItem = "Some default text";
private void btnSelect_Click(object sender, EventArgs e)
{
ChosenItem = listOfChoices.SelectedItems[0].SubItems[0].ToString();
this.Close();
}
// Rest of form code omitted...
}
Form1
Launch Form2 as a dialog and then set our value from the form after it closes:
public partial class Form1 : Form
{
private void openF2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
textBox1.Text = f2.ChosenItem;
}
// Rest of form code omitted...
}

Remove last item in datagridview with another form C# [duplicate]

I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.
So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2
Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);
But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.
Any ideas?
Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice.
public Form1(String customItem)
{
InitializeComponent();
this.myListBox.Items.Add(customItem);
}
// In the original form's code:
Form1 frm = new Form1(this.textBox.Text);
Let's assume Form1 calls Form2. Please look at the code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
frm.VisibleChanged += formVisibleChanged;
}
private void formVisibleChanged(object sender, EventArgs e)
{
Form2 frm = (Form2)sender;
if (!frm.Visible)
{
this.listBox1.Items.Add(frm.ReturnText);
frm.Dispose();
}
}
}
Form2:
public partial class Form2 : Form
{
public string ReturnText { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.textBox1.Text;
this.Visible = false;
}
}
The answer is to declare public property on Form2 and when form gets hidden. Access the same instance and retrieve the value.
Below code working perfect on my machine.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.listBox1.Items.Add(textBox1.Text );//ListBox1 : Modifier property made public
f1.ShowDialog();
}
Ok, If you are Calling Sequence is like, Form1->Form2 and Form2 updates the value of Form1 then you have to use ParentForm() or Delegate to update the previous form.
Form1 frm = new Form1();
frm is now a new instance of class Form1.
frm does not refer to the original instance of Form1 that was displayed to the user.
One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.
Please avoid the concept of making any public members like you said
>>i have done is make modifier of listbox to public and then in form2 in button code<<
this is not a good practice,on the other hand the good one is in Brad Christie's Post,I hope you got it.
This code will be inside the form containing myListBox probably inside a button click handler.
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.myListBox.Items.Add(frm2.myTextBox.Text);
frm2.Dispose();

How to close a form and come back to mainform

My question seems very easy but it's actually quite tricky. I search a lot on here and I dont find anything conclusive.
I have a mainform which need to be constantly open. I have a button which call a form2, I do some action on it, then call a form3 which let the user choose some item then close it and come back to mainform. The tricky part is i need to pass data from f3 to f1.
My problem is, on the second form I have a next & cancel buttons (and a close button) . Next button hide f2, form1 "come back" & call the form3. Why not call directly the form3? because I need to pass data from form3 to form1 so i Need to call form3 from form1.
So i want Cancelbutton (and closing button) to come back to form1 without calling form3. I am not sure to be understood so here's my code
public partial class Form1 : Form
{
private void bouya_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
Form3 form3 = new Form3(); //it opens when form2 closes!!!
form3.ShowDialog();
string data1 = form3.label2.Text; //the data i need, without calling form3 from here i can't pass data !!!
//do some action with data1
}
}
form2 is very simple
public partial class Form2 : Form
{
private void Next_Click(object sender, EventArgs e)
{ this.Hide(); }
private void Cancel_Click(object sender, EventArgs e)
{//i just want here to cancel winform3 to be called}
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ // Actually this command does'nt work at all i tried
//to put a messagebox.Show("bla") nothing happens but i would like to do the same}
}
And form3 I select an item from a list to be send to form1
public partial class Form3 : Form
{
private void button1_Click(object sender, EventArgs e)
{
foreach (Object obj in listBox1.SelectedItems)
{
label2.Text = obj.ToString();
this.Hide();
}
}
}
Ok for some of you my problem would seem useless and i complicated my code for nothing but i really need these form. (and i need to open form3 from form1 to send data 3 to 1)
Forget about *Form1 form1 = new Form1(); it calls a new form1 and i don't want it !
You actually do not need to call form3 from form1 just to pass a value. If I understand you correctly, the desired flow should be like this
form1
opens
form2
opens
form3
|
| value
|
form1 <---+
So form1 opens form2 which opens form3 and the value from form3 should be passed back to form1, right?
What I'd do is declare a property ValuePickedInForm3 or whatever in form2 and get this in form1 when form2 closes.
Example
This pseudo-code is in the form2 class
public string ValueFromForm3
{
get;
private set;
}
public void ShowForm3()
{
using (Form3 f3 = new Form3())
{
if (f3.ShowDialog(this) == DialogResult.OK)
ValueFromForm3 = f3.TheValueYouNeed;
}
}
This pseudo-code goes into the form1 class:
public void ShowForm2()
{
using (Form2 f2 = new Form2())
{
if (f2.ShowDialog(this) == DialogResult.OK)
DoSomethingWith(f2.ValueFromForm3);
}
}

pass listbox item to a textbox on another form c#

I am making this simple Windows forms app in Visual studio in c#. I have two forms. On form1 I have a textbox,listbox and two buttons (one to insert into listbox from textbox and another to open form2). On form2 I only have a textbox. I just simply want, when click on a button (for opening form2) on form1, form2 to open and textbox to contain (on formLoad) selected item from listbox from form1. But when I click on button it says "Object reference not set to an instance of an object". What am I doing wrong? I am pretty sure it's something simple but I just can't get it.
Thanks in advance!
Here is my code:
on form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
public string Transfer
{
get { return listBox1.SelectedItem.ToString(); }
}
and on form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
textBox1.Text = f1.Transfer;
}
Because in the Form2_Load event you always create a new instance of Form1 and then access the Transfer property which accesses listBox1.SelectedItem which is not set for the newly created form.
You should rather pass a referece to form 1 in the button event:
on form1:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
and on form2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.Transfer;
}
}
In your Form2_Load method, you're creating a new instance of the object Form1, separate from your existing item.
Instead, you need to either:
a) Pass a reference to your current Form1 object to Form2, so that Form2 can access the Transfer property.
or b) Add a new property to Form2 (called Transfer, say), and then when you create Form2, assign the current textbox value to this property, like so:
Form2 f2 = new Form2();
f2.Transfer = listBox1.SelectedItem.ToString();
f2.ShowDialog();
You could also do this by adding a parameter to the constructor of Form2, although that's really a design decision.
because you haven't selected your listbox item,value listBox1.SelectedItem is null.Practice doing try catch block
You are creating new Form1 instance here, which is not related to Form1 instance which you used to open Form2:
private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1(); // here is new instance of Form1 created
textBox1.Text = f1.Transfer;
}
So, this new Form1 instance does not have selected item and you have error. I suggest you to pass selected item value to Form2 when you opening Form2:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(Transfer); // pass selected item value to constructor
f2.ShowDialog();
}
All you need is changing Form2 constructor to accept this string:
public Form2(string transfer)
{
InitializeComponent();
textBox1.Text = transfer;
}

Categories