I have a Login form that users use to log in. After logging in, they will be presented with another form, and this form contains different labels. Each label is being treated as a button. The form is divided into 2 panels, the left panel is for the labels, and the right panel is for the button action. For example, I have 3 labels on the left side one called "Home" and the other one is "Register User" and lastly "Log Out" when I press the label "Users" a TabControl will be showing on the right side and it has 2 tabs "Register user" And "Current Users".
After I click on "Log Out" and I log in with a different user I want everything to be clean just like if I was opening it from the beginning. For example, the Home page should show first and not the last visible panel. If I log in with user called "test" now and I click on "Users" and then log out and log in from a different user I will still see The "Users" panel open and every value entered in textboxes is still open. And I don't want this.
The code I used to log out is:
private void LogOutBtn_Click(object sender, EventArgs e)
{
this.Close();
Globals.LoginForm.Show();
}
The code I use to move from the login form to the welcome form is:
private void LoginBtn_Click(object sender, EventArgs e)
{
this.Hide();
Globals.WelcomeForm.ShowDialog();
}
Try declaring a new WelcomeForm each you show it. Something like this:
private void LoginBtn_Click(object sender, EventArgs e)
{
Globals.WelcomeForm NewWelcomeForm = new Globals.WelcomeForm();
this.Hide();
NewWelcomeForm.ShowDialog();
}
The new constructor will reset all the controls and their values.
Never Mind I got it working. I know my mistake :) declaring it in a different class and calling from that class was causing me this problem.
My code now is
private void LoginBtn_Click(object sender, EventArgs e)
{
Welcome NewWelcomeForm = new Welcome();
his.Hide();
NewWelcomeForm.ShowDialog();
}
Related
this is my frmMain image
I have been struggling with this all day. I have 7 buttons in my nav menu. I have my main form which all those buttons are placed and 7 forms that I will connect to my 7 buttons. 1 form for each button. When I go to another form the previous form I opened is still there. I tried using hide and close to exit the other forms but it was not working for me. I'm also using none border form
btnItems to frmItems
btnUsers to frmUsers
btnSuppliers to frmSuppliers
btnStocks to frmStocks (and so on with the 3 more buttons connected to different forms)
This is the code.
private void btnItems_Click(object sender, EventArgs e)
{frmItems op = new frmItems();
op.Show();
}
private void btnSuppliers_Click(object sender, EventArgs e)
{
frmSuppliers op = new frmSuppliers();
op.Show();
}
Although I don't understand EXACTLY what you are trying to accomplish, is showing the forms as a modal dialog an option? This would ensure that a window must be closed before allowing control to go back to the main form and allow another window to be opened.
private void btnSuppliers_Click(object sender, EventArgs e)
{
using (var op = new frmSuppliers())
op.ShowDialog(this);
}
I have a C# Windows Forms App that contain a menu bar.
I want to display a Help Message when I press on the "HELP" menu button.
All that I can see when I press view code is this:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I think that I need to create inside the function a MessageBox or an event that will display the desired message.
Do you have any idea how should I do this, please?
Below should work for what your asking. If you are on your form you can double click the button you want to interact with, and Visual Studio should take you to the empty method.
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This is supposed to be helpful");
}
I am working at a problem in ASP.NET.
I have to create 2 windows (i think that I need to make web forms, i don't know why they said windows) one is the login form, when i press ok and the username and password is ok, I need to
show my second window (webform)
How can I do that?
I tried to do
protected void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.SetFocus("id");
}
But it gives me error
A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property.
What should I do?
Am i right, I have to do separate webforms for thoose windows?
This is the picture from the problem that they provided
If you use webforms you can just use the following code to redirect to second form:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Webform2.aspx");
}
I created a UserControl with the buttons Save, Close and Cancel. I want to close the form without saving on the Cancel button, prompt a message to save on the Close button and Save without closing on the Save button. Normally, I would have used this.Close() on the Cancel button, but the UserControl doesn't have such an option. So I guess I have to set a property for that.
Scrolling down the "Questions that may already have your answer" section, I came across this question: How to close a ChildWindow from an UserControl button loaded inside it? I used the following C# code:
private void btnCancel_Click(object sender, EventArgs e)
{
ProjectInfo infoScreen = (ProjectInfo)this.Parent;
infoScreen.Close();
}
This does the job for one screen, but I wonder if I have to apply this code for all the screen I have? I think there should be a more efficient way. So my question is: Do I need to apply this code for every form I have, or is there another (more efficient) way?
you can use
((Form)this.TopLevelControl).Close();
you can use the FindForm method available for any control:
private void btnCancel_Click(object sender, EventArgs e)
{
Form tmp = this.FindForm();
tmp.Close();
tmp.Dispose();
}
Do not forget to Dispose the form to release resources.
Hope this helps.
You also can close one form in any part of the code using a remote thread:
MyNamespace.MyForm FormThread = (MyNamespace.MyForm)Application.OpenForms["MyForm"];
FormThread.Close();
I found the simple answer :) I all ready thought of something like that.
To close a WinForm in a ButtonClicked Event inside a UserControl use the following code:
private void btnCancel_Click(object sender, EventArgs e)
{
Form someForm = (Form)this.Parent;
someForm.Close();
}
I have a Form1 and another one that I added. Form1 is being run by program.cs at the start. I need to hide Form1 and show options form by the press of a button.
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
}
the above code opens the options form on top, but I need it to replace the current form. how can I achieve this?
Hide current form using this.Close() before showing new one and make sure you are using parameterless Application.Run so program won't close when you close it's main form.
You can use "Hide" and "Show" method :
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
this.Hide();
optionForm.Show();
}
private void submitPassword_Click(object sender, EventArgs e)
{
options optionForm = new options();
optionForm.Show();
this.Hide();
}
Similar solutions where one form calls and acts on another... Such as this one I answered for another. You could do a similar process... pass in your first form to the second... Then show the second... Then, you could HIDE your first form (via this.Hide() ). Then, in your second form, when you click whatever button to select your choice, and need to return back to the first form, you could then use the original form's reference passed INTO the second form to re-Show it, such as in the click on the second form...
this.PreservedForm.Show(); // re-show original form
this.Close(); // and CLOSE this second form...