C# - 列表框删除部分行

发布于 11-26 09:56 字数 2338 浏览 1 评论 0原文

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

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    

单击按钮时,我想“删除结尾”并将其重新上传到列表框。 因此,新的列表框将如下所示:

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

所有可能的结尾都在此正则表达式中: 正则表达式placementOneRegex = new正则表达式(@"(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[5 之外的所有 split ]...除了我不知道如何去做。

  • 我的代码不起作用

问题:

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

I have a listbox that has contents that look like this:

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    

On the click of a button, I would like to "REMOVE ENDINGS" and re-upload it to the ListBox.
So the new ListBox would look like this:

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

All of the possibly endings are in this REGEX:
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)");

CODE:

    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);
        }

As you can probably tell.. this is not the best away to go about it. I think it would be easiest to string.Split() and then just join all the split's except the split[5]... except I do not know how to go about doing this..

  • My code does not work

QUESTIONS:

  • How do I grab each line in the listbox and remove the last piece of data (using string.Split())?
  • Is there an easier way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

汹涌人海2024-12-03 09:56:22

这样的事情会起作用吗?我没有测试过。

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”。

Will something like this work? I didn't test it.

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());
       }
    }
}

if those are tabs you might want to try '\t' in the split method.

伊面2024-12-03 09:56:22
    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());
        }
    }
    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());
        }
    }
倦话2024-12-03 09:56:22

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

所有行都是这种格式吗?

Looking at your data I'm thinking that you could use a string position to remove the endings. It looks like you want to truncate each entry after the 33rd position.

Are all the lines in this format?

掐死时间2024-12-03 09:56:22

并不完美,但它本质上满足了您的要求。您必须调整连接以使其按照您想要的方式输出。

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 或数组的长度时,它不起作用。

Not perfect, but it does what you are looking for essentially. You have to tweak the Join to make it output how you want.

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;
    }


}

EDIT: Put this code in it's own class as is.

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;
    }
}

EDIT: Don't use my previous one, I had to fix the extension method, it wasn't working when the specified index to be removed was 0 or the length of the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文