如何在列表框中使用拆分,单击

Jiale Xue - MSFT 37,946 信誉分 Microsoft 供应商
2024-03-19T06:38:52.6733333+00:00

大家好, 我想在listBox3_SelectedIndexChanged中使用拆分,但仍然失败。在previuos线程中,在这种情况下无济于事。

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.IO;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
      
namespace Checking_1  
{  
    public partial class Form1 : Form  
    {  
        List<string> Data1 = new List<string>();  
        List<string> Data2 = new List<string>();  

  
        //List<Info> Items = new List<Info>();  
        public Form1()  
        {  
            InitializeComponent();  
        }  

  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            foreach (var s in File.ReadLines(@"C:\Users\family\Documents\Tool_Ori.txt").Skip(4000).Select(s => s.Split(new[] { ',' }, 3)[2]))  
            {  
                Data1.Add(s);  
            }  
            var linecount = 0;  

  
            foreach (var item in Data1)  
            {  
                listBox1.Items.Add(linecount++ + "   " + item);  
            }  
        }  

  
        private void button1_Click(object sender, EventArgs e)  
        {  
            foreach (var item in Data1)  
            {  
                listBox2.Items.Add(item);  

  
                var Items = new List<Info>();  

  
                int min = int.MaxValue;  
                int max = int.MinValue;  

  
                for (int j = 0; j <= 9; j++)  
                {  
                    String Number_ = j.ToString();  
                    int NumberCount_ = item.Replace(",", "").ToCharArray().Count(c => c.ToString() == Number_);  
                    listBox2.Items.Add(Number_ +" = " + NumberCount_);  
                    Items.Add(new Info() { Index = Convert.ToInt32(Number_.ToString()), Value = NumberCount_ });  
                }  

  
                foreach (Info ITM in Items)  
                {  
                    if (ITM.Value < min)  
                    {  
                        min = ITM.Value;  
                    }  
                    if (ITM.Value > max)  
                    {  
                        max = ITM.Value;  
                    }  
                }  

  
                string List = "";  
                string List1 = "";  

  
                foreach (Info ITM in Items)  
                {  
                    if (ITM.Value == min)  
                    {  
                        List += "Min =" + min.ToString() + " = " + "Min Item =" + ITM.Index.ToString();  
                        List += "  ";  

  
                    }  
                    if (ITM.Value == max)  
                    {  
                        List1 += "Max =" + max.ToString() + " = " + "Min Item =" + ITM.Index.ToString();  
                        List1 += "  ";  

  
                    }  
                }  
                listBox2.Items.Add(List + "  " + List1);  
                Data2.Add(List + "  " + List1);  

  
            }  

  

  
        }  

  
        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            listBox4.Items.Clear();  
             string curItem = listBox3.SelectedItem.ToString().Trim();  
             string[] arr = curItem.Split(' ', ',', '.').Where(i=>i!="").ToArray();  
             foreach (var item in arr)  
             {  
                 string[] strarr = item.Split(new string[] { "=>", "=" }, StringSplitOptions.None);  
                 int index = Convert.ToInt32(strarr[0]);  
                 string listbox1item = listBox1.Items[index].ToString();  
                 listBox4.Items.Add(listbox1item);  

      
             }  

  
        }  

  
        private void button2_Click(object sender, EventArgs e)  
        {  
            listBox3.Items.Clear();  

  
            var duplicates = Data2.Select((t, i) => new { Index = i, Text = t }).GroupBy(g => g.Text).Where(g => g.Count() > 1).ToList();  
            foreach (var item in duplicates)  
            {  
                var result = item.Select(i => i.Index + "=>" + i.Text).ToArray();  
                string text = string.Join("       ", result);  
                listBox3.Items.Add(text);  
            }  
        }  
    }  
}  

  
public partial class Info  
{  
    public int Index;  
    public int Value;  

  
    public override string ToString()  
    {  
        return Index.ToString() + "  " + Value.ToString();  
    }  
}  

文本文件;

TXT的复制

6131,6433,3457,6615,4102,5786,4429,0554,1096,6749,6164,7932,2546,9557,7782,4423,2767,8839,2988,9450,9872,2220,2930    
8229,6309,8758,7699,1532,6548,3209,7172,6031,3161,1367,3964,9031,0645,7415,7480,1806,7664,5697,7189,4700,9372,9504    
0558,2100,6498,3658,3031,4536,0368,4647,0943,3576,2487,3854,7633,5901,8171,5300,8315,0073,9409,7738,6955,2172,0490   
6733,8571,0977,1758,2288,1280,8427,6100,7091,1285,8588,3928,2179,1486,6647,0171,1149,6691,3499,2316,8876,6179,8647   
6138,9855,8590,2506,5598,3363,3294,5097,8842,2746,2692,0462,6217,6536,4541,6274,2040,5207,9965,6093,1941,3891,1798   
6247,1084,4075,8293,3823,5994,6797,7324,6744,6758,6657,4276,4578,1496,8424,7582,8814,2551,4311,6368,9451,5157,4855  
2617,5522,4776,0297,6088,0337,7253,4374,5167,3930,1407,1118,1323,8912,5418,9738,2516,0833,6079,3447,6405,0700,0555    
3627,3096,7803,7570,8777,2863,8540,7048,1657,4994,3587,0065,6171,4839,3518,6125,9418,6703,6724,6852,7923,8295,2316    
7532,6792,6505,9696,6481,9223,1385,4187,3612,7372,5050,6716,6403,3792,4163,3504,4133,6545,5261,6284,9646,8465,3622    
2607,3836,8540,4848,8080,8018,6807,6238,2486,0771,0504,2154,9826,6737,6982,3010,8217,5414,7137,0145,2421,2362,4997    
1635,7492,6019,5295,2741,0482,7006,8132,8463,9402,3078,0863,7792,2474,7333,9239,7467,7016,5916,2106,8584,1789,4330    
4639,6922,6676,5271,1713,0473,0227,2671,2250,7284,7747,3505,1156,9734,0955,9792,6117,6947,9976,5670,1412,7253,9914    
8128,1211,0287,7649,4181,5522,9241,8835,7188,5081,2301,3470,7892,6198,9287,6095,5490,9125,6038,9326,1350,5679,4781    
2899,2093,5562,5768,7794,8404,3020,9378,0870,1049,1329,7613,3170,2902,3170,1083,7939,4531,8300,8089,1379,4365,5909    
0436,5107,9944,2291,8945,4647,7228,5580,9122,7006,1341,0214,8606,2032,1406,1573,4359,1270,6801,4196,7765,1499,6901    
1138,0886,2059,1298,1844,1327,9516,5899,1433,1912,0303,4733,8682,3493,7669,8406,5734,7835,5327,4605,8416,7601,0333    
7697,0582,9478,7943,7761,6370,0172,0726,9000,4013,1575,0429,0269,1776,8543,0452,9522,7734,9194,8382,6106,5781,8248    
4828,7042,4429,0093,4193,3859,9781,7814,7343,9454,6713,0423,5449,9330,2557,3014,2132,5898,5644,3719,2854,7552,6742    
6776,0884,8531,9925,1256,9574,3781,4469,1898,0059,4469,3139,8709,1508,3643,1724,8411,6241,1648,9507,9226,6376,3004    
2670,2673,4418,1524,3313,1264,9779,5546,0850,0327,1430,4367,7313,5662,5629,0267,2135,4572,2663,5114,9642,1204,0278    
3965,9688,8577,1431,3545,7533,4565,5590,3266,2476,8054,1720,2100,6593,6329,7501,8490,6693,9289,6515,8852,1539,5786    
7221,4947,3344,1482,1513,3673,9477,2417,7423,9165,1558,5317,6144,6638,9647,9002,5806,8747,1600,8234,1845,9410,5378    
8138,3429,1581,3960,0516,5406,3884,9944,1526,4977,2648,3222,8410,8482,0331,4759,1523,8541,4738,0836,9833,1731,8808    
0086,9763,3395,8542,5244,9760,5083,5394,8548,3578,1137,2398,0445,1043,3128,5478,9260,5566,6678,4723,8000,3594,7615    
9968,2356,1590,6003,9644,1330,3516,9920,1684,2419,3025,0763,7954,8300,3398,5947,5633,1510,9577,5387,0841,5321,5879    
1908,2137,5273,2437,4598,4287,3491,9891,2899,9067,7719,0709,9711,3701,6557,7513,5465,4189,7323,9490,5414,3137,8423    
9922,2121,1519,0073,7644,1213,9746,8655,4045,2326,0781,1909,9133,3431,7582,3739,2697,0537,4585,0244,7942,3114,2445    
7526,8322,7585,7174,3071,8155,5125,4246,6406,3412,9511,8304,5608,2849,7675,8819,9117,6481,6711,0928,2605,5574,8398    
7350,8200,9549,6836,0613,8406,5175,5913,9989,2985,7860,0858,0679,1020,4637,4230,9760,7967,5156,7142,3661,4562,7700    
0374,7860,2663,1708,6617,4432,6697,4799,6102,7775,6435,1082,8146,3488,9108,2034,4905,7574,0226,3136,7589,1886,0475    
9726,4505,8135,3561,3804,1073,0601,0173,5777,2539,3902,9497,9186,8228,9756,3182,3887,7237,7481,1509,2677,3806,2510    
7628,9297,4891,7549,6743,0759,2603,7758,3976,7697,5095,4156,7667,5139,9858,4844,1816,6390,2460,5759,8352,1282,5184    
5600,9120,6891,3544,2239,1036,0340,9984,2500,5344,9212,1242,9342,4553,9058,6747,5815,5072,5236,6447,3826,4577,8111    
7232,7812,2480,0216,3233,3031,2370,3941,7554,4102,5284,6255,3024,0473,6960,3447,1816,5243,6418,2429,5844,8509,7800    
5171,7294,5832,7261,9842,9739,1209,1467,8757,2391,5810,1316,5227,2958,9928,3631,3655,8645,4738,9864,6908,1170,1850    
8689,1251,5802,2985,5267,9712,0868,7807,9718,9000,6550,6536,6926,8262,4127,0864,8348,4337,9253,2972,3754,9519,1224    
8182,2114,9871,3335,5653,1812,5503,5310,9234,8864,0841,6947,0275,5765,0869,5877,0160,5120,0107,3023,8970,6360,4241    
0251,1292,8111,7166,6149,5676,6301,5207,3242,2205,0765,9177,9988,4080,6972,5337,9173,4805,4720,8152,8805,5028,8625    
5685,8977,6755,1198,6196,5904,3310,4159,7938,4458,0394,7348,5459,9165,0346,6047,7171,0715,0733,3528,8751,4026,4066    
9958,7361,2240,6848,2874,5092,0199,9898,9387,7323,8021,4130,4954,1651,6877,4377,0761,7249,1547,3562,0587,7777,3259

Note:此问题总结整理于:How to use Split in listbox click

C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
145 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 44,471 信誉分 Microsoft 供应商
    2024-03-19T11:13:27.61+00:00

    根据我的进一步测试,我几乎重现了你的问题。

    您可以尝试使用 string.split 将空字符串拆分为字符串数组。

     private void listBox3_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                listBox4.Items.Clear();  
                string curItem = listBox3.SelectedItem.ToString().Trim();  
                string[] arr = curItem.Split(' ').Where(i => i != ""&&i.Contains("=>")).ToArray();  
                foreach (var item in arr)  
                {  
                    string[] strarr = item.Split(new string[] { "=>" }, StringSplitOptions.None);  
                    int index = Convert.ToInt32(strarr[0]);  
                    string listbox1item = listBox1.Items[index].ToString();  
                    listBox4.Items.Add(listbox1item);  
      
                }  
            }  
    

    测试结果:

    222922-image.png

    希望这对你有所帮助。


    希望这对你有所帮助。 如果回复有帮助,请单击“接受答案”并投赞成票。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助