winform中的弹出窗口c#

我正在开发一个需要弹出窗口的项目。 但问题是我也想通过表单设计器在这个弹出窗口中添加文本框等。

所以基本上我有一个按钮,当你点击它时,它将打开我在表单设计器中设计的另一个窗口。

我一直在做一些谷歌搜索,但我还没有找到我需要的东西所以我希望你们能帮助我!

只需使用Visual Studio创建另一个表单(让我们称之为FormPopoup )。 在按钮处理程序中编写以下代码:

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

如果你需要非模态窗口使用: form.Show(); 。 如果您需要对话框(因此您的代码将在此调用中挂起,直到您关闭打开的表单),请使用: form.ShowDialog()

C#中的Form是inheritanceForm基类的类。

您可以通过创建类的实例并调用ShowDialog()来显示弹出窗口。

这并不容易,因为Windows窗体中不支持弹出窗口。 虽然Windows窗体基于win32,但支持win32弹出窗口。 如果你接受一些技巧,下面的代码将设置你弹出窗口。 您决定是否要充分利用它:

 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); } } 

尝试一下,你必须玩它的位置和大小。 使用它错误,没有任何显示。

如果您想在单击按钮时创建新表单,则以下代码可能对您有用:

 private void settingsButton_Click(Object sender, EventArgs e) { // Create a new instance of the Form2 class Form2 settingsForm = new Form2(); // Show the settings form settingsForm.Show(); } 

从这里,您还可以使用“显示对话框”方法

“但问题是我也希望能够通过表单设计器在这个弹出窗口中添加文本框等。”

从您的描述中不清楚您在开发过程的哪个阶段。如果您还没有弄清楚,要创建一个新表单,请单击项目 – >添加Windows表单 ,然后输入名称单击并单击“添加”按钮。 现在,您可以按预期向表单添加控件。

当需要显示它时,请按照其他post的建议创建实例并根据需要调用Show()或ShowDialog()。

我正在使用这种方法。

从要弹出的内容中添加一个,添加所需的所有控件。 在代码中,您可以处理用户输入并将结果返回给调用者。 弹出窗体只需创建窗体和显示方法的新实例。

 /* create new form instance. i am overriding constructor to allow the caller form to set the form header */ var t = new TextPrompt("Insert your message and click Send button"); // pop up the form t.Show(); if (t.DialogResult == System.Windows.Forms.DialogResult.OK) { MessageBox.Show("RTP", "Message sent to user"); }