交换c#.net 3.5中的combox值... [英] swap the combox values in c#.net 3.5...

查看:91
本文介绍了交换c#.net 3.5中的combox值...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...我有两个文本框和一个Windows窗体组合框.组合框的值将是A,B,C,D ......一个是FROM文本框,另一个是TO文本框...当我在FROM文本框中输入2并在TO文本框中输入3时,组合框值将像这样交换A,C,B,D .....在c#.net 3.5中的操作方法...

Hi... i have two textboxes and one combobox in windows form.The Combobox values ll be A,B,C,D........... One is FROM textbox and Another is TO textbox... when i enter 2 in FROM textbox and 3 in TO textbox then the combobox values will be exchange like this A,C,B,D..... how to do in c#.net 3.5......

推荐答案

您可以用不同的方法来做.您可以将新项目从组合框的一个位置添加/插入到理智的组合框的另一个位置(它将添加两个附加项目),然后删除旧项目.引用类型的相同项目可以在组合框中同时使用两次,没有问题.此外,在这种方法中,在删除重复项之前,这些项会临时在组合框中参与的次数超过一次.

由于第二种方法取决于组合框项目的类型,因此首先是字符串,不是字符串或值类型的引用类型. (这听起来很混乱.您需要知道ComboBox项可以是任何类型.以文本形式显示在项中的内容取决于方法object.ToString()的输出,可以重写该方法以提供所需的外观.项目.)

假设您的商品类型为string.这种类型非常特殊,它模仿按值语义作为引用类型.解决方案是这样的:您在两个局部变量的组合框中复制两个具有不同索引的项目,然后将这些变量的值复制回相同的组合框项目,但是将变量与第一次复制相比进行交换.

如果组合框项目是任何其他类型,则可以使用此想法;但是,如果此类型是字符串以外的其他引用类型,则需要使用克隆而不是复制.

—SA
You can do it in different ways. You can add/insert a new item from one position of combo box to another position of the sane combo box (it will make two extra items) and than remove old items. The same item of a reference type can be used twice in a combo boxe at the same time, no problem. Moreover, in this method the items participate more then once in a combo box on temporary basis, before duplicate items are removed.

As second method depends on the type of combo box item, first of all, is it a string, a reference type other than string or value type. (It can sound confusing. You need to know that ComboBox items can be of any type. What is presented in the item in text form depends on the output of the method object.ToString() which can be overridden to provide desired look of the item.)

Let''s assume your items are of the type string. This type is very special, it mimics by-value semantic being a reference type. The solution is this: you copy two items of different indexes in a combo box in two local variables, then you copy the values of these variable back the the same combo box items, but swap the variables compared to first copy.

You can use this idea if you combo box items are of any other type; however, if this type is a reference type other then string, you will need to use cloning instead of copy.

—SA


您应该使用组合框的RemoveAtInsert方法,例如,

You should use RemoveAt and Insert methods of combobox like,

Object value = ComboBox1.Items(from-1);
ComboBox1.Items.RemoveAt(from-1);
ComboBox1.Items.Insert(to-1, value);


这篇关于交换c#.net 3.5中的combox值...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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