How to call a method from another Form? - c#

I have my dataGridView in Form1. I have UpdateForm method in Form2. I have Click Event in dataGridview Form1 that loads to Form2 UpdateForm. But when I Close Form2 after made Update then dataGridview Form1 still shows the old information. I have to reload Form1 to see Changes I made. My question is I want to see Changes I made directly after I Close Form2.
I tried like following:
In Form1
Public void RealoadForm()
{
dataGridView1.Update();
RealoadForm();
this.Refresh();
}
Then in Form2 update button and tried even in FormClosed:
Form1 frm = new Form1();
frm.RealoadForm();
But still not working.

Just create public methode that reload your dataGridview. The same method that load information to load to your dataGridview and call it from Form2 like this:
Form1 form = Application.OpenForms.OfType<Form1>().FirstOrDefault();
if(form != null)
{
form.YourLoadMethode();
}

You can change the FormattingApplied = true; in OnCellFormatting event
OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.FormattingApplied = true;
}

Related

C# open Form in Panel

I have 3 forms. The Mainform, Form2 and Form3.
The Mainform contains a large panel in which Form 2 is displayed when the program is started.
I do that with the following code in my Mainform which works great:
private Boolean loaded = false;
private void MainForm_Load(object sender, EventArgs e)
{
if (!loaded)
{
openChildForm(new Form2());
loaded = true;
}
}
public Form activeForm = null;
public void openChildForm(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
panelMain.Controls.Add(childForm);
panelMain.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
Now I want to add a button in Form2 (which is still being displayed in the Panel).
When I press that Button, Form2 should remove itself from the Mainform Panel and instead Form3 should be displayed in that MainForm panel. I tried that with the following code in Form2 at Buttonclick:
new Mainform().openChildForm(new Form3());
However, this does not work. It still displays the Form2 in the Panel and doesn't display Panel3.
In the Mainform it also says that "activeForm" is "null". I guess it is because of the "new MainForm()" call. Can I just access the MainForm without creating a new Instance?
Now I want to add a button in Form2 (which is still being displayed in
the Panel). When I press that Button, Form2 should remove itself from
the Mainform Panel and instead Form3 should be displayed in that
MainForm panel.
Can I just access the MainForm without creating a new Instance?
In Form2, you can cast the ParentForm property to MainForm, and then call openChildForm() against that:
// ... in Form2 ...
private void button1_Click(object sender, EventArgs e)
{
MainForm mf = this.ParentForm as MainForm;
if (mf != null)
{
mf.openChildForm(new Form3());
}
}

In C# how to open third form inside first form when i click a button of the second form

In my Windows Forms application, I have 3 different Forms (Form1, Form2, Form3)
Form1 Contains a Button and a Panel (button1, panel1)
Form2 Contains only a button (button)
Form3 Contains nothing
In Form1 when I click button1 , Form2 opens in panel1
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
Form2 f2 = new Form2();
f2.TopLevel = false;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Fill;
f2.Show();
}
Now, inside Form2, when i click the button, I want the Form3 open inside Form1's panel, I've tried this code...
Form1 f1 = new Form1();
private void button1_Click(object sender, EventArgs e)
{
f1.panel1.Controls.Clear();
Form3 f3 = new Form3();
f3.TopLevel = false;
f1.panel1.Controls.Add(f3);
f3.Dock = DockStyle.Fill;
f3.Show();
}
Note: In Form1 I've changed the modifier to public for the Panel
Step 1: Create functions in Form1 to Show the other Forms:
class Form1
{
public void ShowForm2() {...}
public void ShowForm3() {...}
// TODO: think about: do you need IsForm2Shown?
// TODO: think about what you want if ShowForm2 is called twice
...
}
Step 2: Let Form2 know about Form1. Give Form2 a function to show Form3 inside Form1
class Form2
{
public Form1 Form1 {get; set;}
protected void ShowForm3InsideForm1()
{
this.Form1.ShowForm3();
}
}
Whenever Form one creates Form2 it should fill property Form1. So inside Form1.ShowForm2():
var form2 = new Form2()
{
Form1 = this,
...
}
form2.Show();
Step 3: whenever you find in Form 2 that you want to "show form 3 in form 1", just call the proper function
// Somewhere inside Form2 code you decide that you want to show Form3 inside Form1:
this.ShowForm3InsideForm1();
Simple comme bonjour!

Open Form1 as new instance after successful operations on Form2

I am having two forms as Form1 and Form2 on Form1 button click I am opening the form2 as follows
private Form2 form2;
this.Hide();
form2.ShowDialog();
this.Show();
I have a back button and submit button on Form2 when I click on Back this is how I am showing Form1
this.Hide();
this.DialogResult = DialogResult.OK;
But when I submit the form I need to load the Form1 with new instance, this is what I tried but Form1 is showing multiple times
if (DialogResult.OK == MessageBox.Show("Installation was done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information))
{
Form f1 = new Form ();
f1.Show();
this.Close();
}
When I execute my application this is how it looks after entering data to my textbox
After clicking on button1 it will show the data in Form2 if user wants to modify data he can come back if he clicks on submit I would like to have the new instance of Form1
What you need to do is after you open form 2 do something like
Form1.close();
and then when you are done with the operation:
Form1 form1 = new Form1();
form1.ShowDialog();
this.Close();
Ok if you want to save the data from form1 then i would either fave the data as a file that you could retrieve when ever of pass all of the variables you want to save into Form2 and then save them there. also instead of using a new form 1 you could use the existing one and reset any values that you want to reset.
I hope this helps.
Here is the code I tried, on your Form1 button click event write the following
private void button1_Click(object sender, EventArgs e)
{
form2.ShowData(textBox1.Text);
form2.ShowDialog(this);
}
In your Form2 submit click have the following code
private void button2_Click(object sender, EventArgs e)
{
this.Owner.Hide();
this.Owner = new Form1(this);
this.Hide();
this.Owner.Show();
}

Closing a background form from a custom message box

Brief description:
Cannot close form1 by clicking a button that is on form2.
I have the following forms:
form1
formMessageBox which has button1
form3
I want to:
Pop up formMessageBox from form1 (this is working ok) then from formMessageBox, click button1 to close form1 and open form3.
I have used a variety of techniques however form1 cannot be targeted.
Help much appreciated.
Edit
When form1 and the messagebox are open at the same time. Form1 does not close or hide when button1 is clicked to try and close form1.
Not really sure what you're looking for, but I think you are needing something like this.
Say you have Form1 and you pop up a formMessageBox using ShowDialog().
public class Form1 : Form
{
if (somethingHappened)
{
var formMessageBox = new FormMessageBox();
//ShowDialog() sets the formMessageBox object as a modal dialog owned by Form1
DialogResult result = formMessageBox.ShowDialog();
//Assuming you have OK buttons or something like that in formMessageBox
if (result == DialogResult.OK)
{
var form3 = new Form3();
/*Can't use this.Close() here as it would close the entire application
Just hide it and don't forget to dispose of it later when you actually
want the application to close*/
this.Hide();
form3.Show();
}
}
}
Then in your formMessageBox code, just set the DialogResult according to what occurs:
public class FromMessageBox : Form
{
private void okButton_Click(object sender, EventArgs e)
{
//Do some stuff
this.DialogResult = DialogResult.OK
this.Close();
}
}

C# WindowsForms - Send textbox from form1 to form2 on visual studio

i have 2 forms on visual studio,
form1 have textbox1.text
form2, have textbox2.text and btnSave
obs: form2 open when i click on another button on form 1:
Form new = new form2();
nova.Show();
how can i send textbox2 content from form2 to form1 (textbox1) clicking on btnSave ?
What code will be necessary inside this click button event.
Thanks
Try this please:
Step1: Create a constructor for form2 class as below:
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text = strTextBox;
}
Step2: Instantiate form2 class in form1’s button click event handler as below:
private void button1_Click(object sender, EventArgs e)
{
Form2 obj1 = new Form2(textBox1.Text);
obj1.Show();
this.Hide();
}
Create an event on your second form that can be fired when the form is saved:
public event Action Saved;
Then create a property on that form that allows the textbox's text to be accessed:
public string SomeTextValue //TODO: rename to meaningful name
{ get{ return textbox2.Text;} }
Then you need to fire off the Saved event when you save your form:
if(Saved != null)
Saved();
Then when you first create the form in Form1 attach an event handler to that event:
Form2 child = new Form2();
child.Saved += () => textbox1.Text = child.SomeTextValue;
child.Show();
Note that if you are also closing the second form right when you save it then you don't need a custom event, you can just utilize FormClosing instead.
researching i was able to make it work, lost some hours but now all is perfect, this is code that worked for me:
On form2:
public partial class form2 : Form
{
private string nome;
public string passvalue
{
get { return nome; }
set { nome = value; }
}
form2, button save:
private void btnSalvar_Click(object sender, EventArgs e)
{
passvalue = txtMetragemcubica.Text;
this.Hide();
}
on form1 (this button open form2):
private void btnMetragemcubica_Click(object sender, EventArgs e)
{
form2 n = new form2();
n.ShowDialog();
txtMetragem.Text = n.passvalue
}
Now it work this way: Open on form 1, then i click on button btnMetragemcubica and form2 open, then i insert values on different textbox and have result on txtMetragemcubica, when i click on save button (btnSalvar) it close form2 and send value to form1 in txtMetragem textbox.
Working perfect here, hope help another persons too.
Anyway thanks for all help

Categories

Resources