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.
Related
I have three forms, the main, the second one and the last which is opened by the second one using ShowDialog().
If I minimize the last form, I go back to the first form, and in turn a button is activated to return to the last form. If I click this button, I can see the last form but I can't use it, I can only see it.
How can I get the control of this form?
Code of Minimize of the last form:
private void button_minimize_Click(object sender, EventArgs e)
{
this.Hide();
Principal fom = new Principal(true, this);
fom.ShowDialog();
}
Code of first form to open again the last form:
private void bt_galvCh_Click(object sender, EventArgs e)
{
formMinimizadoGalv.Activate();
formMinimizadoGalv.BringToFront();
formMinimizadoGalv.Focus();
formMinimizadoGalv.Show();
}
This is the last form, I can see it with this code but I can use the green button:
Picture of the last form
i am writing a code for serial key registration.
if the serial key entered by the user is correct then anothr form must open and the present form must close.
please go thought the code.
namespace ExtTrigger
{
public partial class Activation : Form
{
public Activation()
{
InitializeComponent();
}
private void ActivateButton_Click(object sender, EventArgs e)
{
String key;
key = string.Concat(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
if (key == "1234123412341234")
{
Properties.Settings.Default.Registered = true;
MessageBox.Show("hurray", "", MessageBoxButtons.OK);
Form1 f1= new Form1();
f1.ShowDialog();
this.Close();
}
else
MessageBox.Show("No Match", "", MessageBoxButtons.OK);
}
private void Activation_Load(object sender, EventArgs e)
{
}
}
my problem is: On clicking on ActivateBotton, Form1 opens but the present form doesnot close.
i have read in few threads that in VB we can change the property: ShutdownMode.
How can we do that in c#?
f1.ShowDialog(); blocks the call, it doesn't go to the next line until that new form has been closed.
An option would be to use:
f1.Show();
Show doesn't block the call, it passes to the next statement. It will not wait for the new form to be closed.
since you have show the second form as f1.ShowDialog() so first one remain open untile second one close, try this
Form1 f1= new Form1();
f1.Show();
this.Close();
The following code should do the trick:
using(Form1 f1 = new Form1())
{
this.Hide();
DialogResult result = f1.ShowDialog();
if(result == DialogResult.OK)
{
this.Show();
}
}
You create your new form within the using-block, then you hide your main form(or the form you are in at the moment) create a DialogResult that gets set by the newly opened form and open this form. Now you can set the results you want to check for inside of your new form and if everything went well inside of you new form you set the DialogResult to OK via:
this.DialogResult = DialogResult.OK;
Now back in our first form you check for the DialogResult and if it is okay you show your main form again. If it was not okay you could just reopen the 2nd form and let the user try again.
Opening a new form is very simple, but the way you do it really depends on your need.
Case 1: I would like to freeze/ block the calling form on secondary form call
In this case you should be using secondaryFormObj.ShowDialog();
Of course, when using this technique your called form, which now acts as a dialog, should "return" an answer to its caller parent on closure.
For example:
private void SecondaryForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Just a dummy code example.
// Always returns Yes result on form closure
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
}
For more help and examples on this manner you can use MSDN:
DialogResult - MSDN
Case 2: I would like to have both forms responding at the same time
In this case you basically need to call secondaryFormObj.Show();
If you want the caller form to be hidden on secondary form call, just
invoke this.Hide();
after the call to secondaryFormObj.Show(); in the caller class.
You can also close the caller form using this.Close(); as long as the caller form is not the application's main form.
... And remember
Always make sure you initialized the secondary form object before
invoking it with either secondaryFormObj.Show(); or
secondaryFormObj.ShowDialog();
Initializing a form is done the same way like every typical object using the
new operator.
For example: secondaryFormObj = new Form();
Hopes this helps. Happy coding!
I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.
Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.
selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}
I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}
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);
}
}
What is the best way to close a form and get to another form.
At present I have a Main Form and then two forms:
I want to know how to close one form and open another form efficiently.The two times I did it had a slight different outcome:
The main form in my application is just the introduction which will load some Company Logo and show a progress bar which will then take it to a log-in form, where in I have a log-in button, from where the actual application will open.
I have some questions.In the Main Form I have added this code:
private void Form1_Load(object sender, EventArgs e)
{
int i;
for (i = 0; i <= 100; i++)
{
progressBar.Value = i;
label1.Text = "Please wait while Refresh loads up...";
}
}
private void progressTimer_Tick(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
Form form = new FirstForm();
form.ShowDialog(this);
}
1) This works fine, just that the new form that opens up is at the task bar and is not in the center of the screen(as I have set it's property).How do I fix this?
In the First Form I have added this code:
private void loginButton_Click(object sender, EventArgs e)
{
using( ERPQueries eq = new ERPQueries())
{
int? count = eq.CheckEmployee(userTextBox.Text,passwordTextBox.Text);
if (count == 1)
{
//testLabel.Text = "Ok";
this.Close();
Form form = new SecondForm();
form.ShowDialog(this);
}
else
{
testLabel.Text = "Invalid username or password!";
}
}
}
2) Here the next Form pops up in the center of the screen.I want to know how is it different from the first case, as I have used showDialog() in both the cases?
3) Also in the first case my Main Form Disappears, whereas in the second case the First Form is still visible in the background and disappears only after closing the SecondForm.
I'm sure I'm doing a lot of mistakes, my code is flawed.Please help.This is the first time I'm making an application with multiple forms.
Edit:
When I use Show() instead of ShowDialog() I don't see the new form.Am I missing something?
I tried this.Dispose() instead of this.Close().In the first case it works fine.In the second case, it disposes all the forms.
try:
form.Show();
not
form.ShowDialog(this);
which is what makes it modal.
ShowDialog() is blocking call, so if in one Form's deactivate event you call ShowDialog() of another, your parent form will not yet finish it deactivating.
Like a suggesion I can give you create a collection/stack of forms you want to manage and give control over it to a FormManager class, where you implement whatever logic you want and can call either ShowDialog() or Show(). In other words bring out forms Show/Hide management out forms itself.
Regards.