winform c#中的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16463599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:03:35  来源:igfitidea点击:

Popup window in winform c#

c#winformspopupwindow

提问by Kevin

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!

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

采纳答案by Piotr Stapp

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

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

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

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

回答by SLaks

Forms in C# are classes that inherit the Formbase class.

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

You can show a popup by creating an instance of the class and calling ShowDialog().

您可以通过创建类的实例并调用ShowDialog().

回答by Sean

If you mean to create a new form when a button is clicked, the below code may be of some use to you:

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

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

From here, you could also use the 'Show Dialog' method

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

回答by Idle_Mind

"But the thing is I also want to be able to add textboxes etc in this popup window thru the form designer."

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

It's unclear from your description at what stage in the development process you're in. If you haven't already figured it out, to create a new Form you click on Project --> Add Windows Form, then type in a name for the form and hit the "Add" button. Now you can add controls to your form as you'd expect.

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

When it comes time to display it, follow the advice of the other posts to create an instance and call Show() or ShowDialog() as appropriate.

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

回答by Philip Stuyck

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 :

这不是那么容易,因为 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);
    }
}

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

稍微试验一下,你必须玩弄它的位置和大小。用错了,什么都不显示。

回答by Benny Margalit

i am using this method.

我正在使用这种方法。

add a from that you want to pop up, add all controls you need. in the code you can handle the user input and return result to the caller. for pop up the form just create a new instance of the form and show method.

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

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