帮看下,C#里的Button不能用变量识别Button[1],Button[2],Button[i]吗?

oumiga111 2017-08-09 08:30:02

问题:Button[i] 怎么表示 ???
 
if (!theoplevel.ToString().Contains(btnitem1.Text.ToString())) btnitem1.Visible = false;
if (!theoplevel.ToString().Contains(btnitem2.Text.ToString())) btnitem2.Visible = false;
if (!theoplevel.ToString().Contains(btnitem3.Text.ToString())) btnitem3.Visible = false;
if (!theoplevel.ToString().Contains(btnitem4.Text.ToString())) btnitem4.Visible = false;
if (!theoplevel.ToString().Contains(btnitem5.Text.ToString())) btnitem5.Visible = false;


转换成


for (int i = 1; i <= 5; i++)
{
if (!theoplevel.ToString().Contains(btnitem[i].Text.ToString())) btnitem[i].Visible = false;
}

...全文
646 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yizuoshan 2019-12-04
  • 打赏
  • 举报
回复
我是菜鸟过来看能不能学点啥,发现啥都看不懂o(╥﹏╥)o
EIT王子 2017-08-10
  • 打赏
  • 举报
回复
   string theoplevel = "button1button2button4";
            string name = "button";
            for (int i = 1; i <= 5; i++)
            {
                string fullname = string.Format("{0}{1}", name, i);
                //判断数据库的字符串里是不是包含这个字串
                if (theoplevel.Contains(fullname))
                {
                    //这个this是窗体容器,如果你是面板要改成你对应的面板
                    //这个Find的参数是控件的Name不是Text啊。也不是控件的变量名啊。
                    Control[] cs = this.Controls.Find(fullname, false);
                    if (cs.Count() > 0)
                    {
                        cs[0].Visible = false;
                    }

                }
               
            }
dy00544 2017-08-10
  • 打赏
  • 举报
回复
啥? 你把控件信息存到数据库? 然后从数据库里面提取信息来创建界面? 666 服了气了。
乱臣贼子 2017-08-10
  • 打赏
  • 举报
回复
楼主是初学者,鉴定完毕!! 兄弟,你可以这样做,找到所有Button的父级控件,然后循环这个父级控件的子集,找到子集中的所有Button,然后再设置Visible就行了。
巴士上的邂逅 2017-08-10
  • 打赏
  • 举报
回复
var list=new List<BUtton>(){btnitem1,btnitem2,btnitem3,btnitem4,btnitem5};
list.ForEach(btn=>btn.Visible=!theoplevel.COntains(btn.Text));
Hobo丶 2017-08-10
  • 打赏
  • 举报
回复
我楼上的可以试一下 我觉得是最直接的遍历
xuzuning 2017-08-10
  • 打赏
  • 举报
回复
foreach(var c in Controls) c.Visible= theoplevel.Contains(c.Text);
oumiga111 2017-08-10
  • 打赏
  • 举报
回复
更正:theoplevel.COntains(bntitem[i].Text)目的就是想看看当前用户的theoplevel包含这5个按钮.Text的哪几个,包含的就代表有这个权限则Visible=True否则为False
oumiga111 2017-08-10
  • 打赏
  • 举报
回复
我知道这个theoplevel还可以用 0,1,2,3,4,5,6,7, 这样来表示,我以前就是这样做的,感觉易读性和维护性不好, 如果有其他关于根据权限分级,按钮显示的案例希望能推荐一二,谢谢
oumiga111 2017-08-10
  • 打赏
  • 举报
回复
首先感谢C#前辈指导 ,本人确实初学者,算上今天第9天, 我有必要把思路说清一下,方便大家帮助, theoplevel变量,是字符串,是某个角色拥有的权限,比如:普通用户的theoplevel字段内容是:会员管理,数据录入,数据查询 而管理员的theoplevel是:会员管理,数据录入,数据查询,报表查询,管理员管理,角色管理 我现在有5个按钮,name分别是bntitem1 bntitem2 bntitem3 bntitem4 bntitem5 bntitem1.Text="会员管理" bntitem2.Text="数据录入" bntitem3.Text="数据查询" bntitem4.Text="管理员管理" bntitem5.Text="角色管理" theoplevel.Controls(bntitem[i].Text)目的就是想看看当前用户的theoplevel包含这5个按钮.Text的哪几个,包含的就代表有这个权限则Visible=True否则为False 我想用循环FOR 解决此问题,而不是每个按钮一行代码,象下面这样 if (!theoplevel.ToString().Contains(btnitem4.Text.ToString())) btnitem4.Visible = false; if (!theoplevel.ToString().Contains(btnitem5.Text.ToString())) btnitem5.Visible = false; 再次感谢大家,本着C#精神给的帮助和讨论,其实效果我用上面代码能实现,只是感觉拉杂,虽然我是初学第9天,但我有良好的书写习惯
大米粥哥哥 2017-08-09
  • 打赏
  • 举报
回复
1.把button的名字都存到一个数组里 名字得一个一个加到数组里 2.试试foreach
oumiga111 2017-08-09
  • 打赏
  • 举报
回复
引用 7 楼 eit520 的回复:
theoplevel是什么鬼。我看你转成了字符串
是数据库里获取的 字符串, 我是想看一下,这个theoplevel字符串里是否包含了 按钮的TEXT, 如果有,设置按钮的visible 你的方法我试了,在this.Controls 里Find不到fullname的控件,但我确定有此控件名
EIT王子 2017-08-09
  • 打赏
  • 举报
回复
theoplevel是什么鬼。我看你转成了字符串
EIT王子 2017-08-09
  • 打赏
  • 举报
回复
string name = "button";
            for (int i = 1; i <= 5; i++)
            {
                string fullname = string.Format("{0}{1}", name, i);
                Control[] cs = this.Controls.Find(fullname, false);
                if (cs.Count()>0)
                {
                    cs[0].Visible = false;
                }
            }
            
EIT王子 2017-08-09
  • 打赏
  • 举报
回复
引用 4 楼 oumigai 的回复:
那托出来的5个Button控件,不能用1维数组命名吗? Button[1] Button[1]
不能。你只能用findcontroller去找
oumiga111 2017-08-09
  • 打赏
  • 举报
回复
那托出来的5个Button控件,不能用1维数组命名吗? Button[1] Button[1]
EIT王子 2017-08-09
  • 打赏
  • 举报
回复
还有就是你这好像有问题吧。你是直接拖过去的控件想要直接用[i]把I加在名称后面啊?那是不行的
EIT王子 2017-08-09
  • 打赏
  • 举报
回复
可以转换。你要强制转换下。
prk_2015 2017-08-09
  • 打赏
  • 举报
回复
你得弄一个Button[]数组才可以吧
C#.net实现学生成绩管理系统 namespace 学生成绩管理系统 { partial class Formlogin { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.labeluser = new System.Windows.Forms.Label(); this.textBoxuser = new System.Windows.Forms.TextBox(); this.labelcode = new System.Windows.Forms.Label(); this.textBoxcode = new System.Windows.Forms.TextBox(); this.buttonin = new System.Windows.Forms.Button(); this.buttonout = new System.Windows.Forms.Button(); this.SuspendLayout(); // // labeluser // this.labeluser.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labeluser.Location = new System.Drawing.Point(31, 50); this.labeluser.Name = "labeluser"; this.labeluser.Size = new System.Drawing.Size(55, 26); this.labeluser.TabIndex = 0; this.labeluser.Text = "用户名"; this.labeluser.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // textBoxuser // this.textBoxuser.Location = new System.Drawing.Point(126, 55); this.textBoxuser.Name = "textBoxuser"; this.textBoxuser.Size = new System.Drawing.Size(112, 21); this.textBoxuser.TabIndex = 1; // // labelcode // this.labelcode.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.labelcode.Location = new System.Drawing.Point(31, 124); this.labelcode.Name = "labelcode"; this.labelcode.Size = new System.Drawing.Size(55, 23); this.labelcode.TabIndex = 2; this.labelcode.Text = "密码"; this.labelcode.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // textBoxcode // this.textBoxcode.Location = new System.Drawing.Point(126, 124); this.textBoxcode.Name = "textBoxcode"; this.textBoxcode.PasswordChar = '*'; this.textBoxcode.Size = new System.Drawing.Size(112, 21); this.textBoxcode.TabIndex = 3; // // buttonin // this.buttonin.Location = new System.Drawing.Point(34, 200); this.buttonin.Name = "buttonin"; this.buttonin.Size = new System.Drawing.Size(75, 23); this.buttonin.TabIndex = 4; this.buttonin.Text = "登陆"; this.buttonin.UseVisualStyleBackColor = true; this.buttonin.Click += new System.EventHandler(this.buttonin_Click); // // buttonout // this.buttonout.Location = new System.Drawing.Point(163, 200); this.buttonout.Name = "buttonout"; this.buttonout.Size = new

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧