簡體   English   中英

計算表單中TextBox和CheckBoxes的數量

[英]Count the number of TextBoxes and CheckBoxes in a form

我的要求是當用戶點擊'btnGetCount'時,直接在表單內部使用id="form1"計算TextBox和CheckBox的總數。 這是我嘗試過的代碼,盡管我在表單中有三個TextBoxes和兩個CheckBoxes,但它不計數任何內容,計數器仍為零。 但是,如果我刪除foreach循環並傳遞TextBox control = new TextBox(); 而不是當前代碼然后它計算第一個TextBox和countTB返回值為一。

protected void btnGetCount_Click(object sender, EventArgs e)
    {
        Control control = new Control();

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in this.Controls)
        {
            if (control.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (control is TextBox)
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    }

您必須遞歸遍歷其他控件。

    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    <asp:CheckBox ID="cb" runat="server"></asp:CheckBox>
    </div>
    </form>

protected void Page_Load(object sender, EventArgs e)
{
    var controls = form1.Controls;
    var tbCount = 0;
    var cbCount = 0;
    CountControls(ref tbCount, controls, ref cbCount);

    Response.Write(tbCount);
    Response.Write(cbCount);
}

private static void CountControls(ref int tbCount, ControlCollection controls, ref int cbCount)
{
    foreach (Control wc in controls)
    {
        if (wc is TextBox)
            tbCount++;
        else if (wc is CheckBox)
            cbCount++;
        else if(wc.Controls.Count > 0)
            CountControls(ref tbCount, wc.Controls, ref cbCount);
    }
}

它允許給你零,因為你計算控制控件的類型是不可用的,將你的代碼更改為:

protected void btnGetCount_Click(object sender, EventArgs e)
    {

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (c.GetType()== typeof(TextBox))
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    } 

我相信你必須是遞歸的, this.Controls只會返回它的直接子this.Controls的控件。 如果TextBox位於其中的控件組內,則還需要查看容器控件。

請參見以下其他stackoverflow的答案: 如何獲取特定類型(按鈕/文本框)的Windows窗體表單的所有子控件?

編輯:我意識到答案是WinForms,但解決方案仍然適用。

只需對Alyafey,pcnThird和Valamas建議的代碼做一些小的改動,我就可以編寫出此代碼。

protected void btnGetCount_Click(object sender, EventArgs e)
    {

        int countCB = 0;
        int countTB = 0;
        foreach (Control c in form1.Controls) //here is the minor change
        {
            if (c.GetType() == typeof(CheckBox))
            {
                countCB++;
            }
            else if (c.GetType()== typeof(TextBox))
            {
                countTB++;
            }
        }

        Response.Write("No of TextBoxes: " + countTB);
        Response.Write("<br>");
        Response.Write("No of CheckBoxes: " + countCB);
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM