Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
i have textbox1 that shows a value from database and the values are always 7 numbers

I want these 7 numbers to be shown in 4 different textboxes as shown below :

if textbox1 value as an example are : 2145576

then the other textboxes should be like this :

textbox2 value =2 - textbox3 value = 14 - textbox4 value = 55 - textbox5 value = 76
Posted
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 11:19am    
You got two answers, but better think at changing your design; four separate numbers separate semantically should be different attributes in your database.
—SA

This should be very easy to do using String methods, String Methods (System)[^]

For example, use the SubString() method. String.Substring Method (Int32, Int32) (System)[^]. You tell it which characters from a string to get.
 
Share this answer
 
Comments
Thomas Daniels 28-Dec-15 8:12am    
Beat me to it; +5.
ZurdoDev 28-Dec-15 8:24am    
Only because you took the time to do it for them. ;)
[no name] 28-Dec-15 9:58am    
A 5 for the hint to the Basic Tools.
Sergey Alexandrovich Kryukov 28-Dec-15 11:19am    
5ed.
—SA
You can use the String.Substring Method[^] for this:
C#
string original = textbox1.Text;
textbox2.Text = original.Substring(0, 1);
textbox3.Text = original.Substring(1, 2);
textbox4.Text = original.Substring(3, 2);
textBox4.Text = original.Substring(5, 2);
 
Share this answer
 
Comments
[no name] 28-Dec-15 9:58am    
A 5 for the "ready to use" example.
Thomas Daniels 28-Dec-15 10:03am    
Thank you!
Ahmed Zoeil 30-Dec-15 4:31am    
it's working but throws error because it need the whole 7 numbers to be there to work .. is there's a way to make it add to textboxes as I type ?
Thomas Daniels 30-Dec-15 4:35am    
Yes, you can. Use the Length attribute of textbox1.Text to check how long the string is and then do correct Substring calls.
Ahmed Zoeil 30-Dec-15 12:20pm    
can you show me how to do that in code you gave me please
I would approach a problem like this by, first, creating a small "general purpose" tool that would let me split a string into a bunch of "chunks," where the size of each chunk can vary.
C#
private List<string> ChunkString(string source, params int[] chunksizes)
{
    // check for match between source length and total digits required
    if (source.Length != chunksizes.Sum()) throw new ArgumentException("bad argument");

    List<string> results = new List<string>();

    StringBuilder sb = new StringBuilder(source);

    char[] chunk;

    foreach (int chunksz in chunksizes)
    {
        // create a new Char[] to hold the chunk
        chunk = new char[chunksz];

        // copy #chunksz characters from StringBuilder to chunk
        sb.CopyTo(0, chunk, 0, chunksz);
        // trim #chunksz characters off the StringBuilder
        sb.Remove(0, chunksz);

        // save the current result as string
        results.Add(new string(chunk));
    }

    return results;
}
Then, in your case, I might use it this like this:
C#
// in some method or EventHandler
List<string> results = ChunkString("2145576", 1, 2, 2, 2);
// assuming you have references to the four TextBoxes in a TextBox Array named 'tbAry:

TextBox[] tbAry = new TextBox[]{textBox1,textBox2,textBox3,textBox4};

for (int i = 0; i < tbAry.Length; i++)
{
    tbAry[i].Text = results[i];
}
Notes:

1. While I use a 'StringBuilder here to try and minimize the creation of new Strings, I'm not at all sure that has a real "payoff" here ... the business of grabbing Char[] arrays and turning them into strings may be offsetting any gains using a 'StringBuilder.

2. I chose to avoid using Linq here, but you could probably whip-up some Linq code to do this ... which looks simple until you try to understand what it does :)
 
Share this answer
 
Comments
Thomas Daniels 28-Dec-15 9:49am    
Nice general solution, +5!
[no name] 28-Dec-15 9:56am    
Nice adds you show the OP, a 5.
Ahmed Zoeil 30-Dec-15 4:32am    
thanks for your answer working too

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