我有一個 Windows 表單應用程式。我創建了一個自定義UserControl擴展Panel. 在我的其中一個表單中,有一個按鈕應該在單擊時打開該面板。
但是,單擊后,我仍然沒有看到表單上顯示的面板。
表格代碼
public partial class IngredientMenu : Form
{
public IngredientMenu()
{
InitializeComponent();
}
private void btnOpenRegisterBasePanel_Click(object sender, EventArgs e)
{
BaseIngredientPanel baseIngredientPanel = new BaseIngredientPanel();
baseIngredientPanel.Location = new Point(257, 63);
baseIngredientPanel.Show();
baseIngredientPanel.BringToFront();
Console.WriteLine("panel should open");
Console.WriteLine(baseIngredientPanel.Visible);
Console.WriteLine(baseIngredientPanel.Location);
}
}
面板代碼
public partial class BaseIngredientPanel : UserControl
{
public BaseIngredientPanel()
{
InitializeComponent();
}
private void btnRegisterBaseIngredient_Click(object sender, EventArgs e)
{
IngredientContext ingredientContext = new IngredientContext();
if (ingredientContext.RegisterIngredient(txtName, txtUnit, lblBaseIngredientNameValidation, lblBaseIngredientUnitValidation))
{
MessageBox.Show("Ingredient Registered."
, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtName.Text = "";
txtUnit.Text = "";
}
else
{
MessageBox.Show("There are errors in the form fields."
, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BaseIngredientPanel_Load(object sender, EventArgs e)
{
Console.WriteLine("I AM OPEN");
}
}
此外,即使單擊后也不會出現“我正在打開”訊息,因此它似乎通常甚至沒有加載控制元件。
單擊按鈕后如何打開面板?我寧愿不手動將面板拖到設計器中,因為我需要有 5 個面板,而設計師只是想像俄羅斯娃娃一樣將它們裝箱。
uj5u.com熱心網友回復:
所有 UI 控制元件都需要通過 Controls.Add() 方法擁有它們所屬的父級。父控制元件可以是 Form 或其他控制元件(并非所有控制元件都接受子控制元件)。例如,您的面板可以是文本框、組合框、標簽等的父級。
private void btnOpenRegisterBasePanel_Click(object sender, EventArgs e)
{
BaseIngredientPanel baseIngredientPanel = new BaseIngredientPanel();
baseIngredientPanel.Location = new Point(257, 63);
//Add user panel to form.
this.Controls.Add(baseIngredientPanel);
//You will probably not need these two rows any more. Try it out! But make sure your usercontrol has Visible = true.
baseIngredientPanel.Show();
baseIngredientPanel.BringToFront();
}
編輯:
在下面的評論中回答您的問題
如果您需要同時進行許多 UI 更改,那么最好使用 this.SuspendLayout()并 this.ResumeLayout()暫時掛起 UI 邏輯。在這種情況下,這將有助于提高性能。有關更多詳細資訊,請參閱https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.suspendlayout?view=windowsdesktop-5.0
如果您需要在添加控制元件后的某個階段洗掉它們,那么您有兩個選擇。
List<BaseIngredientPanel>在將控制元件添加到表單時,創建一個單獨的(也)存盤控制元件。這使用此串列通過this.Controls.Remove()方法查找和洗掉它們BaseIngredientPanel創建時給你一個唯一的名稱,并使用它通過this.Controls.Remove()方法查找和洗掉控制元件 。所有控制元件都已具有 Name 屬性,因此您可以使用它。
uj5u.com熱心網友回復:
如果要顯示 Form,則該類應從 Form 派生。你BaseIngredientPanel不是一個表單,它是一個用戶控制元件。
UserControls 類似于按鈕、組合框、選單、DataGridViews 等其他控制元件。通常您使用 Visual Studio 設計器在表單上顯示它們。在極少數情況下,您以編程方式執行此操作。
我創建了一個擴展面板的自定義用戶控制元件。
你真正想要的是什么:
- 我想創建一種特殊的面板,它的行為類似于標準面板,只有很小的差異。我的類的用戶可以使用我的特殊面板的幾乎所有屬性:派生自類面板
- 我想創建一種特殊的面板,它的行為類似于標準面板。我想控制我的特殊面板的用戶可以呼叫哪些方法: 創建一個派生自 UserControl 的類并在其上放置一個面板。
- 我想創建一個表單,我想在上面放置一個面板。這個面板有一些我只在這個表單上使用的行為。
如果要創建一種特殊型別的面板,即要在多個表單上重用的面板,請使用派生。無論您是從 Panel 還是 UserControl 派生,都取決于您希望您的類是多么愚蠢,以及您的類的用戶可以呼叫多少 Panel 方法。
如果您的特殊面板僅用于一個表單,請不要費心創建特殊的面板類。創建一個包含面板的表單。
在特殊表格上使用標準面板
如果您決定不必重用此面板的行為,您可以將它放在一個新表單上。使用 Visual Studio 設計器創建表單并在其上放置一個面板。訂閱您想要對其做出反應的面板事件。
如果您希望表單也有一個 Button,還可以使用 Visual Studio 設計器添加 Button 并訂閱 Button_Clicked 事件。
要顯示 PanelForm,您必須決定 PanelForm 是模型還是非模型。
- Modal:操作員只能使用這個Form,直到它關閉。他不能使用此應用程式的其他形式。這是迄今為止最常用的一種Form:在選單選擇上,或單擊按鈕:顯示PanelForm,使用PanelForm,關閉PanelForm,之后父表單可以讀取PanelForm的結果并采取相應的行動。模態對話框使用 Form.ShowDialog 顯示。
- 無模式:在顯示 PanelForm 時,操作員可以切換回父表單并與之互動。這不是經常使用。您可以使用它來顯示有關父視窗或應用程式狀態的一些額外資訊,例如滑鼠的位置或您將繪制的選定形狀。使用 Form.Show() 顯示無模式對話框。請記住,在關閉父表單之前,您必須關閉無模式對話框
顯示模態
一個很好的例子是 OpenFileDialog 表單
在您的父表單中:
using (var form = new OpenFileDialog())
{
// set properties of the OpenFileDialog before showing
form.InitialDirectory = this.GetInitialDirectory();
form.DefaultExt = ".txt";
...
// Show the Form as a modal box and wait until it is Closed
DialogResult dialogResult = form.ShowDialog(this);
// the operator has closed the form. Interpret the result:
if (dialogResult == DialogResult.OK)
{
string fileName = form.FileName();
this.OpenFile(fileName);
}
}
無模式:操作員可以切換回這種形式
private MyPanel PanelForm {get; set;} = null;
private void ShowMyPanel()
{
if (this.PanelForm != null) return; // panel already shown
this.PanelForm = new MyPanel();
// set properties:
this.PanelForm.DisplayedItems = ...
// show the PanelForm
this.PanelForm.Show();
}
操作員可以切換回此表單,并可能關閉此表單。在這種情況下,您必須關閉 PanelForm:
private void CloseMyPanel()
{
if (this.MyPanel != null)
{
this.MyPanel.Close();
this.MyPanel.Dispose();
this.MyPanel = null;
}
}
private void OnFormClosing(object sender, ...)
{
// if MyPanel is shown, is it allowed to Close this form AND myPanel
// or should you warn the operator to close MyPanel first?
if (this.MyPanel != null)
{
this.CloseMyPanel(); // close immediately
// or:
MessageBox.Show(..., "Please close Panelbox first");
}
}
當然,如果操作員關閉了面板箱,您也應該做出反應。在顯示面板框之前:
this.MyPane.Closed = OnMyPanelClosed;
private void OnMyPanelClosed(object sender, ...)
{
if (object.ReferenceEquals(sender, this.MyPanel)
{
// my panel is closed
this.MyPanel = null;
You can't dispose the Panel, because it is still being used. The Panel show Dispose itself when Closed.
Special Panel Class
If you have a Special Panel Class (derived from either Panel, or UserControl), then after compiling, the panel control is visible in the toolbox of visual studio designer.
You should use use visual studio designer to create a Form and put the panel on the Form:
class MyPanelForm : Form
{
public MyPanelForm()
{
InitializeComponent();
If you have used visual studio designer to add the special Panel, then you will find it in InitializeComponent. You can also add it yourself in the constructor. In that case, don't forget to add it to this.components, so your panel will be disposed when MyPanelForm is disposed.
To show MyPanelForm, use either ShowDialog or Show, as described in the previous section.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332913.html
上一篇:C#Process.Start打開外部exe的2個實體
下一篇:資料結構和演算法
