c# 一些控件使用方法

本文详细介绍了C#中ListBox控件的使用,包括属性如SelectionMode、Items、SelectedIndex等,以及添加、移除项的操作。同时,讲解了如何实现多选、联动列表及项的移位。此外,还讨论了DataGridView的用法,如通过DataSource和Add方法添加数据,单元格的编辑与读取,以及行的删除和新行的默认值设定。文章提供了丰富的示例代码,帮助开发者深入理解和应用这两个控件。
摘要由CSDN通过智能技术生成

ListBox:

1. 属性列表:

    SelectionMode    组件中条目的选择类型,即多选(Multiple)、单选(Single)
    Rows             列表框中显示总共多少行
    Selected         检测条目是否被选中
    SelectedItem     返回的类型是ListItem,获得列表框中被选择的条目
    Count            列表框中条目的总数
    SelectedIndex    列表框中被选择项的索引值
    Items            泛指列表框中的所有项,每一项的类型都是ListItem

2. 取列表框中被选中的值 

     ListBox.SelectedValue 

    ListBox.Text

3. 动态的添加列表框中的项:

     ListBox.Items.Add("所要添加的项");

4. 移出指定项:

     //首先判断列表框中的项是否大于0
     If(ListBox.Items.Count > 0 )
     {
//移出选择的项
ListBox.Items.Remove(ListBox.SelectedItem);
     }

5. 清空所有项:

     //首先判断列表框中的项是否大于0
     If(ListBox.Items.Count > 0 )
     {
//清空所有项
ListBox.Items.Clear();
     }

6. 列表框可以一次选择多项:
   
     只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选

7. 两个列表框联动,即两级联动菜单

     //判断第一个列表框中被选中的值
     switch(ListBox1.SelectValue)
     {
//如果是"A",第二个列表框中就添加这些:
case "A"
      ListBox2.Items.Clear();
      ListBox2.Items.Add("A1");
      ListBox2.Items.Add("A2");
      ListBox2.Items.Add("A3");
//如果是"B",第二个列表框中就添加这些:
case "B"
      ListBox2.Items.Clear();
      ListBox2.Items.Add("B1");
      ListBox2.Items.Add("B2");
      ListBox2.Items.Add("B3");
     }

8. 实现列表框中项的移位
     即:向上移位、向下移位
     具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。
     如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后
     把刚才新加入的对象的值,再附给当前选定项的前一项。
     具体代码为:
      //定义一个变量,作移位用
      index = -1;
      //将当前条目的文本以及值都保存到一个临时变量里面
      ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
      //被选中的项的值等于上一条或下一条的值
      ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
      //被选中的项的值等于上一条或下一条的值
      ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
      //把被选中项的前一条或下一条的值用临时变量中的取代
      ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
      //把被选中项的前一条或下一条的值用临时变量中的取代
      ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
      //把鼠标指针放到移动后的那项上
      ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;

9. 移动指针到指定位置:

      (1).移至首条
          //将被选中项的索引设置为0就OK了
          ListBox.SelectIndex=0;
      (2).移至尾条
          //将被选中项的索引设置为ListBox.Items.Count-1就OK了
          ListBox.SelectIndex=ListBox.Items.Count-1;
      (3).上一条
          //用当前被选中的索引去减 1
          ListBox.SelectIndex=ListBox.SelectIndex - 1;
      (4).下一条
          //用当前被选中的索引去加 1
          ListBox.SelectIndex=ListBox.SelectIndex + 1;

 

this.ListBox1.Items.Insertat(3,new   ListItem("插入在第3行之后项","")); 

this.ListBox1.Items.Insertat(index,ListItem)

ListBox1.Items.Insert(0,new   ListItem("text","value"));


DataGridView:

1、datagridview可以通过datasource赋值,但赋值后不可再通过add方法来添加新行,网上有人以bindingsource来中转datasource,但研究后貌似不可行,有待继续验证。可以通过datatable再一次赋值解决。

2、datagridview不用datasource添加数据的两种方法:

方法一:

int index=this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1"; 
this.dataGridView1.Rows[index].Cells[1].Value = "2"; 
this.dataGridView1.Rows[index].Cells[2].Value = "监听";

方法二:

DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
row.Cells.Add(comboxcell);
dataGridView1.Rows.Add(row);

方法二虽复杂,但可以添加button,label等控件。

3、  DataGridView 属性说明

① 取得或者修改当前单元格的内容 
② 设定单元格只读 
③ 不显示最下面的新行 
④ 判断新增行 
⑤ 行的用户删除操作的自定义 
⑥ 行、列的隐藏和删除 
⑦ 禁止列或者行的Resize 
⑧ 列宽和行高以及列头的高度和行头的宽度的自动调整 
⑨ 冻结列或行 
⑩ 列顺序的调整                                                 
? 行头列头的单元格
? 剪切板的操作 
? 单元格的ToolTip的设置 
? 右键菜单(ContextMenuStrip)的设置 
? 单元格的边框、 网格线样式的设定 
? 单元格表示值的设定 
? 用户输入时,单元格输入值的设定 
? 设定新加行的默认值

-------------------------------------------------------------------------------- 
① DataGridView 取得或者修改当前单元格的内容:

当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null) 

// 取得当前单元格内容 
Console.WriteLine(DataGridView1.CurrentCell.Value); 
// 取得当前单元格的列 Index 
Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex); 
// 取得当前单元格的行 Index 
Console.WriteLine(DataGridView1.CurrentCell.RowIndex); 

另外,使用 DataGridView.CurrentCellAddress 属性(而不是直接访问单元格)来确定单元格所在的
行: DataGridView.CurrentCellAddress.Y 
列: DataGridView.CurrentCellAddress.X 。这对于避免取消共享行的共享非常有用。 

当前的单元格可以通过设定 DataGridView 对象的 CurrentCell 来改变。可以通过 CurrentCell 来设定 
DataGridView 的激活单元格。将 CurrentCell 设为 Nothing(null) 可以取消激活的单元格。 
-------------------------------------------------------------------------------- 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值