Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 20, 2009 7:19 AM
Hello
how can i create pop up window in C# desktop applicationIt's Me
All replies (4)
Tuesday, October 20, 2009 7:33 AM âś…Answered
Hi,
Add a new form to your project, create an instance of it and use either the Show or ShowDialog method of the object instance. For example, if you add a new form called Form2;
Form2 popUpForm = new Form2();
popUpForm.Show();
or
using (Form2 popUpForm = new Form2())
{
popUpForm.ShowDialog();
}
The second example will popup the new form, prevent any existing windows in the same application being used, and stop the code from executing further until the pop-up form is closed. The first example pops up the new form and lets the code continue immediately.
Is that what you wanted or do you have additional requirements ?
Tuesday, October 20, 2009 7:31 AM
You can open any form by ShowDialog method to show form as modal popup, or you can call Show directly.
Ex:
Form1 frm1 = new Form1();
frm1.Show(); //or frm1.ShowDialog();
Also here are some usefull links.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(VS.71).aspx
http://msdn.microsoft.com/en-us/library/39wcs2dh(VS.80).aspx
Monday, October 26, 2009 9:08 PM
Sorry I am new to learning C# and having to teach it to my self so sorry if my questions seem stupid just want to make sure I understand.
Ok say you have created Form1 call Alpha for you main window of your applications. Within this application you have created a menu with different menuitems. Say you have a menuitem call Handover and when this menuitem is clicked you want another form to popup called Handover. How would you code this? I noticed in VS2008 you basically have two place to put code the .designer.cs and the .cs fiile. Right now I have gotten as far as figuring out that once you double click the menuitem in the Designers View it takes you to the .cs which is where you program the functions you want to happen. Also once you double-click you create a private void variblename_Click(object sender, EventArgs e) { code here } statement. I am just not sure what code to place in the code here part. I know more about C++ and this is a little different. Any help would be great here. Thanks!
Monday, October 26, 2009 9:44 PM | 1 vote
Hi,
So a couple of things;
While you can (and sometimes should) write code in the designer file, that file is pretty much there just to store the code generated for you when you use the Windows Forms designer (or WPF/Web Forms designer etc). That code is maintained for you by VS and usually doesn't need you to edit it, or write code in that file, so most of the time you can just ignore it. The other .cs file is where you write your code, and that's why when you double click a control it creates the event handler in that file and not the designer file. One other note... since the code in the designer file is managed by the designer, editing it is often a bad idea since the designer may well overwrite your changes the next time it is opened, or used to modify the form etc.
The controlname_Click(object sender, EventArgs e) method is called an event handler... it is a method that is called when some event occurs. While the name can be anything, it typically takes the form of <controlname>_<eventname>. In this case the designer has generated the method that wll be called for you when that control is clicked, so in your example this is where you would write the code to create and show the new form, i.e
private void mnuHandOver_Click(object sender, EventArgs e)
{
HandOverForm hof = new HandOverForm();
hof.Show();
}
If you look inside the designer file, in the InitializeComponent method you will also see a line that looks something like
mnuHandOver.Click += new EventHandler(mnuHandOver_Click);
This line was also generated for you by the designer when you double clicked the control, and is connecting the method it generated to the specific event on the control that it is associated with. While this isn't important to you right now, it is useful to know in the future if you are working with events on non-controls, or dynamically created controls at runtime.