c#控件

来源:互联网 发布:冒险岛资源提取器源码 编辑:程序博客网 时间:2024/05/25 13:34

1、程序添加控件
        

            TextBox myText = new TextBox();
            myText.Location = new Point(25,25);
            myText.Text = "textbox ";
            Label label = new Label();
            label.Width = 400;
            label.Text = "welcome to the label";
            this.Controls.Add(myText);
            this.Controls.Add(label);

 

 

 

2、acceptbutton

acceptbutton 是指当按下Enter键,就会激发button1按钮的click事件,与点击按钮的结果是一样的

 this.AcceptButton= button1;

 

 

3、CancelButton

cancelbutton按钮,通过设置CancelButton属性,可以设置窗体的“取消”按钮。如果设置该属性,则按下“ESC”键就相当于点击了该按钮

this.CancelButton = button1;

 

4、密码框

            //自己手动设置密码的显示符号

            TextBox myText = new TextBox();
            myText.PasswordChar='@';
            //设置文本框的UseSystemPasswordChar属性为true 系统默认的为*
            //myText.UseSystemPasswordChar = true;

5、创建只读文本:

//创建只读文本框
            TextBox myText = new TextBox();
            myText.Text = "用一生来下载你";
            myText.ReadOnly = true;
            this.Controls.Add(myText);
           

 

6、设置多行文本框

//设置多行文本框
            myText.Multiline = true;
            myText.Height = 50;
            myText.Text = "昨夜星辰昨夜风,画楼西畔桂堂东,身无彩凤双飞翼,心有灵犀一点通";
            myText.SelectionStart = 1;
            myText.SelectionLength = 5;
            this.Controls.Add(myText);

 

7、Listbox

private void button1_Click(object sender, EventArgs e) // 添加按钮
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入要添加的数据");
            }
            else
            {
                listBox1.Items.Add(textBox1.Text);//textBox1文本框
                textBox1.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)//删除按钮
        {
            if (listBox1.SelectedItems.Count == 0)
            {
                MessageBox.Show("选择要删除的项目");
            }
            else
            {
                listBox1.Items.Remove(listBox1.SelectedItem);  
            }
        }

 

 

 8、tab 选项卡控件

 

 

 private void Form1_Load(object sender, EventArgs e)
        {

           //拖进个控件imagelist,然后在属性中image中选择选项卡的图片
            tabControl1.ImageList = imageList1;
            tabPage1.ImageIndex = 0;
            tabPage1.Text = "选项卡1";
            tabPage2.ImageIndex = 1;
            tabPage2.Text = "选项卡2";
            //选项卡三维按钮
            tabControl1.Appearance =TabAppearance.Buttons ;

            //在选项卡中添加按钮
            Button btn1 = new Button();
            btn1.Text = "新增按钮";
            tabPage1.Controls.Add(btn1);
           
        }
        //添加选项卡
        private void button1_Click(object sender, EventArgs e)
        {
           
            string Titile = "新增选项卡" + (tabControl1.TabCount + 1).ToString();
            TabPage newtabpage = new TabPage(Titile);
            tabControl1.TabPages.Add(newtabpage);
        }
        //删除选项卡
        private void button2_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                MessageBox.Show("请选择要删除的选项卡");
            }
            else
            {
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
        }

 

原创粉丝点击