2017-11-22 6 views
-2

コンボボックスで選択されたトラック数が表示されているときに、テキストボックスとボタンを表示しようとしています。コンボボックスで選択されたトラックの数が増えると、テキストボックスとボタンが表示される

たとえば、3を選択すると、トラックを選択する3つのテキストボックスと3つのボタンが有効になります。このコードを単純な0​​またはforに変更するにはどうすればよいですか?

if (numero_faixas == 1) { 
    txtFaixa1.Visible = true; 
    btnFaixa1.Visible = true; 
} else if (numero_faixas == 2) { 
    txtFaixa1.Visible = true; 
    btnFaixa1.Visible = true; 
    txtFaixa2.Visible = true; 
    btnFaixa2.Visible = true; 
} else if (numero_faixas == 3) { 
    txtFaixa1.Visible = true; 
    btnFaixa1.Visible = true; 
    txtFaixa2.Visible = true; 
    btnFaixa2.Visible = true; 
    txtFaixa3.Visible = true; 
    btnFaixa3.Visible = true; 
} 
+1

'Controls'クラスでgoogle検索をやってみてください。また、ループを通ってループする方法もあります。 – MethodMan

+0

Ayy lmao DAT CODE !!さらに10個のテキストフィールドとボタンがある場合は、1000行を追加しますか? –

答えて

-1

これは、ビジネスルールを使用して再スムーズにすることができる概念の証明です。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace ShowHideButtons_47439046 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 

      InitOurThings(); 
     } 

     private void InitOurThings() 
     { 
      //lets create a combo box with options to select 
      ComboBox combo = new ComboBox(); 
      combo.Location = new Point(5, 5);//place it somewhere 

      //add selectable items 
      for (int i = 0; i < 10; i++) 
      { 
       combo.Items.Add(i); 
      } 

      combo.SelectedValueChanged += Combo_SelectedValueChanged;//the event which will handle the showing/hidding 
      Controls.Add(combo);//add the combo box to the form 


      //lets create some buttons and textboxes 
      int btnx = 5; 
      int btny = combo.Height + combo.Location.Y + 5; 
      for (int i = 0; i < 10; i++) 
      { 
       Button btn = new Button(); 
       btn.Location = new Point(btnx, btny); 
       btn.Name = i.ToString(); 
       btn.Text = i.ToString(); 
       Controls.Add(btn); 
       btny += btn.Height + 5; 

       TextBox txtbx = new TextBox(); 
       txtbx.Location = new Point(btn.Location.X + btn.Width + 5, btn.Location.Y); 
       txtbx.Name = i.ToString(); 
       txtbx.Text = i.ToString(); 
       Controls.Add(txtbx); 
      } 

     } 

     private void Combo_SelectedValueChanged(object sender, EventArgs e) 
     { 

      int selectedValue = int.Parse(((ComboBox)sender).SelectedItem.ToString()); 

      foreach (Control item in Controls) 
      { 
       //show/hide the controls based on their Name being Equal Or Smaller than the selectedItem 
       if (item is TextBox) 
       { 
        int itemNumber = int.Parse(item.Name); 
        item.Visible = itemNumber <= selectedValue ? true : false; 
       } 
       if (item is Button) 
       { 
        int itemNumber = int.Parse(item.Name); 
        item.Visible = itemNumber <= selectedValue ? true : false; 
       } 
      } 
     } 
    } 
} 
+0

あまりにも多くのコード、これはより効率的な方法で行うことができますIMO – MethodMan

-1

あなたは同じコントロールを参照するために何度もありませんので、あなたの条件を変更することで、コードの行数を減らすことができます。foreachループを使用するには

if (numero_faixas > 0) 
{ 
    txtFaixa1.Visible = true; 
    btnFaixa1.Visible = true; 
} 
if (numero_faixas > 1) 
{ 
    txtFaixa2.Visible = true; 
    btnFaixa2.Visible = true; 
} 
if (numero_faixas > 2) 
{ 
    txtFaixa3.Visible = true; 
    btnFaixa3.Visible = true; 
} 

を、あなたが唱えられますIEnumerable<Control>using System.Linq;のコレクションにとButtonのコントロールをフィルタリングできます。コントロール名には"Faxia"が含まれています。次に、ループ本体に、我々はintにコントロール名の最後の文字を変換しようとするint.TryParseを使用することができ、そしてそれが成功した場合に管理番号が未満numero_faixas + 1であれば、その後、Visibleに制御を設定します。

foreach (Control control in Controls.Cast<Control>() 
    .Where(c => (c is Button || c is TextBox) && c.Name.Contains("Faixa"))) 
{ 
    // Get the number associated with this control and compare it to numero_faixas 
    int controlNumber; 
    if (int.TryParse(control.Name.Substring(control.Name.Length - 1), out controlNumber) && 
     controlNumber < numero_faixas + 1) 
    { 
     control.Visible = true; 
    } 
} 
関連する問題