Count forward and backward string

MiPakTeh 1,476 Reputation points
2022-11-05T14:34:30.957+00:00

Hi All,
How to count in three 3 number forward and backwark.
By click button1 the result show

698 = 6
075 = 5
150 = 4

here code;
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_12  
{  
    public partial class Form1 : Form  
    {  
        //..Text File=" 968, 986, 698, 689, 896, 869, 075, 051, 057, 105, 150, 705, 750, 501, 507 "  
  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            foreach (var s in File.ReadLines(@"D:\Ro.txt"))  
            {  
                listBox1.Items.Add(s);  
            }  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
  
            var count = listBox1.Items.ToString().Split(new[] { ',', ':' });  
            var count_ = count.Where(s => s.Count().ToString() ="xxx");  
            listBox2.Items.Add(count_);  
        }  
  
    }  
}  
  
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,636 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.6K Reputation points
    2022-11-05T17:25:10.607+00:00

    Try an alternative suggestion too:

    private void button1_Click( object sender, EventArgs e )  
    {  
        var r = listBox1.Items  
            .Cast<string>( )  
            .Select( i => i.Split( ',' ).Select( n => string.Concat( n.Trim( ).OrderBy( c => c ) ) ) )  
            .SelectMany( a => a )  
            .GroupBy( n => n )  
            .Select( g => new { n = g.Key, c = g.Count( ) } );  
      
        listBox2.Items.AddRange( r.Select( z => $"{z.n} = {z.c}" ).ToArray( ) );  
    }  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 27,656 Reputation points
    2022-11-05T15:29:57.967+00:00

    How to count in three 3 number forward and backward.

    Your example is very confusing as it counts backward by 5 first then by 4 not 3! It is not clear how you get 6, 5, 4 or what these values represent and why you are using a listbox when you have one item in the listbox1.

    This is a very basic math question. I'll assume you want to count backward by 3 as stated in your post. There are 15 items in the collection so the first item is 15-(3*i)= 12 where i =1. The second item is 15-(2*i) = 9 where i = 2. Simply do the math until the results of the addition/subtraction is less than or equal to 0. In your button click simply increment i. The following example counts backward. To count forward the index math is simply (i*3) where i starts at 0.

        public partial class Form1 : Form  
        {  
            private int i;  
            private int totalItems;  
            private List<string> items;  
            private const int backwardCount = 3;  
    
            public Form1()  
            {  
                InitializeComponent();  
                i = 1;  
            }  
    
            private void Form1_Load(object sender, EventArgs e)  
            {  
                foreach (var s in File.ReadLines(@"Ro.txt"))  
                {  
                    listBox1.Items.Add(s);  
                }  
    
                items = listBox1.Items[0].ToString().Split(new[] { ',', ':' }).ToList();  
                totalItems = items.Count();  
            }  
    
            private void button1_Click(object sender, EventArgs e)  
            {  
                int idx = totalItems - (backwardCount * i++);  
                label1.Text = idx.ToString();  
                if(idx >= 0)  
                {  
                    listBox2.Items.Add(items[idx]);  
                }  
    
            }  
        }  
    

    If the code does not meet your requirements, I'm sure you can tweak the code to suite your needs since all you're doing is basic addition and subtraction.


  2. Karen Payne MVP 35,386 Reputation points
    2022-11-05T18:45:24.8+00:00

    Another option is to use Chunk and Reverse done here with .NET Core 6. Extra blank entries are for clarity.

    using System.ComponentModel;  
      
    namespace WinFormsApp3;  
      
    public partial class Form1 : Form  
    {  
        private readonly BindingList<string> _bindingList1 = new();  
        private readonly BindingList<string> _bindingList2 = new();  
        private readonly BindingSource _bindingSource1 = new();  
        private readonly BindingSource _bindingSource2 = new();  
        public Form1()  
        {  
                  
            InitializeComponent();  
      
            _bindingSource1.DataSource = _bindingList1;  
            _bindingSource2.DataSource = _bindingList2;  
      
            listBox1.DataSource = _bindingSource1;  
            listBox2.DataSource = _bindingSource2;  
        }  
      
        private void ChunkButton_Click(object sender, EventArgs e)  
        {  
            var container = " 968, 986, 698, 689, 896, 869, 075, 051, 057, 105, 150, 705, 750, 501, 507"  
                .Split(',')  
                .Select(x => x)  
                .ToList();  
      
            var chunked = container.Chunk(3).ToList();  
      
            foreach (var chunker in chunked)  
            {  
                foreach (var data in chunker)  
                {  
                    _bindingList1.Add(data);  
                }  
      
                _bindingList1.Add("");  
            }  
      
            chunked.Reverse();  
      
            foreach (var chunker in chunked)  
            {  
                foreach (var data in chunker)  
                {  
                    _bindingList2.Add(data);  
                }  
      
                _bindingList2.Add("");  
            }  
      
        }  
    }  
    

    257541-chunked.png