Reputation:
I have made a winform app which takes the values from the textboxes and pass to different textboxes. But in my case I am able to pass the values. When I delete some letters in between I am unable to pass exactly each numbers to the specified textboxes.
I have 10 textboxes to receive values. And I have one major textbox (i.e.) textbox11 to write the values and pass them to rest 10 textboxes.
So lets say I am typing N14 G73 X315.2 Y83.7 I40.0 J6.4 A0.0 H3 K75 T11 F5 M0 C0.0 in textbox11. I can successfully pass values of
N to textbox1 = 14
G to textbox2 =73
X to textbox3 = 315.2
Y to textbox4 = 83.7
I to textbox5 = 40.0
J to textbox6 = 6.4
H to textbox7 = 3
K to textbox8 = 75
F to textbox9 = 30
T to textbox10 = 11
But when I change the format to N14 G73 X315.2 Y83.7 I40.0 A0.0 H3 K75 T11 F5 M0 C0.0 So here when compared with above line, we can see J6.4 is missing In this case, my values are left shifted.. Below is the output I am getting..
N to textbox1 = 14
G to textbox2 =73
X to textbox3 = 315.2
Y to textbox4 = 83.7
I to textbox5 = 40.0
J to textbox6 = 0.0
H to textbox7 = 0.0
K to textbox8 = 3
F to textbox9 = 75
T to textbox10 = 30
How to deal with this situation.. I want to take each number to each textbox individually regardless of the string length.
Code snippet:
private void textBox11_TextChanged(object sender, EventArgs e)
{
string originalText = textBox11.Text;
string[] splitString = originalText.Split(new Char[] { 'N', 'G', 'X', 'Y', 'I', 'J', 'A', 'H', 'K', 'F', 'T', 'M', 'C' });
foreach (String str in splitString)
{
if(str == null || str.Length == 0)
{
switch (str[0])
{
case 'N':
textBox1.Text = str.Substring(1);
break;
case 'G':
textBox2.Text = str.Substring(1);
break;
case 'X':
textBox3.Text = str.Substring(1);
break;
case 'Y':
textBox4.Text = str.Substring(1);
break;
case 'I':
textBox5.Text = str.Substring(1);
break;
case 'J':
textBox6.Text = str.Substring(1);
break;
case 'H':
textBox7.Text = str.Substring(1);
break;
case 'K':
textBox8.Text = str.Substring(1);
break;
case 'F':
textBox9.Text = str.Substring(1);
break;
case 'T':
textBox10.Text = str.Substring(1);
break;
}
}
}
}
Upvotes: 1
Views: 84
Reputation: 832
Update: The final version: For the particular question, its better to use regular expression. . Here is the optimized code:
private void textBox11_TextChanged(object sender, EventArgs e)
{
string originalText = textBox11.Text;
string[] splitString = originalText.Split(new Char[] { 'N', 'G', 'X', 'Y', 'I', 'J', 'A', 'H', 'K', 'F', 'T', 'M', 'C' });
String charset = "NGXYIJHKFT";
Regex regex = new Regex(@"[NGXYIJAHKTFMC]\d*\.?\d*");
String str = "";
foreach (Match match in regex.Matches(originalText))
{
str = match.Value;
Control[] controls = Controls.Find("textBox" + (charset.IndexOf(str[0]) + 1), true);
if (controls.Length > 0)
((TextBox)controls[0]).Text = str.Substring(1);
}
}
====== Older answers..
You better split by space and then while setting into textboxes you can remove the first character. The reason is split by multiple characters cannot ensure the order by which they will come into the output array.
private void textBox11_TextChanged(object sender, EventArgs e)
{
string originalText = textBox11.Text;
string[] splitString = originalText.Split(new Char[] { 'N', 'G', 'X', 'Y', 'I', 'J', 'A', 'H', 'K', 'F', 'T', 'M', 'C' });
foreach(String str in splitString)
{
if(str == null || str.Length == 0)
continue;
switch(str[0])
{
case 'N':
textBox1.Text = str.Substring(1);
break;
case 'G':
textBox2.Text = str.Substring(1);
break;
case 'X':
textBox3.Text = str.Substring(1);
break;
case 'Y':
textBox4.Text = str.Substring(1);
break;
case 'I':
textBox5.Text = str.Substring(1);
break;
case 'J':
textBox6.Text = str.Substring(1);
break;
case 'H':
textBox7.Text = str.Substring(1);
break;
case 'K':
textBox8.Text = str.Substring(1);
break;
case 'F':
textBox9.Text = str.Substring(1);
break;
case 'T':
textBox10.Text = str.Substring(1);
break;
}
}
}
=========================
Update: There is further improvement possible with the code block: The idea is to use Controls.Find to find a textbox by name. You need form the charset exactly according the order that you want to show in the text-boxes:
private void textBox11_TextChanged(object sender, EventArgs e)
{
string originalText = textBox11.Text;
string[] splitString = originalText.Split(new Char[] { ' ' });
String charset = "NGXYIJAHKFTMC";
foreach(String str in splitString)
{
((TextBox)this.Controls.Find("textBox" + charset.IndexOf(str[0]), true)).Text = str.Substring(1);;
}
}
Upvotes: 2
Reputation: 48149
Will this work for you... It will actually work regardless of the order, spacing or whatever between the letters you supply... Add more if you need to...
string yourLetters = "NGXYIJAHKTFMC";
string[] parts = new string[13];
string yourText = textBox11.Text;
int p = -1;
for (int i = 0; i < yourText.Length; i++)
{
if (yourLetters.Contains(yourText.Substring(i, 1)))
{
p++;
parts[p] = "";
}
else
parts[p] += yourText.Substring(i, 1);
}
// Now, throw the parts into the textboxes
textBox1.Text = parts[0];
textBox2.Text = parts[1];
textBox3.Text = parts[2];
textBox4.Text = parts[3];
textBox5.Text = parts[4];
textBox6.Text = parts[5];
textBox7.Text = parts[6];
textBox8.Text = parts[7];
textBox9.Text = parts[8];
textBox10.Text = parts[9];
Upvotes: 0
Reputation: 769
first of all , i believe you should really learn to use your debugger.
I am not sure how you can get the results you get, as first index in the array is 0, not 1. So splitString[1] could never be 14.
You probably have something prior to N maybe a tab or a blank.
Then after removing J from string the resulting array becomes smaller.
[14,73,315.2,83.7,40.0,0.0,3,75,11,5,0,0.0]
If you count the elements, the last one is [11] , so splitString[12] throws an exception.
Sorry , i am not sure how it should work at all.
Upvotes: 0