2016-05-31 86 views
-4

我有一個用戶選擇客人人數的表單。我想隱藏第二種形式中不需要的額外文本框。如何根據數字值隱藏文本框?

示例用戶在form2上的form1上選擇8個guest虛擬機我想隱藏textbox9和textbox10,以便僅顯示他們需要填寫guest虛擬機名稱的文本框1-8。

在Visual Studio中使用Windows Forms C#完成此操作的最佳方法是什麼?

下面是一個例子,但它似乎很重複

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; 
     } 
    } 
+0

難道你不只是繪製jquery所需的文本框?對於(int i = 0; i jelleB

+0

我可能應該澄清一下,這是使用visual studio編寫的C# – yaksushi

+0

在htmlWriter ? – jelleB

回答

0

爲客人提供一個有限的最大數量,你可以做

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

對於大量的客人/ textboxes,這變得笨拙。

+0

這可能是最簡單的,因爲它只有10個文本框 – yaksushi

0

如果您動態構建表單,則可以將所有文本框放入數組中並循環。但除非你文章中,我們不能給出一個滿意的答案代碼

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開始,經過所有的文本框數組中

+0

對不起,我沒有任何具體的代碼爲此,但我仍然試圖圍繞它,我已經擺出了S ome代碼,但它似乎是非常重複的 – yaksushi

+0

我的答案確保您不需要任何重複 – jelleB

0

嘗試循環

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> 

進一步,你可以添加兩個按鈕或鏈接像第二種形式的「添加客人」和「刪除客人」,並通過jquery處理它

+0

這是一個Windows Forms應用程序(桌面)而不是ASP.Net MVC。 –

+0

這只是** for循環**而已。你可以使用任何地方。我的意思是編碼的邏輯是相同的所有即MVC,Asp.Net或Windows窗體應用程序。你只需要改變你的語法並在你想要的任何地方實現這個邏輯。 – Riddhi