2013-05-09 155 views
16

我正在一个项目,我需要一个弹出窗口。但事情是我也希望能够通过窗体设计器在这个弹出窗口中添加文本框等。winform c弹出窗口#

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

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

+0

你能不能请我们发一些示例代码来解密? – Sean 2013-05-09 14:03:11

+0

你最好创建自己的表单来完成你想要做的事情。 – gwin003 2013-05-09 14:06:46

回答

19

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

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

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

+0

在代码片段中的注释应为://如果您需要非模态窗口 – AdamBT 2016-02-16 19:45:18

+0

@AdamBT感谢修复 – 2016-02-16 20:44:10

+0

20年过去了,至今仍是一片热潮! – 2018-01-07 20:43:53

5

C#中的表格是继承Form基类的类。

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

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

从这里,你还可以使用'Show Dialog' method

1

“但事实是我也希望能够添加文本框等,在这个弹出窗口直通窗体设计器。”

这是从你在你在什么阶段,在发展过程中的描述不清楚如果您还没有计算出来,创建一个新的表格你点击项目 - >添加Windows窗体,则键入表单的名称并点击“添加”按钮。现在,您可以按照预期的方式将控件添加到表单中。

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

5

这不是那么容易,因为基本弹出窗口不支持在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); 
    } 
} 

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

+0

不错,这可以从UserControl继承还是会产生问题?重写wm_mouseactivate的目的是什么?最后一个问题,我可以使用this.Activate()保持与单声道compat或将不起作用,因为这不是一种形式? ty – Behrooz 2015-06-30 07:35:29

+1

不知道用户控制,它本身已经从控制派生,所以有其他专门的行为而不是控制,但它可能工作。从来没有在单声道上测试过,我不确定dll导入是否更具体。总之它正在使用底层的win32 API系统。 – 2015-06-30 14:54:31

+0

嗨phillip,你有关于这个问题的任何想法https://stackoverflow.com/questions/47384504/in-c-sharp-winforms-how-can-i-prevent-form-from-being-activated-when-一个控制 – barlop 2017-11-20 03:44:38

0

我正在使用这种方法。

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

/* 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"); 
}