C# 如何基于数字值隐藏文本框?

C# 如何基于数字值隐藏文本框?,c#,C#,我有一个表单,让用户选择客人的数量。我想隐藏第二个表单中不需要的额外文本框 示例用户在form2上的form1上选择8位来宾我想隐藏textbox9和textbox10,这样他们只会看到他们需要填写来宾姓名的文本框1-8 在VisualStudio中使用WindowsFormsC实现这一点的最佳方法是什么 这里有一个例子,但它似乎非常重复 private void DisplayTextBoxs() { if (xBillInformationForm.dGuestNum

我有一个表单,让用户选择客人的数量。我想隐藏第二个表单中不需要的额外文本框

示例用户在form2上的form1上选择8位来宾我想隐藏textbox9和textbox10,这样他们只会看到他们需要填写来宾姓名的文本框1-8

在VisualStudio中使用WindowsFormsC实现这一点的最佳方法是什么

这里有一个例子,但它似乎非常重复

private void DisplayTextBoxs()
    {
        if (xBillInformationForm.dGuestNumber == 1)
        {
            xCustomer1Label.Visible = true;
            xCustomer1TextBox.Visible = true;
        }
        if (xBillInformationForm.dGuestNumber == 2)
        {
            xCustomer1Label.Visible = true;
            xCustomer1TextBox.Visible = true;
            xCustomer2Label.Visible = true;
            xCustomer2TextBox.Visible = true;
        }
        if (xBillInformationForm.dGuestNumber == 3)
        {
            xCustomer1Label.Visible = true;
            xCustomer1TextBox.Visible = true;
            xCustomer2Label.Visible = true;
            xCustomer2TextBox.Visible = true;
            xCustomer3Label.Visible = true;
            xCustomer3TextBox.Visible = true;
        }
        if (xBillInformationForm.dGuestNumber == 4)
        {
            xCustomer1Label.Visible = true;
            xCustomer1TextBox.Visible = true;
            xCustomer2Label.Visible = true;
            xCustomer2TextBox.Visible = true;
            xCustomer3Label.Visible = true;
            xCustomer3TextBox.Visible = true;
            xCustomer4Label.Visible = true;
            xCustomer4TextBox.Visible = true;
        }
    }

对于人数有限的客人,您可以

Textbox2.Visible = userChoice >= 2;
Textbox3.Visible = userChoice >= 3;
等等


对于大量的来宾/文本框,这将变得非常笨拙。

如果动态构建表单,您可以将所有文本框放入一个数组中,并在其中循环。但是我们不能给出一个好的答案,除非你发布你的代码

TextBow[10]textboxes;
textBoxes[0] = your first textbox
textBoxes[1] = you second textbox

For(int i = userChoice -1; i < textBoxes.Length; i++){
   textBoxes[i].Visible = false;
}
因此,我的代码从userChoice开始,遍历数组中的所有文本框

var quantity = convert.ToInt32(xBillInformationForm.dGuestNumber);

<table>
    for(int i=1, i<=quantity; i++)
    {
        <tr>
            <td>
                <label id="name">Name</label>
                <input id="address" type="text" placeholder="address" />
            </td>
        </tr>        
    }
</table>

此外,您还可以在第二个表单中添加两个按钮或链接,如AddGuest和RemoveGuest,并通过jquery进行处理

难道您不能只绘制jquery中需要的文本框吗?forint i=0;我有选择;i++{用id=textbox'+i;}绘制一个textbox;}我可能应该澄清一下。这是用visual studio用C编写的。请使用Post代码…必须用C编写。我知道我可以设置textbox10.visible=false这样的文本框;只是不确定它的逻辑部分。要根据用户选择的数字动态创建文本框吗?这可能是最简单的,因为它只有10个文本框。我还没有任何特定的代码,因为我仍在尝试围绕它。我已经提出了一些代码,但它似乎非常重复。我的回答确保您不需要任何重复。这是一个Windows窗体应用程序桌面,而不是ASP.Net MVC。这仅用于循环。你可以在任何地方使用。我的意思是c编码逻辑对所有应用程序都是一样的,比如MVC、Asp.Net或windows窗体应用程序。您只需要更改语法并在任何地方实现此逻辑。