Get control of a Minimized form - c#

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

Related

Why can I not call a form object in two events?

I'm making like a add contact form with two buttons, ADD CONTACT and EDIT CONTACT. When a user clicks add contact, it pops up another form where the user can add contact information.
I want to give them the option to edit that info by clicking the EDIT CONTACT button which should pop up the SAME form.
However its not letting me call the object of the form twice, saying that I cannot press the edit button after the add button.
How do I call a form object twice?
//instatiating an object of the form
FormContact contactForm = new FormContact();
public FormManager()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
//displaying it when the user clicks add button
contactForm.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
//trying to display it again but gives this exception
///System.ObjectDisposedException: 'Cannot access a disposed
///object.
///Object name: 'FormContact'.'
contactForm.Show();
}
Error:
///System.ObjectDisposedException: 'Cannot access a disposed
///object.
///Object name: 'FormContact'.'
The problem is you're closing the form after you've shown it.
You click the Show button
Your only instance of the form is shown
You close it with the X in the top corner
The runtime destroys the form (disposes it) after it is closed
You try to show it again but this this it's gone, trashed, doesn't exist any more, waiting to be garbage collected
Either make a new form each time you open it (to reduce code clutter, assign this same event handler to both button clicks, or copy paste it out twice if you want them to be coded differently eventually):
private void btnAddOrEdit_Click(object sender, EventArgs e)
{
new FormContact().Show();
}
Or intercept the FormClosing event of the FormContact form and cancel the closing, and perform a Hide() instead so that instead of being destroyed your form is made invisible. It will then still exist and can be Show()n the next time. To handle the event, open the FormContact designer, click anywhere in the form background, click lightning bolt in properties grid, double click the FormClosing entry:
private void FormClosing(object sender, FormClosingEventArgs e){
e.Cancel = true;
this.Hide();
}
Which method you choose depends how you want your program to behave:
If you make a new form each time, and you Show instead of ShowDialog your user could click Add twice and see two forms. Then could click Add 10 times and see 10 forms. Using ShowDialog means that the main window won't accept any more clicks until the FormContact is closed. You might or might not want this either
If you Hide (not close; hiding is different from closing) and Show the same form rather than making a new one then the user can click Add 10 times but they still only see one form
FormContact contactForm = new FormContact(); is a Member variable, it's scope is Private and is visible to the entire class.
After you first Show the form:
contactForm.Show();
The form is already showing. Therefore if you call Show again, it won't do anything as the instance of the form/class is already showing.
If you want to show two instances of the form, you will need to instantiate two instances eg:
private void btnAdd_Click(object sender, EventArgs e)
{
FormContact contactForm = new FormContact();
contactForm.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
FormContact contactForm = new FormContact();
contactForm.Show();
}
Or make two instances of it:
FormContact contactForm1 = new FormContact();
FormContact contactForm2 = new FormContact();
private void btnAdd_Click(object sender, EventArgs e)
{
contactForm1.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
contactForm2.Show();
}
Or add a argument in the parameter of the Constructor to indicate Add or Edit, eg:
public class FormContact
{
public FormContact(int id)
{
if (id > 0)
{
//Load contact for Editing
}
else
{
//Clear all fields for Adding
foreach(var ctrl in this.Controls)
{
if (ctrl Is TextBoxBase) ctrl.Text = string.Empty
//TODO other controls types... if (ctrl Is ....
}
}
}
}
Then you can call it passing a contactID to edit or 0 to add:
FormContact contactForm = new FormContact(contactID);
contactForm.Show();
I've got another answer, in both methods simply show your form modally:
contactForm.ShowModal();

Override Hide of main form with button in second form

I have a stupid and i think dumb question.
i am building an windows application to go to an login page for an website but it has 2 forms 1 to go to the login and redirect to the stock exachange acount of the user that works fine but when the user clicks the stock exchage rate bunnton the main forn wil hide.
Now for the seccond form there i have a button that need to override that hide but it does'nt
code for the button on de Koers Form to go back to the Belegen form
have tried if else statements but they dont work.
private void Belegen_Click(object sender, EventArgs e)
{
Belegen.Show();
this.Hide();
}
The button om the Belegen form to open stock reates Koers form
private void Koersen_Click(object sender, EventArgs e)
{
Koers Koers = new Koers();
Koers.Show();
this.Hide();
}
Can some one help me out.
basicly what i want is when i click the Koers button Bellegen need to hide and when ik click Belegen in the Koers form Koers needs to hide and belegen needs to be unhidden but Koers need to be opend als well and both forms need to be usable not just one.
I don't really understandy your description, but looking at the code, you should use instances of the forms, and not static calls on the classes themselves.
public class Test {
private Belegen belegen; // do not forget to initialize
private Koers koers; // do not forget to initialize
private void Belegen_Click(object sender, EventArgs e)
{
this.belegen.Show();
this.Hide();
}
private void Koersen_Click(object sender, EventArgs e)
{
koers.Show();
this.Hide();
}
}
You might have split those two two forms in your example. Then you just need to have a reference to Koersen in the Belegen form, and vice versa.

Closing and Opening Forms

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.

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

Re-bind datagrid when window is closed

I have a data grid that displays data from a sql server. I have an add button, that when clicked it opens a new window where the user can put the information for the new item that is being added. When the user clicks save, the data is being saved to the database, but its not showing up the in the grid. Is there a way that I can make the datagrid bind when the add window is closed? Let me know if more info is needed. Thanks.
In my main window, that contains the datagrid code, i have an add button:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
showAddWindow();
}
And, the showAddWindow method is:
private void showAddWindow()
{
add addWindow = new add(dgDataView);
addWindow.Owner = this;
addWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
addWindow.ShowDialog();
}
If you know the Id of new inserted object, then you can send it to the main form, and call Add.Row on the grid with the new info. This way you'll not make a callback to the database for rebinding.
Assuming this is WinForms:
First, when calling the "Add" window, use ShowDialog() instead of Show()
In the main form with the DataGrid the code would look like
private void btnAdd_Click(Object sender, EventArgs e)
{
DialogResult b = frmAdd.ShowDialog();
if(b == DialogResult.Ok)
{
// code to re-bind the grid here.
}
}
in the frmAdd form, you will need to make your Save button set the DialogResult for the form to be DialogResult.Ok after updating the database.
I'm guessing that what you have tried is along the lines of:
private void btnAdd_Click(Object sender, EventArgs e)
{
frmAdd.ShowDialog();
// code to re-bind the grid here.
}
The difference is that with the ShowDialog() call, the main form will wait until the "add" form is closed to continue executing. In my second code sample, just using Show(), the code to re-bind the grid happens immediately after showing the "Add" form, before the user gets a chance to update the data.
(note, I did that code off the top of my head, not in Visual Studio, so it may have mistakes)

Categories

Resources