Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup window in winform c#

I'm working on a project where I need a popup window. But the thing is I also want to be able to add textboxes etc in this popup window thru the form designer.

So basically I have a button and when you click on it it will open another window that I've designed in the form designer.

I've been doing some googling but I haven't found what I needed yet so I was hoping you guys could help me!

like image 284
Kevin Avatar asked May 09 '13 14:05

Kevin


People also ask

How do I create a popup in Visual Studio?

Just type mbox then hit tab it will give you a magic shortcut to pump up a message box. Note: This only works in Visual Studio.

How do I open ASPX page as a popup window from code behind?

string url = "D13. aspx"; string s = "window. open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"; ClientScript. RegisterStartupScript(this.

Is WinForm obsolete?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.


2 Answers

Just create another form (let's call it formPopup) using Visual Studio. In a button handler write the following code:

var formPopup = new Form(); formPopup.Show(this); // if you need non-modal window 

If you need a non-modal window use: formPopup.Show();. If you need a dialog (so your code will hang on this invocation until you close the opened form) use: formPopup.ShowDialog()

like image 129
Piotr Stapp Avatar answered Oct 05 '22 14:10

Piotr Stapp


This is not so easy because basically popups are not supported in windows forms. Although windows forms is based on win32 and in win32 popup are supported. If you accept a few tricks, following code will set you going with a popup. You decide if you want to put it to good use :

class PopupWindow : Control {     private const int WM_ACTIVATE = 0x0006;     private const int WM_MOUSEACTIVATE = 0x0021;      private Control ownerControl;      public PopupWindow(Control ownerControl)         :base()     {         this.ownerControl = ownerControl;         base.SetTopLevel(true);     }      public Control OwnerControl     {         get         {             return (this.ownerControl as Control);         }         set         {             this.ownerControl = value;         }     }      protected override CreateParams CreateParams     {         get         {             CreateParams createParams = base.CreateParams;              createParams.Style = WindowStyles.WS_POPUP |                                  WindowStyles.WS_VISIBLE |                                  WindowStyles.WS_CLIPSIBLINGS |                                  WindowStyles.WS_CLIPCHILDREN |                                  WindowStyles.WS_MAXIMIZEBOX |                                  WindowStyles.WS_BORDER;             createParams.ExStyle = WindowsExtendedStyles.WS_EX_LEFT |                                    WindowsExtendedStyles.WS_EX_LTRREADING |                                    WindowsExtendedStyles.WS_EX_RIGHTSCROLLBAR |                                     WindowsExtendedStyles.WS_EX_TOPMOST;              createParams.Parent = (this.ownerControl != null) ? this.ownerControl.Handle : IntPtr.Zero;             return createParams;         }     }      [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]     public static extern IntPtr SetActiveWindow(HandleRef hWnd);      protected override void WndProc(ref Message m)     {         switch (m.Msg)         {             case WM_ACTIVATE:                 {                     if ((int)m.WParam == 1)                     {                         //window is being activated                         if (ownerControl != null)                         {                             SetActiveWindow(new HandleRef(this, ownerControl.FindForm().Handle));                         }                     }                     break;                 }             case WM_MOUSEACTIVATE:                 {                     m.Result = new IntPtr(MouseActivate.MA_NOACTIVATE);                     return;                     //break;                 }         }         base.WndProc(ref m);     }      protected override void OnPaint(PaintEventArgs e)     {         base.OnPaint(e);         e.Graphics.FillRectangle(SystemBrushes.Info, 0, 0, Width, Height);         e.Graphics.DrawString((ownerControl as VerticalDateScrollBar).FirstVisibleDate.ToLongDateString(), this.Font, SystemBrushes.InfoText, 2, 2);     } } 

Experiment with it a bit, you have to play around with its position and its size. Use it wrong and nothing shows.

like image 20
Philip Stuyck Avatar answered Oct 05 '22 14:10

Philip Stuyck