相关疑难解决方法(0)

强制绑定到DataSource的复选框在尚未查看时进行更新

这是一个测试框架,用于显示我在做什么:

  1. 创建一个新项目
  2. 添加选项卡式控件
  3. 在标签1上放一个按钮
  4. 在选项卡2上放置一个复选框
  5. 将此代码粘贴到其代码中

(使用控件的默认名称)

public partial class Form1 : Form
{
    private List<bool> boolList = new List<bool>();
    BindingSource bs = new BindingSource();
    public Form1()
    {
        InitializeComponent();
        boolList.Add(false);
        bs.DataSource = boolList;
        checkBox1.DataBindings.Add("Checked", bs, "");
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);

    }
    bool updating = false;
    private void button1_Click(object sender, EventArgs e)
    {
        updating = true;
        boolList[0] = true;
        bs.ResetBindings(false);
        Application.DoEvents();
        updating = false;
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (!updating)
            MessageBox.Show("CheckChanged fired outside …
Run Code Online (Sandbox Code Playgroud)

c# controls deferred-loading winforms

12
推荐指数
1
解决办法
3044
查看次数

数据绑定是否对隐形控制起作用?

这是winforms的.net问题,而不是asp.net.

我有一个带有几个标签的窗体.我在加载表单时设置所有控件的数据绑定.但我注意到第二个选项卡上的控件的数据绑定不起作用.这些绑定仅在加载表单和选择第二个选项卡时起作用.这给我带来了怀疑:数据绑定仅在绑定控件可见时才起作用.

任何人都可以告诉我这是否属实?测试这个并不难,但我想知道一些确认.

谢谢

.net c# winforms

8
推荐指数
2
解决办法
6569
查看次数

ListBox.DataSource集合与ListBox.Items之间的区别?

我正在动态创建Winforms多选ListBox并将其添加到flowpanel控件中.我从我创建的对象绑定数据源,并验证DataSource实际上有大约14个元素.当我这样做时,listBox.SetSelected(0, true)我会System.ArgumentOutOfRangeException抛出一个错误.

我已经确定问题是,虽然DataSource有14个元素,但Item集合没有(0),因此抛出异常.我的问题是为什么这两者彼此不同,为什么我不简单地在数据源中添加一个foreach项添加到项集合中?

以下是我到目前为止的代码:

case InsertableItemParameter.ParameterType.ListBox:
    //note: two-way bindings are not possible with multiple-select listboxes
    Label lblListBox = new Label();
    lblListBox.Text = param.DisplayText;
    ListBox listBox = new ListBox();
    listBox.DataSource = param.Values;
    listBox.DisplayMember = "Value";
    listBox.SelectionMode = SelectionMode.MultiExtended;
    listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
    listBox.SetSelected(0, true);   //will throw argument out of range exception here!
    listBox.SetSelected(1, true);
    flowPanel.Controls.Add(lblListBox);
    flowPanel.Controls.Add(listBox);
    flowPanel.SetFlowBreak(listBox, true);
    break;
Run Code Online (Sandbox Code Playgroud)

下面是我尝试和工作的替代解决方案,但为什么我会使用DataSource与Items集合?

case InsertableItemParameter.ParameterType.ListBox:
    //note: two-way bindings are not possible with multiple-select listboxes
    Label …
Run Code Online (Sandbox Code Playgroud)

c# winforms

6
推荐指数
1
解决办法
2524
查看次数

标签 统计

c# ×3

winforms ×3

.net ×1

controls ×1

deferred-loading ×1