[Winform入门教程]各基础控件委托使用

一、DataGridView控件委托

  1.更新指定单元格值

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate void UpdateDataGridViewCellValueDelegate(DataGridView dgv, int rowIndex, int colIndex, string value);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private void SafeUpdateCellValueToDataGridViewFunc(DataGridView dgv, int rowIndex, int colIndex, string value)
{
    if (dgv.InvokeRequired)
    {
        dgv.Invoke(new UpdateDataGridViewCellValueDelegate(SafeUpdateCellValueToDataGridViewFunc), new object[] { dgv, rowIndex, colIndex, value });
    }
    else
    {
        dgv.Rows[rowIndex].Cells[colIndex].Value = value;
    }
}

//3.实例化委托
private UpdateDataGridViewCellValueDelegate UpdateDataGridViewCellValue;

//4.将方法绑定到委托对象
UpdateDataGridViewCellValue = new UpdateDataGridViewCellValueDelegate(SafeUpdateCellValueToDataGridView);

//5.调用委托
UpdateDataGridViewCellValue(this.dataGridView1, 2, 11, "已完成");

  2.获取指定单元格值

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate string GetDataGridViewCellValueDelegate(DataGridView dgv, int rowIndex, int colIndex);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private string SafeGetCellValueFromDataGridViewFunc(DataGridView dgv, int rowIndex, int colIndex)
{
    if (dgv.InvokeRequired)
    {
        return (string)dgv.Invoke(new GetDataGridViewCellValueDelegate(SafeGetCellValueFromDataGridViewFunc), dgv, rowIndex, colIndex);
    }
    else
    {
        return dgv.Rows[rowIndex].Cells[colIndex].Value?.ToString();
    }
}

//3.实例化委托
private GetDataGridViewCellValueDelegate GetCellValueFromDataGridView;

//4.将方法绑定到委托对象
GetCellValueFromDataGridView = new GetDataGridViewCellValueDelegate(SafeGetCellValueFromDataGridViewFunc);

//5.调用委托
GetCellValueFromDataGridView(this.dataGridView1, 2, 11);

 

二、RichTextBox控件委托

1.将字符串追加到RichTextBox控件的内容

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate void AppendTextToRichTextBoxDelegate(string text);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private void SafeAppendTextToRichTextBoxFunc(string text)
{
    if (this.richTextBox1.InvokeRequired)
    {
        this.richTextBox1.Invoke(new AppendTextToRichTextBoxDelegate(SafeAppendTextToRichTextBoxFunc), text);
    }
    else
    {
        this.richTextBox1.AppendText($"{text}\n");
    }
}

//3.实例化委托
private AppendTextToRichTextBoxDelegate AppendTextToRichTextBox;

//4.将方法绑定到委托对象
AppendTextToRichTextBox = new AppendTextToRichTextBoxDelegate(SafeAppendTextToRichTextBoxFunc);

//5.调用委托
AppendTextToRichTextBox($"线程{thread_index}已完成");

 

三、CheckBox控件委托

1.获取CheckBox控件的选中状态

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate bool GetCheckboxStateDelegate(CheckBox cb);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private bool SafeGetCheckBoxStateFunc(CheckBox cb)
{
    if(cb.InvokeRequired)
    {
        return (bool)cb.Invoke(new GetCheckboxStateDelegate(SafeGetCheckBoxStateFunc), cb);
    }
    else
    {
        return cb.Checked;
    }
}

//3.调用委托
SafeGetCheckBoxStateFunc(this.checkbox1);

2.设置CheckBox控件的选中状态

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate bool SetCheckboxStateDelegate(CheckBox cb, bool isChecked);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private bool SafeSetCheckBoxStateFunc(CheckBox cb, bool isChecked)
{
    if(cb.InvokeRequired)
    {
        cb.Invoke(new SetCheckboxStateDelegate(SafeSetCheckBoxStateFunc), cb, isChecked);
    }
    else
    {
        cb.Checked = isChecked;
    }
}

//3.调用委托
SafeSetCheckBoxStateFunc(this.checkbox1, true);

 

四、 ListView控件委托

1.获取指定单元格值

//1.声明委托类型,定义委托的签名(返回类型和参数列表)
private delegate string GetCellValueFromListViewDelegate(ListView lv, int rowIndex, int colIndex);

//2.定义匹配方法,创建与委托签名匹配的方法(静态或实例方法)
private string SafeGetCellValueFromListViewFunc(ListView lv, int rowIndex, int colIndex)
{
    if(lv.InvokeRequired)
    {
        return (string)lv.Invoke(new GetCellValueFromListViewDelegate(SafeGetCellValueFromListViewFunc), lv, rowIndex, colIndex);
    }
    else
    {
        return lv.Items[rowIndex].SubItems[colIndex].Text;
    }
}

//3.实例化委托
private GetCellValueFromListViewDelegate GetCellValueFromListView;

//4.将方法绑定到委托对象
GetCellValueFromListView = new GetCellValueFromListViewDelegate(SafeGetCellValueFromListViewFunc);

//5.调用委托
GetCellValueFromListView (this.listView1, 2, 11);

 

 

持续更新学习中。。。

posted @ 2025-10-07 15:06  修道者~  阅读(11)  评论(0)    收藏  举报