在Winform窗体中有50按钮,分别叫做:button_t1、button_t2~button_49、button_50,这五十按钮的初始状态是“Visible=fase”。现在要实现的功能是,“i”是一个1~50的随机数,当“i=1”的时候,button_t1显示,当“i=35”的时候button_t35显示,已经显出的按钮不必在隐藏。"i"等于几,对应的按钮就显示第几号按钮请问这样的效果应该怎样实现?

解决方案 »

  1.   

    什么程序要用五十个按钮来实现?你的要求很容易满足(字典或数组里查找"button_t"+i.tostring()就行了),但程序不能这样去写。
      

  2.   


    private void ShowBtn(int i)
    {
                foreach (Control ctl in this.Controls)
                {
                    if (ctl is Button)
                    {
                        Button btn = ctl as Button;
                       if(i==btn.Name.SubString(btnName.Length-1,2))
                       {
                           btn.Visable=true;
                           break;
                       }
                       else if(i==btn.Name.SubString(btnName.Length-1,1))
                       {
                           btn.Visable=true;
                           break;
                       }                }
                }}
      

  3.   

    Button btn = this.Controls.Find("button_" + i.ToString())[0];
    btn.Visible = true;
      

  4.   


    “ this.Controls.Find("button_t"+questions_num.ToString())[0]”这一句提示错误:错误 1 “Find”方法没有采用“1”个参数的重载
    C:\Users\Administrator\Desktop\CS_Colliery\CS_Colliery\CS_ExamOnline\Main.cs 174 26 CS_ExamOnline
      

  5.   

    记错了根据提示自己加一下嘛this.Controls.Find("button_t"+questions_num.ToString(), false)[0]
      

  6.   


    this.Controls代表什么?是WInform中所有的控件集合吗?怎么提示“超出索引”?
    Button btn = (Button)this.Controls.Find("button_t" + questions_num.ToString(),false)[0];
      

  7.   

    你的Button都加到哪个父控件里了?如果在Form上,直接用this.Controls
    如果在Panel里用 this.Panel1.Controls 因为用Find返回的是Control数组。如果没有找到,就会报你现在这个错误。
      

  8.   

    比如:
    Button btn = new Button();
    btn.Name = "btn1";
    this.Controls.Add(btn);Button btn1 = this.Controls.Find("btn1", false)[0];
    MessageBox.Show(btn1.Name);
      

  9.   


    Form上的“GropBox”里面啊
      

  10.   

    那你就换成 this.GroupBox.Controls.Find 嘛举一反三下嘛谁是button的父控件,就用哪个的.Controls.Find
      

  11.   

    fangxinggood好有耐心   我赞成