C# windows form application, closing parent form from child - c#

I know this is going to sound a little confusing but here it goes. So i have this parent form and when I click a button a new child form shows up(note that my parent form its still open). What i want is when i press a button from my child form i want is a new parent form to show up and to close the parent form that was already opening from the beginning. I hope this doesnt sound confusing. I try playing around with it but nothing seems to work
I have something like this on my parent form
Form2 loging = new Form2();
loging.ShowDialog();
on my child form
Form1 loging = new Form1();
loging.Close()
loging.ShowDialog();
this.Close();

Based on your comments to Mitch, it sounds like you need to rebind data on your main form after you close the child form. This is a much better approach than closing/reopening the main form.

In short, you cannot change a window's parent, and you cannot change whether a window is modal. Destroy the child, close the parent, open a new parent, show a new child. Alternatively, if the child window need not be modal, create the child with Form.Show() and then do something like the following in the child form:
parentForm.Close();
Form newParent = new NewParentForm();
newParent.Show();
this.BringToFront();
MFC used to be able to fake being modal, but it did so by using a custom window procedure - not something particularly easy to do in C#.

Based on your comment to Mitch, this what you should do:
In your parent form, create a static ListView object which point to you customer list
public partial class Form1 : Form
{
public static ListView lsvCustomer;
public Form1()
{
InitializeComponent();
// this will allow access from outside the form
lsvCustomer = this.listView1;
}
private void button1_Click(object sender, EventArgs e)
{
frmInput f = new frmInput();
f.ShowDialog(this);
}
}
Then in your child form, you update the list directly from your child form as below :
public partial class frmInput : Form
{
public frmInput()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//update user input to customer list in parent form
Form1.lsvCustomer.Items.Add(textBox1.Text);
}
}

Related

Reloading the form instead of making a new one / accessing textbox outside of form

I am having a problem controlling a textbox. I need to change a value from outside of Form1.cs where the textbox is located for this I have found this snippet.
public void UpdateText(string newValue)
{
torrent_name0.Text = newValue;
}
this allows me in theory to control the textbox outside of Form1.cs, but here comes the problem, every time I want to access it I create a new instance of Form1 instead of using the old one and refreshing it.
Form1 frm = new Form1();
frm.UpdateText("aaaaaaaaaaaa");
frm.Show();
am I missing something? is there a better way to do this? I have tried multiple ways to update the new form but got nowhere.
Bokker,
You will have to have a reference to the singular form1 to which all things refer.
If this is a child form, then as Aybe commented, create a public member of your mainform and name it something.
Public Form myForm1;
You probably have some Event by which you would like Form1 be launched...
A Button click, menu item, toolbar item, etc. In that event you will have to check if the form exists and create if required.
private SomeEvent() {
if (myForm1 == null)
{
myForm1 = new Form1();
myForm1.Show(this);
}
myForm1.UpdateText("some text");
}
Alternatively, you could create the form in the Form Load event, just so long as you create the form prior to attempting the myForm1.UpdateText()
If you follow this paradigm, you are creating myForm1 as part of the main form, best practice says you should also dispose of the form in your main form Closing Event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (myForm1 != null)
{
myForm1.Close();
myForm1.Dispose();
}
}
This is all off the top of my head, so it might not be perfect.
In that case you can pass the instance of form in the method as argument and make the changes like
public void UpdateText(string newValue, Form1 frm)
{
frm.torrent_name0.Text = newValue;
}

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}

how to show form in mdiparent from button in another form by C#

I have two form ( form1 and form2 ) and mdiparent .
button1 in form1
when click this button I want show form2 in mdiparent
The key points when you want to show a form in an MDI Parent, are:
You should have an form with property IsMdiContainer set to true
You should show your mdi parent form
When you want to show a form as mdi child, Set the propetry MdiParent of your child form to and instance of your mdi parent form
So if your Form1 is showing as mdi Child, in button click handler of your form 1, you can simply do this:
var f = new Form2();
f.MdiParent = this.MdiParent;
f.Show();
else, if your mdi parent form is open but form 1 is not mdi child:
var f = new Form2();
//I supposed that [mdiparent] is class name of your mdi parent form
f.MdiParent = Application.OpenForms.OfType<mdiparent>().FirstOrDefault();
f.Show();
esle you should show your mdi parent form first and use above code to show form 2 as mdi child.
The only thing you need is to create a Form2, set it's MdiParent property and show it. The only problem is dynamically setting MdiParent property - you will need to hold the instance of MdiParent. There are several ways to do this "properly".
Simple way
In Form1 button click should have the following event handler:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = this.MdiParent; // "this" is Form1
form.Show();
}
This solution is less architectural - however, you can choose this one if it is suitable.
Singleton solution
If I did this, I would use singleton pattern. That's how I would do this:
MdiParent:
public class MdiParent : Form
{
private static MdiParent _instance;
public static MdiParent Instance
{
get { return _instance ?? (_instance = new MdiParent()); }
}
}
In the place where you instantiate your MdiParent:
MdiParent.Instance.Show();
// instead of
new MdiParent().Show();
If it is a main form - Main in Program.cs:
Application.Run(MdiParent.Instance);
// instead of
Application.Run(new MdiParent());
Form1 button click event:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = MdiParent.Instance;
form2.Show();
}

call the form_load() function on closing a child form

I am creating an app which currently has 3 forms,
Parent form - to accept login details from the user, validate and if successful hides itself and opens a child form.
1st Child form - on form load connects to the database fetches data and displays it. Click on new entry and another child form opens.
2nd child form - user enters new data for new entry in the database, on success a success message box comes up. user then may click on close button and this form hides itself and the 1st Child form is shown.
what i want to do is to find out someway to reload the 1st Child form, on closing thee 2nd Child form. This will result in the records being displayed in it to get refreshed and thus show the new entry that was just made using the 2nd Child form.
Please guide me as to how i can achieve this.
Here is the code that i used to handle the hide events.
In the main form on login event = true
cpanel child = new cpanel(); //create new isntance of form, cpanel is the 1st child form
child.FormClosed += new FormClosedEventHandler(child_FormClosed); //add handler to catch when child form is closed
child.Show(); //show child
this.Hide(); //hide parent
void child_FormClosed(object sender, FormClosedEventArgs e)
{
//when child form is closed, the parent reappears
MessageBox.Show("You have been logged out.");
Application.Exit();
}
In the 2nd child form i have something like this
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
Make your ChildForm1 to be main form:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using(var loginForm = new LoginForm())
if (loginForm.ShowDialog() != DialogResult.OK)
return;
Application.Run(new ChildForm1());
And show ChildFrom2 from ChildForm1 form:
Hide(); // you really don't need to hide main form
using(var childForm2 = new ChildForm2())
{
if (childForm2.ShowDialog() == DialogResult.OK)
{
// update ChildForm1 with data from ChildForm2
}
}
Show();
You can use this :
In 2nd Child form before closing:
childForm1.refresh()
In 1st Child form:
public static void refresh() //You can use it with parameters too
{
//do something
}
Ok once again i have finally solved the problem, and is posting the solution in hopes it will someday help someone with similar problem.
The actual solution is very simple really, here is what needed to be done.
add this line in the child form, somewhere in the protected section of 2nd Form other than inside a method so that it is always present.
cpanel child = new cpanel();
inside the close button method of the 2nd Form needs to be something like this.
private void button3_Click(object sender, EventArgs e)
{
child.Show();
this.Hide(); //hide parent
}
This will enable you to get an updated 1st Form when you click on the Close button of the 2nd Form after updating the contents.

how to change the property of the first form while using a second form?

I'm using two forms and I disable the first one when the second form shows up. I couldn't find a way to enable the first form when the second one is closed.
Passing a parameter could be a solution but I bet there is a simpler way.
First I thought of enabling the first form on the destructor of the second but could not do it.
Anyone have any suggestions?
You can show second form with ShowDialog() - form will be shown as modal, first form will be enabled only when second will be closed.
For future problems you can have a field in second form to have instance of first one, and use that instance, if you need, for example you can use custom constructor:
class SecondForm: Form
{
FirstForm _parentForm;
public SeconForm(FirstForm form)
{
InitializeComponent();
_parentForm = form;
}
void DoSomethingWithParent()
{
_parentForm.DoSomesting();
}
}
As has been mentioned, in this specific case it probably makes sense to use a modal dialog for opening the second form.
To cover the case when that isn't applicable, the accepted best practice would be to subscribe to the FormClosing event of the second form from the first, and in the event handler you could enable "yourself" and do anything else that you might want to do as a result of the other form being closed. Here is a simple example:
public partial class ParentForm : Form
{
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.FormClosing += new FormClosingEventHandler(child_FormClosing);
Hide();
child.Show();
}
private void child_FormClosing(object sender, FormClosingEventArgs e)
{
Show();
}
}

Categories

Resources