【问题标题】:C# - Listbox Remove part of lineC# - 列表框删除部分行
【发布时间】:2011-07-22 18:02:19
【问题描述】:

我有一个列表框,其内容如下所示:

C44     EXCLUDES    237.910  193.469  0    0603_5    
C45     EXCLUDES    244.102  193.387  0    0603_5    
R47     EXCLUDES    226.935  179.519  90   0402_2    
C18     CAP-00129G  230.960  190.619  0    0402_4    
C17     CAP-00129G  250.085  198.569  90   0402_3     
C25     CAP-00130G  255.635  189.669  90   0402_3    
C56     EXCLUDES    229.430  189.374  0    0402_4    
R42     EXCLUDES    241.010  192.194  90   TANT3216  
R10     EXCLUDES    246.560  203.894  0    0402_9    

单击按钮后,我想“删除结尾”并将其重新上传到列表框。 所以新的 ListBox 看起来像这样:

C44     EXCLUDES    237.910  193.469  0 
C45     EXCLUDES    244.102  193.387  0
R47     EXCLUDES    226.935  179.519  90
C18     CAP-00129G  230.960  190.619  0
C17     CAP-00129G  250.085  198.569  90
C25     CAP-00130G  255.635  189.669  90
C56     EXCLUDES    229.430  189.374  0
R42     EXCLUDES    241.010  192.194  90
R10     EXCLUDES    246.560  203.894  0

所有可能的结尾都在这个正则表达式中: Regex placementOneRegex = new Regex(@"(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551|1913|1313|2513|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505|0705|1020|1812|2225|5764|4532|1210|0816|0363|SOT)");

代码:

    private void removeEndingsButton_Click(object sender, EventArgs e)
    {
        string[] items;
        System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;

        foreach (string str in contents)
        {
            Regex placementOneRegex = new Regex(@"(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551"
                + @"|1913|1313|2513|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505|0705"
                + @"|1020|1812|2225|5764|4532|1210|0816|0363|SOT)");
            items = str.Split(' ');
            items[5].Replace(placementOneRegex.ToString(), "");
            placementOneListBox.Items.Equals(items);
        }

您可能会说.. 这不是最好的方法。我认为string.Split() 是最简单的,然后加入 除了split 之外的所有split[5]... 除了我不知道该怎么做..

  • 我的代码不起作用

问题:

  • 如何抓取列表框中的每一行并删除最后一条数据(使用string.Split()
  • 有更简单的方法吗?

【问题讨论】:

  • 如果你有工作...你为什么要改变它。你可以创建一个字符串数组来匹配我只是不明白这一点。您现在拥有的是更简单的方法,而不必记住每次都更新字符串数组并添加结尾。我也不知道您所说的 split[5] 是什么问题。您如何发布尝试这样做,我们只是从那里开始。
  • 删除结尾后数据是否重新加载到同一个列表框中?如果是这样,为什么首先显示结尾?为什么不先修剪多余的部分?
  • @Ramhound:它不起作用 :-/.. 而 split[5] 是 endings 所在的地方,所以我试图替换 split [5] 什么都没有……这在我上面的代码中不起作用……
  • @IAbstract:是的,数据将加载到同一个列表框中。显示结尾的原因是因为我需要查看结尾以确保它们位于正确的位置并且所有内容都已正确加载以及其他问题。然后我必须修剪结尾,以便我可以正确保存列表框项目并将文件加载到另一个位置。
  • *另一个位置 = 我正在运行的另一个程序需要特殊格式

标签: c# listbox replace split


【解决方案1】:

这样的事情会奏效吗?我没有测试它。

private void removeEndingsButton_Click(object sender, EventArgs e)
{
    string[] items;
    System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;

    foreach (string str in contents)
    {

       List<string> list = str.Split(' ').ToList();
       if(list.Count == 6)
       {
           string remove = list[5];
           list.Remove(remove);           
           placementOneListBox.Items.Equals(list.ToArray());
       }
    }
}

如果这些是标签,您可能想在 split 方法中尝试 '\t'。

【讨论】:

  • 在 Visual Studio 2010 中,当我滚动到 str.Split(' ').ToList().Remove(remove); 下的红线时,它指出 “无法将类型 'bool' 隐式转换为 'System.Collections.Generic.List '"
  • 当我点击removeEndingsButton 并弹出错误提示“索引超出数组边界。” 指向string remove = str.Split(' ')[5];
  • 再次更新。我假设 ' ' 是要拆分的正确字符,并且拆分后总会有 6 个项目。您可能需要稍微调整一下才能使其正常工作,但您应该对需要做什么有基本的了解。
【解决方案2】:
    private void removeEndingsButton_Click(object sender, EventArgs e)
    {
        string[] items;
        char[] splitChars = new char[] {'\t', ' '};
        int fieldCount;
        int lineCount = 0;

        System.Windows.Forms.ListBox.ObjectCollection contents = placementOneListBox.Items;

        foreach (string str in contents)
        {
           lineCount++;
           items = str.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
           fieldCount = items.Length;
           placementOneListBox.Items[lineCount] = string.Join(" ", items.Take(fieldCount - 1).ToArray());
        }
    }

【讨论】:

  • 我不是 100% 的唯一部分是使用placementOneListBox.Items.Equals;拆分和重新连接有效。
  • 点击按钮时出现错误提示,索引超出了数组的范围。并指向placementOneListBox.Items.Equals(string.Concat(items[0], " ", items[1], " ", items[2], " ", items[3]));
  • 每行可能包含的字段数量是否有限制?还是只是 >= 0?
  • 我不确定你在问什么,但我认为它只是 >= 0
  • 对一连串不完整的编辑感到抱歉 - 目前存在连接问题:)。
【解决方案3】:

查看您的数据,我认为您可以使用字符串位置来删除结尾。看起来您想在第 33 个位置之后截断每个条目。

所有的行都是这种格式吗?

【讨论】:

  • 这些值不会总是在同一个位置结束
【解决方案4】:

并不完美,但它基本上可以满足您的需求。您必须调整 Join 以使其输出您想要的方式。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Remove_Click(object sender, EventArgs e)
    {
        ListBox.ObjectCollection collection = placementOneListBox.Items;
        // New collection to store our modified data
        List<string> newCollection = new List<string>();
        // We need regex to account for 0 or more spaces between items in the array.
        Regex r = new Regex(" +");

        foreach (string item in collection)
        {
            // use the regex we created to split the item into an array, it splits it based on any number of spaces.
            string[] splitItem = r.Split(item);
            // We had to write an extension method that "removes" at a particular index.
            splitItem = splitItem.RemoveAt(5);
            // Put it back together again. might have to mess with this to make it output correctly
            string updatedItem = String.Join("    ", splitItem);
            // add new item to collection
            newCollection.Add(updatedItem);
        }
        // clear the existing stuff in the listbox collection
        placementOneListBox.Items.Clear();
        // we can use a List<T> as a data source so that's what were doing here.
        placementOneListBox.DataSource = newCollection;
    }


}

编辑:将这段代码按原样放在它自己的类中。

public static class ExtensionMethods
{
    // An extension method to remove an index from an array, it works with any value type
    public static T[] RemoveAt<T>(this T[] source, int index)
    {
        if (source == null)
            throw new ArgumentNullException();

        if (index < 0 || index > source.Length)
            throw new IndexOutOfRangeException();

        T[] dest = new T[source.Length - 1];

        if (index != 0)
            Array.Copy(source, 0, dest, 0, index - 1);

        if (index != source.Length)
            Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

        return dest;
    }
}

编辑:不要使用我以前的,我必须修复扩展方法,当要删除的指定索引为 0 或数组长度时,它不起作用。

【讨论】:

  • splitItem.**RemoveAt(5)** 似乎不起作用。你知道命中有什么问题吗?
  • 这有什么问题?您具体遇到了什么错误。
  • 我得到的错误是:('System.Array' does not contain a defnition 'RemoveAt' and no extension method 'RemoveAt' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
  • 我编辑了我的帖子。确保您没有将该方法放入表单类中。确保它在自己的静态类中,如我的编辑中所示。
  • 我对其进行了更改,使其属于自己的类。哎呀。哈哈。但是,现在静态类中的异常正在抛出:Index was outside the bounds of the array.
猜你喜欢
  • 1970-01-01
  • 2018-12-25
  • 2014-06-25
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多