How to open a new window in Windows Forms in .NET? - c#

I have an application that among other things has an Edit Button and when the user clicks on that button I want a new window to open with various textboxes for editing purposes.
I can create a new window with code like
Form editform = new Form();
But I want to design that window in the Designer too.

In Visual Studio, right-click on your project and select Add->Windows Form. That will give you a new form to work with. Lay it out how you want. Then you can launch the window from your main window with code similar to the following:
MyEditForm form = new MyEditForm();
form.Show();

To answer Rick's comment on Brian's answer:
using (var login = new Login())
{
switch(login.ShowDialog())
{
case DialogResult.OK:
Application.Run(new Studio());
break;
}
}

Related

Taskbar Issue for My App

When I click on my Winforms app then its forms are opened in separate section in Taskbar with default C# icon. How can I solve this issue?
Update:
At first I open my login form as bellow:
Application.Run(new frmLogin());
Then I open my main form from the login form as bellow:
this.Hide();
frmMain frmMain = new frmMain();
frmMain.Show();
This sounds like a system problem. Here are 3 solutions:
Restart your computer.
Restart explorer.exe or File Explorer. How to do it.
Unpin then re-pin your program on the taskbar.
first solution: you can set this property of the form
and another solution :
Did you try parent form and child form?
Set Parent of the forms, So they will be open as child of your main form
For Example(In the main form when You want to open another form) :
FrmChild frmChild= new FrmChild ();
frmChild.Parent = this;
FrmChild.Show();

Form in a form behind my menu strip

I created a Form with menu strip. When I click on a menu item I want to open a new From in my main From.
It works but a part of the form is behind the menu like this :
my code :
WindowDossierProtection wdPr = new WindowDossierProtection();
wdPr.TopLevel = false;
this.Controls.Add(wdPr);
wdPr.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
wdPr.Dock = DockStyle.Fill;
wdPr.Show();
Can you help me ? thanks !!
Use BringToFront() to get your desired result:
WindowDossierProtection wdPr = new WindowDossierProtection();
wdPr.TopLevel = false;
this.Controls.Add(wdPr);
wdPr.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
wdPr.Dock = DockStyle.Fill;
wdPr.BringToFront();
wdPr.Show();
Do you use Visual Studio or something else?
This can be easily done by the Document Outline.
In Visual Studio go to :
View /
Other Windows /
Document Outline
Or use the shortcut:
Ctrl+W, U
Then, choose your form (in design view).
In the Document Outline window you will see all your controls and the order that exist in your form.
You simply have to drag your menuStrip in the end. (After your new form control)
And voila.
(I use VS2010)

Form closing before new shows

Hello I'm making my first Windows Forms app in C# using Visual Studio and I have a little problem.
While I'm opening a new form and closing the previous one, when I run the app it looks like it's closing the previous form before it opens a new one.
It doesn't look good and I want to avoid it.
UserPanel MDIUserPanel = new UserPanel(Username);
MDIUserPanel.MdiParent = this.MdiParent;
MDIUserPanel.Show();
this.Close();
I don't know what is going wrong. I will be thankful for any help.
Wirth regards,
DarQScreaM
#Edit
This doesn't seem to be the case actually. Propably its that :
I have 3 forms MainForm, Login, LoggedUser.
MainForm is MDI container with FormBorderStyle set on Fixed Single
Login is child of MainForm with FormBorderStyle set on None
LoggedUser is child of MainForm with FormBorderStyle set on None
When application is runned Login form is created in MainForm. MainForm is never closed since its container.
But when i move from Login form to LoggedUser form and vice-versa its created with FormBorderStyle = Fixed Single (normal windows window) and after 0.5~second its changed into None.
Editing it into that didn't really help :
MDIUserPanel.FormBorderStyle = FormBorderStyle.None;
MDIUserPanel.Show();
#Edit2
This change fixed it for me. I don't know why setting it on Form properties didn't work properly. It looked like form was created as FormBorderStyle.FixedSingle and then it was changed into FormBorderStyle.None. If I made this manually in Load it worked but U had to fix the size of my window too. It doesn't seem to be good though. It should work from the beginning since Form properties in Designer are like that from the very beginning.
private void UserPanel_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Size = new Size(649, 357);
}
You can use the form's Shown event to ensure that the new form have been showed before you close the old one.
UserPanel MDIUserPanel = new UserPanel();
MDIUserPanel.Shown += ((s, ee) =>
{
this.Close();
});
MDIUserPanel.Show();
If this form you are trying to close, is the main form that opens when you run your application, then that's the reason. Closing the main form will exit your application. Try instead to just hide this form instead of closing it. And to make sure you can exit your application ('cause you've hidden your main form), just override the closing event of your current form, and put an "Application.Exit()" in it.
Hope this helps you !
First Step:
this.Hide();
Second Step:
MDIUserPanel.Show();

How to open a child windows under parent window on menu item click in WPF?

I am developing an application in C#. I'm using .Net under WPF. I want to open a new child window (like a dialog) and it should open within the main window, just below the menu bar.
However, the newly opened windows is simply showing an OS task bar. How can I open the various new windows(like a dialog box) under the main window?
Try this.
MyChildWindow cw = new MyChildWindow();
cw.ShowInTaskbar = false;
cw.Owner = Application.Current.MainWindow;
cw.Show();

close the form from mdi parent when i click to open any other form

i have an administrator form and in this form lot of menu items and all open a new form. but when I open a new from again i will open a new from then new form will be open but old form will not be closed.there are many form open in administrator form. i wand when i open any form on click a menu item the other form close.
Simply you have to know each form object then you can close it, for example when you want to open one windows you write
new MyForm().ShowDialog();
Ok instead you can declare a form object in the top of your administrator form:
Form oldForm;
when user press one of your forms list:
if (oldForm != null)
oldForm.Close();
Form newOne = new UsersForm();
oldForm = newOne;
newOne.ShowDialog();

Categories

Resources