Selectedlistboxitem for Index and text

MiPakTeh 1,476 Reputation points
2023-05-13T11:07:45.5366667+00:00

Hi All ,

By click item in listbox3 the show the result to other listbox4.It can use two "Index and text".Using Index it can be succeced but "text" fail.

I need Listbox4 show like bellow if click first line in lisbox3;

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

0 = 10

1 = 16

2 = 9

3 = 6

4 = 10

5 = 9

6 = 8

7 = 10

8 = 4

9 = 10

Min = 4 , ItemMin = 8 || Max = 16 , ItemMax = 1 => 1

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

0 = 10

1 = 16

2 = 9

3 = 6

4 = 10

5 = 9

6 = 8

7 = 10

8 = 4

9 = 10

Min = 4 , ItemMin = 8 || Max = 16 , ItemMax = 1 => 6

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)
        {
            //textfile;
            //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
//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
            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_ = "";
                string List_1 = "";

                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_ + "  " + List_1);
            }

        }

        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();
            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 = listBox2.Items[index].ToString();

                listBox4.Items.Add(listbox1item);

            }
        }
    }
}
public partial class ItemInfo
{
    public int Index;
    public int Value;

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

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-05-15T03:25:12.03+00:00

    Hi @MiPakTeh , Welcome to Microsoft Q&A.

    Note the difference between ASCII and int.

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

    enter image description here

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.