【问题标题】:Accessing an object on userControl from another userControl c#从另一个 userControl c# 访问 userControl 上的对象
【发布时间】:2012-07-26 09:43:50
【问题描述】:

我有这个userControl,其中包含ListBox。我想从另一个userControl 访问那个ListBox

例如:

UserControl1.ListBox1.Items.Count;

【问题讨论】:

  • 是什么阻止你这样做?
  • @NikhilAgrawal - 我无法访问列表框。它只是找不到它。

标签: c# object static


【解决方案1】:

在您的用户控件中添加公共属性 ItemsCount

public int ItemsCount 
{
    get { return ListBox1.Items.Count; }
}

or

public ListBox MyListBox 
{
    get { return ListBox1; }
}

访问整个列表框

【讨论】:

    【解决方案2】:

    在您的第一个 usercontrol 中创建 ListBox 的属性并获得类似

    public ListBox lstBox
    {
      get { return this.listBox1;}
    }
    

    现在像这样从其他用户控件访问列表框

    usercontrol1 obj = new usercontrol1();
    int itemCount =  obj.lstBox.Items.Count ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多