1)在Visual studio中新建一个WindowsApplication。在Form1上布置两个控件textBox1和button1,如下图
2)后台代码
using Systemusing System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Linq
using System.Text
using System.Windows.Forms
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// 定义一个checkbox
CheckBox chkBox
public Form1()
{
InitializeComponent()
}
// 点击button1
private void button1_Click(object sender, EventArgs e)
{
if (chkBox != null) return
// 如果textBox1没有内容输入则退出
if(textBox1.Text.Trim()==string.Empty) return
// 动态加载一个Checkbox
chkBox = new CheckBox()
chkBox.Left = 10
chkBox.Top = 10
// 将textBox1输入内容作为checkbox的提示
chkBox.Text = textBox1.Text
// 点击checkbox时的事件处理
chkBox.Click += chkBox_Click
this.Controls.Add(chkBox)
}
void chkBox_Click(object sender, EventArgs e)
{
if (chkBox.Checked)
{
MessageBox.Show(chkBox.Text + "被选中!")
}
else
{
MessageBox.Show(chkBox.Text + "没有选中!")
}
}
}
}
#region[加入选择列]DataGridViewCheckBoxColumn dgv1 = new DataGridViewCheckBoxColumn()
dgv1.Name = "选择"
dataGridView1.Columns.Insert(0, dgv1)
dataGridView1.MultiSelect = true
dgv1.InheritedStyle.Alignment = DataGridViewContentAlignment.TopCenter
dgv1.Width = 35
this.dataGridView1.VirtualMode = false
for (int i = 0i <this.dataGridView1.Columns.Counti++)
{
if (i == 0)
{
this.dataGridView1.Columns[i].ReadOnly = false
}
else
{
this.dataGridView1.Columns[i].ReadOnly = true
}
}
#endregion
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)