Asceadtius
Asceadtius

Reputation: 11

How to find combinations in this way in C#

I have text in textBox1 "Aplle,Orandge,Nuts,Cherries". There can be more words added in text box, even 30.

I need to find combination like this:

First click - I get textBox2.Text = "Aplle" AND textBox3.Text = "Orandge,Nuts,Cherries"

Second click textBox2.Text = "Aplle,Orandge" AND textBox3.Text = "Nuts,Cherries"

Third textBox2.Text = "Aplle,Orandge,Nuts" AND textBox3.Text = "Cherries"

First I understand that I need counter which count commas, if CommaCounter=1, than split after first comma, if CommaCounter=2, split after second comma, if CommaCounter=100, than split after comma 100.

Than I need splitter, I guess, but and don't understand how to really make it.

Why I need find every combination click by click because every combination will be used by another click witch makes another function.

image1

image2

I insert a pictures how it should look.

here is code I started, of course unfinished, incorrect...

int CommaCounter = 0;
string Part1, Part2;
string Vards = textBox1.Text;

char[] delimiterChars = { ',' };

string[] words = Vards.Split(delimiterChars);

string searchTerm = ",";
var matchQuery = from word in words
                 where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
                 select word;

int wordCount = matchQuery.Count();

foreach (string s in words)
{
    textBox2.Text = Part1.ToString();
    textBox3.Text = Part2.ToString();
}

Upvotes: 1

Views: 114

Answers (2)

JBrooks
JBrooks

Reputation: 10013

Keeping it simple:

     void Button1Click(object sender, EventArgs e)
         {
            string divider = ",";

            if(textBox2.Text == "") // first click.
            {
                   textBox3.Text = textBox1.Text;
                   divider = "";
            }

            int pos = textBox3.Text.IndexOf(",");
            if(pos > -1)
            {
                 textBox2.Text = textBox2.Text + divider + textBox3.Text.Substring(0, pos);
                 textBox3.Text = textBox3.Text.Substring(pos + 1);
            }
            else if (textBox3.Text != "" )  // last one
            {
                 textBox2.Text = textBox2.Text + divider + textBox3.Text;
                 textBox3.Text = "";

             }
         }

Upvotes: 0

Han
Han

Reputation: 3072

First, declare a variable (let's say its name is counter) in the form. The initial value is 0. It stores how many clicks have you done.

// stores how many clicks you have done
int counter = 0;

In the button's event handler, split the text in the first textbox using a comma.

// split the first textbox using a comma
var words = textBox1.Text.Split(',');

Then compare the counter variable with the number of words in the first textbox. If counter is lesser than the number of words, split the words into the second textbox and the third textbox.

if (counter < words.Count())
{
    // increase the counter
    counter++;

    // take n words, where n = counter
    textBox2.Text = string.Join(",", words.Take(counter));

    // skip n words, then take the rest (n = counter)
    textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
}

You can get the value of the second textbox using Take() method. The Take() method get some element from the words. For the third textbox, you need to skip some words before you take the words. You skip the words using Skip() method.

Full source.

using System;
using System.Windows.Forms;
using System.Linq;

namespace Sample
{
    public partial class MainForm : Form
    {
        // stores how many clicks you have done
        int counter = 0;

        public MainForm()
        {
            InitializeComponent();
        }

        void Button1Click(object sender, EventArgs e)
        {
            // split the first textbox using a comma
            var words = textBox1.Text.Split(',');

            if (counter < words.Count())
            {
                // increase the counter
                counter++;

                // take n words, where n = counter
                textBox2.Text = string.Join(",", words.Take(counter));

                // skip n words, then take the rest (n = counter)
                textBox3.Text = string.Join(",", words.Skip(counter).Take(words.Count() - counter));
            }
        }
    }
}

Upvotes: 1

Related Questions