Select item listbox to new listbox to show the result

MiPakTeh 1,476 Reputation points
2022-10-19T07:33:53.32+00:00

Hi All,
I have listbox2.item for selected item and listbox3 as a SelectedIndexChanged then listbox4 for show that result.

listbox4 should show like this,

23 3075,7224,9037,4576,6715,2626,2433,3929,6162,1885,0509,8861,3369,0366,4525,6224,1415,5931,5523,6383,8570,8873,0444
0 = 7
1 = 7
2 = 12
3 = 14
4 = 9
5 = 12
6 = 12
7 = 7
8 = 8
9 = 6
Min = 6 , ItemMin = 9 | Max = 14 , ItemMax = 3

107 2180,3643,2024,2167,4421,1683,5221,1604,1349,7360,9720,2594,5343,9758,3138,3899,1317,2805,5766,4375,2202,6871,6533
0 = 8
1 = 12
2 = 13
3 = 14
4 = 9
5 = 8
6 = 9
7 = 9
8 = 7
9 = 6
Min = 6 , ItemMin = 9 | Max = 14 , ItemMax = 3

Bellow code what I try.

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Checking_9  
{  
    public partial class Form1 : Form  
    {  
        public static List<string> Data1 = new List<string>();  
        public static List<string> Data2 = new List<string>();  
        public static List<string> Data3 = new List<string>();  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
        //Text file=  
3096,9575,5857,7523,2939,8257,1090,7786,3978,2445,5678,6082,5068,3348,3116,0567,5330,3163,8051,1694,6228,0958,0520  

1252,7456,2918,7195,5261,3915,9565,1349,1809,7841,1955,6685,9474,3382,0878,0215,0475,5198,6304,0340,4132,7968,4855
5953,7633,5290,3677,9283,0286,4362,5138,3595,1611,5595,2606,0875,6508,4577,1552,9343,5943,0249,0661,7035,7981,3793
3075,7224,9037,4576,6715,2626,2433,3929,6162,1885,0509,8861,3369,0366,4525,6224,1415,5931,5523,6383,8570,8873,0444
4012,4521,5236,7742,4631,3341,2964,0302,4936,3886,0095,8346,1467,5265,4126,4361,0340,5593,3970,9164,3042,9895,7323
2533,3300,8859,6435,0584,7066,3002,4954,3684,9961,1802,9754,0979,0644,9120,8147,8026,7336,8435,2222,7272,6599,8041
7076,5301,8244,2633,0836,1508,7437,7300,0854,0938,4433,7348,0204,1906,3595,2670,0394,0705,7140,7213,0330,6939,2794
8315,2098,8874,5389,9190,4085,8656,3364,0241,0580,4087,8888,8923,8915,9275,4392,0594,8785,6889,0130,1244,1286,7181
5080,4313,3355,0917,6470,7309,3391,5464,9743,2222,8201,9343,5116,1941,4717,9271,6599,9847,7480,1985,1993,3589,6232
0858,9387,5659,0700,1420,9204,0115,9877,1942,2862,2983,4665,3371,4576,4843,8392,7054,9749,5078,7014,2922,9715,3156
3791,2222,6606,0634,4514,1123,0740,2898,9553,0031,7530,6172,1608,1913,6815,3449,2872,8136,5943,0661,3752,4029,0902
2567,5338,1834,7393,4000,5286,2263,0470,1271,2172,4544,0634,5944,1272,0873,2927,0483,1337,8683,6066,0124,5632,0607
3316,0424,0591,4162,2146,2020,0706,2889,4297,8888,7930,3601,6657,9605,7458,8433,6105,8836,4098,3737,1875,3371,6391
5036,4380,2274,8262,1445,9643,9357,1403,7983,9789,4236,0799,5030,0021,3652,2509,4421,6353,7410,2637,5529,9250,3331
2180,3643,2024,2167,4421,1683,5221,1604,1349,7360,9720,2594,5343,9758,3138,3899,1317,2805,5766,4375,2202,6871,6533
9454,9883,5433,0729,2108,1719,1457,9174,2781,7892,7832,5486,4249,3620,6381,6111,3598,2138,2101,2792,9545,2033,2406
3021,5698,6197,7387,3763,2112,8443,1771,2778,4237,3692,7555,0390,3227,9169,9749,9966,7278,7534,9477,7764,6567,7008

        private void Form1_Load(object sender, EventArgs e)  
        {  
            int linecount = 0;  
  
            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\family\Documents\Tool_Ori_4.txt");  
  
            foreach (string line in lines)  
            {  
                listBox1.Items.Add(linecount++ + "     " + line);  
            }  
  
        }  
  
        private void button5_Click(object sender, EventArgs e)  
        {  
            using (System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(@"D:\ViewList_.txt"))  
            {  
                foreach (var item in listBox2.Items)  
                {  
                    SaveFile.WriteLine(item.ToString());  
                }  
            }  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            foreach (var item in listBox1.Items)  
            {  
                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.ToString().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 List_ = "";  
  
                string ListA = "";  
                string ListB = "";  
  
                foreach (Info ITM in Items)  
                {  
                    if (ITM.Value == min)  
                    {  
                        List += "Min = " + min.ToString() + " , " + "ItemMin = " + ITM.Index.ToString() + "  ";  
                        ListA += min.ToString() + " = " + ITM.Index.ToString();  
                    }  
                    if (ITM.Value == max)  
                    {  
                        List_ += "Max = " + max.ToString() + " , " + "ItemMax = " + ITM.Index.ToString() + "  ";  
                        ListB += max.ToString() + " = " + ITM.Index.ToString();  
                    }  
                }  
  
                listBox2.Items.Add(List + "    |    " + List_);  
                Data2.Add(ListA + "  " + ListB);  
            }  
  
        }  
  
        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).ToArray();  
                string 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 Info  
{  
    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.
10,648 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2022-10-20T07:11:41.93+00:00

    Hi @MiPakTeh , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    It seems to me that you want to get all the information for 3 and 14, you need to change it like this:

    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]);  
      
    		for (int i = 12 * index; i < 12 * index + 12; i++)  
    		{  
    			string listbox1item = listBox2.Items[i].ToString();  
    			listBox4.Items.Add(listbox1item);  
    		}  
    	}  
    }  
    

    252352-image.png

    If you simply want lines 3 and 14. You just need to change string listbox1item = listBox2.Items[index-1].ToString();

    252260-image.png

    But according to your calculation, if there is an index 0 problem, you need to consider related issues.


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful