listBox3_SelectedIndexChanged not consistence

MiPakTeh 1,396 Reputation points
2023-05-23T13:56:43.4266667+00:00

This thread from previous it is ok because using 6 lines in txt file. when we use 21 lines it going error in listbox4;

txtfile 6 Lines;

xxxx

040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407

040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754

040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754

041192,19920514,5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760

041192,19920514,5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760

040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407

txtfile 21 Lines;

xxxxxx

040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407

040892,19920507,0905,3591,8690,7202,6796,4959,2008,7506,8364,1770,8376,0861,7478,4562,0823,8938,1933,5378,9909,7586,9765,4640,9963

040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754

041092,19920513,8060,4939,0023,4607,7550,3038,5542,6792,1722,5747,6235,6947,1202,6683,2863,8238,8435,5799,6684,9326,0361,7529,7599

041192,19920514,5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760

041292,19920517,6917,9058,0689,1337,8176,6711,3979,6195,9340,4421,0302,2605,5748,8390,3578,2928,9052,5627,9009,9876,4323,3573,4553

041392,19920520,1765,7819,7323,4484,0194,4250,5873,0890,4133,1558,2420,1589,6190,2116,1603,3035,6298,4620,1426,2135,9507,8118,0409

041492,19920521,9429,4624,8724,7704,3671,4701,4395,2168,3583,4832,7545,9270,9607,3931,2596,7327,0447,4277,9267,1724,1646,6181,0533

041592,19920524,2719,8619,1121,8176,6265,6792,4738,7083,4193,3013,7757,2923,3087,1162,0485,9365,7385,1742,0097,2836,4814,7867,3382

041692,19920527,9815,9594,0408,8995,9428,6689,4185,5083,1183,0055,9180,1507,6691,4791,3275,1575,0138,0733,8270,8267,4519,9989,8258

041792,19920528,3051,1234,9039,7497,0704,0162,8296,1305,0081,2846,5472,1659,0965,3184,5462,4227,0702,8244,8755,2636,7818,8516,2716

041892,19920531,3876,9285,8726,8746,4867,8341,6204,7663,4042,9559,5573,2298,5513,6082,0093,6128,1096,5894,5923,3195,3233,4542,0592

041992,19920603,7450,8356,4274,3185,2361,9267,4306,0213,8845,1668,0019,3443,7990,9451,7241,5625,1365,6489,3392,9023,9827,9730,2233

042092,19920604,1672,4804,0953,9584,8038,5954,4716,0189,2866,3293,7385,6122,2307,3911,6482,5693,4162,8835,8285,2961,4591,4868,3323

042192,19920607,3348,4681,5559,7610,6901,2440,6136,3523,6260,4572,9334,4429,8178,7266,1948,2643,4304,2106,2170,4952,9358,6424,6111

042292,19920610,8507,3040,5886,5941,0981,6218,3388,0349,1500,6106,9238,0926,7079,2244,1579,8735,2992,5134,5134,8863,2816,9605,1401

042392,19920611,8200,8500,1925,3921,2593,2992,4583,9848,6373,3380,2762,4195,4944,3388,4140,2511,9803,2784,4558,3651,6493,7548,4164

042492,19920614,5409,7926,5569,2922,9788,2941,4716,3471,3541,5850,4128,1906,4956,2453,4743,3700,7031,9690,3058,8328,2809,7790,3279

040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407

040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754

041592,19920524,2719,8619,1121,8176,6265,6792,4738,7083,4193,3013,7757,2923,3087,1162,0485,9365,7385,1742,0097,2836,4814,7867,3382

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 Bat_7
{
    public partial class Form1 : Form
    {
        List<string> Data1 = new List<string>();
        List<string> Data2 = new List<string>();

        List<ItemInfo> Items = new List<ItemInfo>();

        public static List<string> list = new List<string>();

        int Lines = 0;
        int Lines_a = 0;

        string text = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var s in File.ReadLines(@"D:\Ori_1.txt")
            .Skip(1)
            .Select(s => s.Split(new[] { ',' }, 3)[2]))
            {
                listBox1.Items.Add(Lines++ + "  " + s);
                Data1.Add(s);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in Data1)
            {
                ++Lines_a;

                listBox2.Items.Add(item + "   =>" + Lines_a);

                var Items = new List<ItemInfo>();

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

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

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

                string List = "";
                string List_ = "";

                foreach (ItemInfo ITM in Items)
                {


                    if (ITM.Value == min)
                    {
                        List += "Min = " + min.ToString() + " , " + "ItemMin = " + ITM.Index.ToString() + "  " + " || ";
                    }
                    if (ITM.Value == max)
                    {
                        if (!List_.Contains("ItemMax"))
                        {
                            List_ += "Max = " + max.ToString() + " , " + "ItemMax = " + ITM.Index.ToString() + "     => ";
                        }

                    }

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

        }

        private void button2_Click(object sender, EventArgs e)
        {
            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();
                text = string.Join("  ", result);
                listBox3.Items.Add(text);
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox4.Items.Clear();
            string curItem = listBox3.SelectedItem.ToString().Trim().Replace(" ", "");

            var arr = curItem.Split('>').ToList();
            foreach (var item in arr)
            {
                if (item == "") return;
                int index = int.Parse(item[0].ToString());
                for (int i = index * 12; i < ((index + 1) * 12); i++)
                {
                    string listbox1item = listBox2.Items[i].ToString();
                    listBox4.Items.Add(listbox1item);
                }
            }
        }
    }
}
public partial class ItemInfo
{
    public int Index;
    public int Value;

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

Thank.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,186 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 3,936 Reputation points Microsoft Vendor
    2023-05-24T03:13:30.08+00:00

    Hi @MiPakTeh , Welcome to Microsoft Q&A.

    The grouping of your button2 seems to be different from before. Is this what it should be?

    The reason for the error is that in the previous code, I only considered the case where the number is a single character.

    You can use Substring with TrimEnd and other methods to get all the numbers ahead, currently it supports 5 digits.

    You just need to modify the code of listBox3_SelectedIndexChanged to:

    private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox4.Items.Clear();
        string curItem = listBox3.SelectedItem.ToString().Trim().Replace(" ", "");
    
        var arr = curItem.Split('>').ToList();
        foreach (var item in arr)
        {
            if (item == "") return;
            string tmpString = item.Substring(0, 5);
            int index = int.Parse(tmpString.TrimEnd("Min=".ToCharArray()));
            for (int i = index * 12; i < ((index + 1) * 12); i++)
            {
                string listbox1item = listBox2.Items[i].ToString();
                listBox4.Items.Add(listbox1item);
            }
        }
    }
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful