我正在一個項目,我需要一個彈出窗口。但事情是我也希望能夠通過窗體設計器在這個彈出窗口中添加文本框等。winform c彈出窗口#
所以基本上我有一個按鈕,當你點擊它時,它會打開我在窗體設計器中設計的另一個窗口。
我一直在做一些谷歌搜索,但我還沒有找到我需要的東西,所以我希望你們能幫助我!
我正在一個項目,我需要一個彈出窗口。但事情是我也希望能夠通過窗體設計器在這個彈出窗口中添加文本框等。winform c彈出窗口#
所以基本上我有一個按鈕,當你點擊它時,它會打開我在窗體設計器中設計的另一個窗口。
我一直在做一些谷歌搜索,但我還沒有找到我需要的東西,所以我希望你們能幫助我!
只需使用Visual Studio創建另一個表單(讓我們稱之爲FormPopoup
)。在按鈕處理程序編寫代碼followng:
var form = new FormPopoup();
form.Show(this); // if you need non-modal window
如果您需要非模態窗口使用:form.Show();
。如果你需要對話框(所以你的代碼將掛在這個調用,直到你關閉打開的表單)使用:form.ShowDialog()
在代碼片段中的註釋應爲://如果您需要非模態窗口 – AdamBT 2016-02-16 19:45:18
@AdamBT感謝修復 – 2016-02-16 20:44:10
20年過去了,至今仍是一片熱潮! – 2018-01-07 20:43:53
C#中的表格是繼承Form
基類的類。
您可以通過創建類的實例並調用ShowDialog()
來顯示彈出窗口。
如果你的意思是創建一個新的表單,當點擊一個按鈕時,下面的代碼可能對你有用:
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
「但事實是我也希望能夠添加文本框等,在這個彈出窗口直通窗體設計器。」
這是從你在你在什麼階段,在發展過程中的描述不清楚如果您還沒有計算出來,創建一個新的表格你點擊項目 - >添加Windows窗體,則鍵入表單的名稱並點擊「添加」按鈕。現在,您可以按照預期的方式將控件添加到表單中。
當需要顯示它時,請按照其他帖子的建議創建實例並根據需要調用Show()或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);
}
}
試驗一下,你必須玩弄它的位置和它的大小。使用它錯誤,沒有顯示。
不錯,這可以從UserControl繼承還是會產生問題?重寫wm_mouseactivate的目的是什麼?最後一個問題,我可以使用this.Activate()保持與單聲道compat或將不起作用,因爲這不是一種形式? ty – Behrooz 2015-06-30 07:35:29
不知道用戶控制,它本身已經從控制派生,所以有其他專門的行爲而不是控制,但它可能工作。從來沒有在單聲道上測試過,我不確定dll導入是否更具體。總之它正在使用底層的win32 API系統。 – 2015-06-30 14:54:31
嗨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
我正在使用這種方法。
添加一個你想彈出的,添加你需要的所有控件。 在代碼中您可以處理用戶輸入並將結果返回給調用者。 用於彈出窗體只是創建窗體和顯示方法的新實例。
/* 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");
}
你能不能請我們發一些示例代碼來解密? – Sean 2013-05-09 14:03:11
你最好創建自己的表單來完成你想要做的事情。 – gwin003 2013-05-09 14:06:46