【问题标题】:Access components c#访问组件 c#
【发布时间】:2013-03-29 23:45:17
【问题描述】:

我想知道为什么我可以从主类外部的组合框中访问文本。但我无法向其中添加项目。我的组合框的修饰符设置为公共

public class ImageManager : mainFrame // Where my components are located
{ 
    public ImageManager()
    {

    }

    public void getText()
    {
       Console.WriteLine(comboBox.Text); //Will perfectly retrieve the text from it
    }

    public void setItem()
    {
       comboBox.Items.Add("Items"); //Does absolutely nothing and doesn't show error 
    }
}

感谢您的帮助!

【问题讨论】:

  • 如何查看组合框的内容?
  • 在我的类 mainFrame 中,我设置了 comboBox.Text = "Test",然后当我从 ImageManager 中的任何位置调用 getText() 时,它在我的控制台中显示“Test”
  • 如果您尝试使用 comboBox.Text 获取文本,您将获得组合框中显示的内容。如果未选择任何项目,则在选择项目之前您将一无所获
  • 我知道,但为什么我不能因为 X 原因添加新闻项目,但我可以访问文本和其他功能?

标签: c# ms-access combobox


【解决方案1】:

如果您的setItem() 填充了ComboboxItem 并添加了它而不仅仅是文本怎么办?

public void setItem() 
{
   ComboboxItem addMe = new ComboboxItem();
   addMe.Text = "your text here";
   addMe.Value = 1234; // make a relevant value
   comboBox.Item.Add(addMe);
}

【讨论】:

    【解决方案2】:

    我看到你成功了,太好了。但以防万一你还在摸不着头脑......

        private void Form1_Load(object sender, EventArgs e)
        {
            ImageManager im = new ImageManager();
            im.Show();
            im.setItem();
        }
    

    ImageManager 继承自具有组合框的 Form2。似乎工作正常。组合框已填充。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多