Reputation: 995
Can't get "z" to be replaced by the code below. Any help would be awesome. Thanks
//Generate Nut Part Number
textBox7.Text = "MLA028Z-CAN-" + comboBox2.Text + "R" + "-" + comboBox3.Text + "z" + "0";
//Replaces z Variable in Lead Screw Assembly Part Number
if (comboBox1.Text == "0")
textBox6.Text.Replace("z", "B");
else if (comboBox1.Text == "1")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "2")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "3")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "4")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "5")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "6")
textBox7.Text.Replace("z", "D");
else if (comboBox1.Text == "7")
textBox7.Text.Replace("z", "B");
Upvotes: 0
Views: 146
Reputation: 21742
Since you're not really using the value assign to the textbox in the first place and only have two different scenarios in your if's I've rearranged the code a bit. The main problem however as stated elsewhere is that you weren't assigning the result of the replace back to the text property
//Replaces z Variable in Lead Screw Assembly Part Number
var replace = "z";
int combo;
if(int.TryParse(comboBox1.Text, out combo)){
if (combo == 0) textBox6.Text = textBox6.Text.Replace("z", "B");
else if (combo >0 && combo < 7) replace = "D";
else if (combo == 7) replace = "B";
}
//Generate Nut Part Number
textBox7.Text = "MLA028Z-CAN-" + comboBox2.Text + "R" + "-" + comboBox3.Text + replace + "0";
Upvotes: 0
Reputation: 21881
Others have answered the main question but i would also recomend replacing all those else if with the following.
var oneToSix = Enumerable.Range(1, 7).Select(x => x.ToString());
if (comboBox1.Text == "0") textBox6.Text = textBox6.Text.Replace("z", "B");
if (comboBox1.Text == "7") textBox7.Text = textBox7.Text.Replace("z", "B");
else if (oneToSix.Contains(comboBox1.Text)) textBox7.Text = textBox7.Text.Replace("z", "D");
Upvotes: 0
Reputation: 115749
String
s are immutable in .NET, hence textBox7.Text = textBox7.Text.Replace("z", "D")
.
Upvotes: 1
Reputation: 6989
Modify each of your
textBox7.Text.Replace("z", "D");
with
textBox7.Text = textBox7.Text.Replace("z", "D");
Upvotes: 4
Reputation: 48596
The problem is Replace()
returns a new string
with the replacement made. It does not update the string
on which it operates, and so you have to do that yourself:
if (comboBox1.Text == "0") textBox6.Text = textBox6.Text.Replace("z", "B");
Making that change (i.e. assigning the result back to textBox6.Text
/textBox7.Text
) will make your code work as expected.
Upvotes: 7