C#,Windows.Forms.Combobox和对象. [英] C#, Windows.Forms.Combobox and Objects.

查看:106
本文介绍了C#,Windows.Forms.Combobox和对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我是C#的新手,想寻求一点帮助.

我在表单上有对象数组和System.Windows.Forms.ComboBox元素.
我需要填充ComboBox的项目列表并将对象附加到每个项目.
使用Delphi,我可以使用现有方法ComboBox.AddObject做到这一点.

Hello!
Im new in C# and want to ask a little help.

I have array of objects and System.Windows.Forms.ComboBox element on form.
I need fill item list of ComboBox and attach object to every item.
Using Delphi I can do this with existing method ComboBox.AddObject.

var ArrayOfObjects = array of Objects;
...
ArrayOfObjects := GetArrayOfObjectsFromSource(); 
...
for i:=0 to Length(ArrayOfObjects)-1 do
begin
  ComboBox.AddItem(ArrayOfObjects[i].stringproperty, TObject(ArrayOfObjects[i]));
end;

// After this I can do the next:
...
var O : Object;
...
  O := Object(ComboBox.Items.Objects[cbAccounts.ItemIndex]);
...
//and make something with received object.
...



但是我在C#中找不到相同的方法.使用方法ComboBox.Items.Add我只能填写项目列表.



But I can''t find the same method in C#. Using method ComboBox.Items.Add I can only fill list of items.

foreach(Object O in ArrayOfObjects)
{
  // I know how to add new string to ComboBox
  ComboBox.Items.Add(O.stringproperty); 
  //and I don't know how insert Object :( 
}
...
  Object NewObject = ????????





Can anyone help me, please?

推荐答案

不是依靠对象,而是在C#中使用显式对象是一种好习惯.例如:
Rather than rely on objects, in C# it is considered good practice to use explicit objects. For example:
public class MyItem
    {
    public string String { get; set; }
    public int Int { get; set; }
    public MyItem(string s, int i) { String = s; Int = i; }
    public override string ToString()
        {
        return "MyItem: " + String;
        }
    }

private void myForm_Load(object sender, EventArgs e)
    {
    MyItem[] list = new MyItem[2];
    list[0] = new MyItem("Hello", 1);
    list[1] = new MyItem("Goodbye", 2);
    myComboBox.Items.AddRange(list);
    }

private void myButton_Click(object sender, EventArgs e)
    {
    MyItem mi = (MyItem)myComboBox.Items[1];
    Console.WriteLine(mi.String + mi.Int);
    }


这是将整个项目放入ComboBox中-显示的字符串来自重写的ToString方法-当您从ComboBox中获得该项目时,您可以访问所需的所有属性.

就个人而言,我将使用List而不是数组来初始化ComboBox,但是您似乎使用了数组,所以这就是我所显示的.


What this does is put the whole of your item into the ComboBox - the displayed string comes from the overridden ToString method - and you can access all the properties you want when you get the item from the ComboBox.

Personally, I would use a List rather than an array to init the ComboBox, but you seemed to use an array, so that is what I showed.

private void myForm_Load(object sender, EventArgs e)
    {
    List<MyItem> list = new List<MyItem>();
    list.Add(new MyItem("Hello", 1));
    list.Add(new MyItem("Goodbye", 2));
    myComboBox.Items.AddRange(list.ToArray());
    }


在过去的好日子里,不建议这样做,但是确保将所有内容混合在一起是一个大的用户界面驱动的意大利面,这是常见的做法.但是,当您考虑它时,为什么要检索一个包含对象实例的数组(可能与用户界面完全无关),并将其作为对象存储到ComboBoxItem中?除了对象现在没有类型的事实之外,您还拥有许多实例的数组,用户可以从中选择一个有用的实例,其余实例可以丢弃.

解决方案是使用户界面更干净,因此将GetArrayOfObjectsFromSource分为两种不同的方法.一种方法将仅为您提供可用于填充组合框的名称,另一种方法将按名称为您提供必要的对象(在组合框中选择).

顺便说一句,如果您请求的对象与用户界面完全无关,那么最好是将所有对象一起处理的方法.用户界面仅用于输入开始处理所需的必要参数.您只需将选定的组合框值传递给该方法(在专门的类中),然后从那里处理它.这是为了避免所谓的魔术按钮模式,这种模式使软件项目瘫痪.

好吧,有很多解释希望可以清楚地表明,关键是要保持UI干净,使UI免受不属于该类的所有事物的影响.

祝你好运!
In those good old days it was not advised but sure common to mingle everything together as one large user interface driven bowl of spaghetti. But when you think about it, why would you retrieve an array with object instances (that probably have nothing to do with the user interface at all) and store that into a ComboBoxItem as an object? Besides the fact that the object is now untyped you also have an array of a lot of instances of which the user will choose one that is useful and the rest can be discarded.

The solution would be to make user interface more clean and therefor devide the GetArrayOfObjectsFromSource up into 2 different methods. One method that will give you only the names which you can use to full the combobox and another method that gives you the necessary object by name (selected in the combobox).

If, by the way, the object that you request has nothing to do with the user interface at all then it would even be better it is a method that would do the processing all together. The user interface is just for entering the necessary parameters required to start the processing. You would just pass the selected combobox value to that method (in a specialized class) and let it handle it from there. This is to avoid the so called magic-push-button-pattern that cripples software projects beyond believe.

Well, a lot of explanation that hopefully made it clear that the key is to keep the UI clean from all kind of stuff that doesn''t belong there.

Good luck!


这篇关于C#,Windows.Forms.Combobox和对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆