Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am using using WinForms.

My objective is to split a string of 18 characters so that each character is seen as a separate value.

For example, if 232223232223323222 is my string, I need to split it into 18 separate strings:
2
3
2
2
2
3
2
....and so on.

How can this be achieved?

I have tried several methods:

C#
String a = 2223232223323222;
            a.ToCharArray();

            richTextBox1.Text = richTextBox1.Text + a;</pre>

and
C#
string input = 2223232223323222;
            double parts = 1;
            int k = 0;
            var output = input.ToLookup(c => Math.Floor(k++ / parts))
                .Select(b => new String(b.ToArray()));

            totalres.Text = output.ToString();


To be honest, i'm not even sure what I am doing.

After each value is its own string (18 strings) how can I place each one into its own label?

I only request that level of detail in your explanations is medium-high.

Thank you!
Posted
Updated 27-Mar-12 12:25pm
v2
Comments
fct2004 28-Mar-12 0:30am    
Do you main label, as in the concept of labeling something? Or, an object of type Label?

If you're talking about the concept, it might be easier to place individual items in a list control of some kind.
vlad781 28-Mar-12 2:05am    
I am talking about the object.

What's wrong with this:

C#
int top = 0;
int gap = 4; //for example...
foreach (char ch in a) {
    Label label = new Label(); 
    label.Location = new Point(0, top);
    label.Text = ch.ToString();
    parent.Controls.Add(label);
    top += gap + label.Height;
}


[EDIT]

As OP still does… who knows what, I added to detail with calculation of position. Let's assume it all is put on some panel of appropriate size, so left-topmost position is {0, 0}.

—SA
 
Share this answer
 
v4
Comments
vlad781 27-Mar-12 18:19pm    
label.Text = ch.ToString();
Will this allow each label to have a separate string?

EDIT:

Well, I tried this (but with an existing label) and the label either disappeared or just went blank.
Sergey Alexandrovich Kryukov 27-Mar-12 19:27pm    
Yes. How could it be "non-separate"? you create an individual label per character, is that what you want? (I have no idea why, by the way; let's say, just to get some understanding...)
--SA
vlad781 27-Mar-12 19:36pm    
I'm sorry, but something doesn't seem to be working.

I am attempting to use my current labels, instead of creating more:

foreach (char ch in a)
{
wdval1.Text = ch.ToString();
wtval1.Text = ch.ToString();
wdval2.Text = ch.ToString();
inval1.Text = ch.ToString();
cyval1.Text = ch.ToString();
cyval2.Text = ch.ToString();
inval2.Text = ch.ToString();
}
Shahin Khorshidnia 27-Mar-12 21:17pm    
I think SA solved your critical problem (Creating Lables in a loop) that's why I voted 5.



And about " //etc....": It's not too hard to do it yourself as your style. But if you want any idea you can look at my solution too :D
Sergey Alexandrovich Kryukov 27-Mar-12 22:53pm    
Thank you, Shahin.
I added some detail on OP's request.
--SA
Hello

Have a Panel and:
C#
string a = "2223232223323222";

char[] chars = a.ToCharArray();
for (int i = 0; i < chars.Count(); i++)
{
    Label l = new Label();

    l.Text = chars[i].ToString();
    this.MyPanel.Controls.Add(l);
    l.Top = i * l.Height;
}


or you can do it without Panel:
C#
this.Controls.Add(l);


If I were you, I would create a method to figure out the accurate coordinate for Labels
C#
private void Form1_Load(object sender, EventArgs e)
{
    string a = "2223232223323222";

    char[] chars = a.ToCharArray();
    for (int i = 0; i < chars.Count(); i++)
    {
        Label l = new Label();

        l.Text = chars[i].ToString();
        this.MyPanel.Controls.Add(l);
        SetCoordinate(l, i);
    }

}
private void SetCoordinate(Label lable, int i)
{
    // some code to computing
}




But:
If You have many Labels in your Form, then tag them.
Set their Tags to 0, 1, 2, 3 and ...
Then:
C#
private void Form1_Load(object sender, EventArgs e)
{
    string a = "2223232223323222";

    char[] chars = a.ToCharArray();
    for (int i = 0; i < chars.Count(); i++)
    {
        Label l = FindLabel(i);
        if (l == null)
            continue;
        l.Text = chars[i].ToString();
    }

}
private Label FindLabel(int i)
{
    foreach (Control c in this.Controls)
    {
        if (c.Tag == null)
            continue;

        int k = 0;
        if (!int.TryParse((c.Tag).ToString(), out k))
            continue;

        if (c is Label && k == i)
            return (Label)c;
    }

    return null;
}
 
Share this answer
 
v6
Comments
vlad781 27-Mar-12 18:30pm    
Thank you, I will try this now. I already have the labels on the form, the only thing that needs to change is the text. But I will try this now. Let you know soon.
Shahin Khorshidnia 27-Mar-12 18:51pm    
Ok, Please read updated solution.
vlad781 27-Mar-12 18:55pm    
Thank you, will let you know how it goes.
Shahin Khorshidnia 27-Mar-12 18:59pm    
Ok
vlad781 27-Mar-12 19:11pm    
Well, I have tagged every single label (0-17, 18 total), but I am confused as to what I should do now. Is there anything in the code you provided that I should adjust to fit my needs? or is it ready to go "as it is now"?

EDIT: When I simply copy and paste your code, nothing seems to happen.

I appreciate your effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900