如何在ListBox中删除此字符“_” [英] How do I remove this character '_' in ListBox

查看:79
本文介绍了如何在ListBox中删除此字符“_”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我的ListBox包含这个项目:

386-25

235-205

236-459



现在如何删除此字符'_'然后将每个数字保存到变量?



谢谢所有

hi all

my ListBox Contain this items :
386-25
235-205
236-459

Now How do remove this character '_' then save each number to Variable ?

thanks all

推荐答案

我想你不想删除任何东西,你需要确定两个从列表框项中分隔数字。解决方案很简单:

http:/ /msdn.microsoft.com/en-us/library/ms131448%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/b3h1hf19%28v=vs.110%29.aspx [ ^ ]:

I think you don't want to remove anything, you need to determine two separate numbers from the list box item. The solution is simple:
http://msdn.microsoft.com/en-us/library/ms131448%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/b3h1hf19%28v=vs.110%29.aspx[^]:
string[] numbers = myItemText.Split('-', System.StringSplitOptions.RemoveEmptyEntries);
int left = int.Parse(numbers[0]);
int right = int.Parse(numbers[1]);



唯一的问题是:这是一个糟糕的愚蠢解决方案!



真正的解决方案是在列表项中存储正确的数据,而不是字符串。 (这是现在初学者的诅咒:尝试使用表示数据的字符串而不是数据本身。)如果列表项可以是任何对象,则为类型。创建一个包含所需数据的类型,以后可以安全使用。例如:


The only problem is: this is a bad dumb solution!

The real solution is to store proper data in your list items, not strings. (This is a curse of beginners these days: trying to work with strings representing data instead of data itself.) The type if the list item can be any object. Create a type which holds the data you need, which you could safely use later. For example:

struct MyListItem {
    internal MyListItem(int left, int right) { this.Left = left; this.Right = right; }
    internal int Left { get; private set; }
    internal int Right { get; private set; }
    public override string ToString() { return string.Format("{0}-{1}", Left, Right); }
}



为什么 System.Object.ToString()在这里被覆盖?因为这是屏幕上的项目中显示的内容。这样,您使用原始数据,没有字符串解析。字符串应仅用于输出,演示。



现在当您需要此数据时,您将该项目设置为类型 MyListItem 并直接访问 MyListItem.Left,MyListItem.Right



-SA


Why System.Object.ToString() is overridden here? Because this is what is shown in an item on screen. This way, you use original data, with no string parsing. Strings should be used only for output, presentation.

Now when you need this data, you case the item to the type MyListItem and directly access MyListItem.Left, MyListItem.Right.

—SA


MY解决方案:



MY solution :

string result = listBox1.SelectedItem.ToString();
           string[] s = result.Split(' ');
            foreach (string ss in s)
            {
                textBox1.Text = s.GetValue(0).ToString();
                textBox2.Text = s.GetValue(1).ToString();
            }





再次感谢-SA(;



THANK AGAIN FROM —SA (;


这篇关于如何在ListBox中删除此字符“_”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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