【发布时间】:2021-10-18 04:09:19
【问题描述】:
我有一个 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 个面板,而设计器只会尝试将它们像俄罗斯娃娃一样装箱。
【问题讨论】:
-
您需要将它添加到父控件的集合中。或者设置它的 Parent 属性。不需要 Show,因为 visible 默认为 true。
-
现在,如果你想隐藏它,你可以这样做,然后根据需要再次显示..