gpt4 book ai didi

C# - 如何通过选中复选框以编程方式创建文本框?

转载 作者:行者123 更新时间:2023-11-30 15:53:09 25 4
gpt4 key购买 nike

我有一个 Form 和一个 Button。当我单击 Button 时,然后以编程方式创建一个带有一个 CheckBox 和一个 TextBoxPanel;但对于 TextBoxVisiblefalse。如果我选中了 CheckBox,我想将我的 TextBox 更改为 Visible = true。任何人都可以帮助我吗?

public void CreateSlide(string name, string title, string desc) 
{
var PanelOrder = new Panel()
{
Name = name,
Size = new Size(395, 33),
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(203, 157)
};

var ckOrder = new CheckBox()
{
Name = name,
Text = "Order",
Size = new Size(102, 21),
Location = new Point(3, 5),
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 10, FontStyle.Bold)
};

ckOrder.CheckedChanged += new EventHandler(this.ckBoxOrder_CheckedChanged);

var TxtQty = new TextBox
{
Name = name,
Text = "1",
Visible = false,
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(100, 25),
Location = new Point(290, 3)
};

PanelOrder.Controls.Add(ckOrder);
PanelOrder.Controls.Add(TxtQty);
}

相关的事件处理器

private void ckBoxOrder_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked == true)
{
// ??? TxtQty.Visible = true; // <- doesn't compile
}
else
{
// ??? TxtQty.Visible = false; // <- doesn't compile
}
}

最佳答案

您可以尝试使用 lambda 以将所有相关代码保存在 CreateSlide 中:

public void CreateSlide(string name, string title, string desc) {
var PanelOrder = new Panel() {
Name = name,
Size = new Size(395, 33),
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(203, 157),
Parent = this // <- Put PanelOrder panel on the form
};

var ckOrder = new CheckBox() {
Name = name,
Text = "Order",
Size = new Size(102, 21),
Location = new Point(3, 5),
FlatStyle = FlatStyle.Flat,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
Parent = PanelOrder // <- Put ckOrder on the PanelOrder panel
};

var TxtQty = new TextBox() {
Name = name,
Text = "1",
Visible = false,
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(100, 25),
Location = new Point(290, 3),
Parent = PanelOrder // <- Put TxtQty on the PanelOrder panel
};

// lambda function
ckOrder.CheckedChanged += (s, e) => {
TxtQty.Visible = ckOrder.Checked;
};
}

关于C# - 如何通过选中复选框以编程方式创建文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com