\n" textadd3.Name = "textBox3" + controlCounts; "\ni need both to be declare for my codes to work.
\ntextadd3.Name = "textBox3" + controlNames.Count + controlCounts;
\nSame Goes for Here\ntextadd4.Name = "textBox4" + controlNames.Count + controlCounts; // Check
\nif ((textBox3.Text != "") && (textBox4.Text != "")) // in this line of Code\nError: "System.NullReferenceException: 'Object reference not set to an instance of an object.' "
\n","author":{"@type":"Person","name":"Imba Gaming"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":null}}Reputation: 3
Hi the codes is running in both ControlName and ControlCounts ControlName Check the Idendity of everytextbox to be located in the form. while controlCounts is use only to multiply the intergers that users input in the textbox and display the result to another textBox Since im using a Dynamic to create a textBox i need first to allocate each box and create Text_Change for me to declare the private void UpdateTotal()
type here //this is the code to indentify and make a formula to multiply if what would the user input in the textbox.
int controlCounts = 0;
private void updateTotal(string index)
{
TextBox textBox3 = (TextBox)Controls["textBox3" + index];
TextBox textBox4 = (TextBox)Controls["textBox4" + index];
TextBox textBox5 = (TextBox)Controls["textBox5" + index];
if ((textBox3.Text != "") && (textBox4.Text != ""))
{
textBox5.Text = (Convert.ToInt32(textBox3.Text) * Convert.ToInt32(textBox4.Text)).ToString();
}
}
In The Next Code i will Declare the Text_Changed in the codes where i create a dynamic textBox : Notice in the textadd3.Name i put two ControlNames and ControlCounts this is where the problem happens i need both to be declare in the .Name since this will the verify for my codes to run. but if i remove the controlNames the ControlCounts wont error and it will work but i need them both for my codes to work since they have both usefull value.
TextBox textadd3 = new TextBox();
this.Controls.Add(textadd3);
textadd3.BackColor = System.Drawing.Color.White;
textadd3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd3.ForeColor = System.Drawing.Color.Black;
textadd3.Location = new System.Drawing.Point(1056, Top);
textadd3.Name = "textBox3" + controlNames.Count + controlCounts; // Check
textadd3.BringToFront();
textadd3.Size = new System.Drawing.Size(64, 24);
textadd3.TextAlign = HorizontalAlignment.Center;
textadd3.Text = "";
textadd3.TextChanged += textBox3_TextChanged; // text_Change
textboxes.Push(textadd3);
controlNames.Add(textadd3.Name);
TextBox textadd4 = new TextBox();
this.Controls.Add(textadd4);
textadd3.BackColor = System.Drawing.Color.White;
textadd4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd4.ForeColor = System.Drawing.Color.Black;
textadd4.Location = new System.Drawing.Point(1122, Top);
textadd4.Name = "textBox4" + controlNames.Count + controlCounts; // Check
textadd4.BringToFront();
textadd4.Size = new System.Drawing.Size(50, 24);
textadd4.TextAlign = HorizontalAlignment.Center;
textadd4.Text = "";
textadd4.TextChanged += textBox4_TextChanged; // text_Changed
textboxes.Push(textadd4);
controlNames.Add(textadd4.Name);
TextBox textadd5 = new TextBox();
this.Controls.Add(textadd5);
textadd5.BackColor = System.Drawing.Color.White;
textadd5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd5.ForeColor = System.Drawing.Color.Black;
textadd5.Location = new System.Drawing.Point(1174, Top);
textadd5.Name = "textBox5" + controlNames.Count;
textadd5.BringToFront();
textadd5.Size = new System.Drawing.Size(108, 24);
textadd5.TextAlign = HorizontalAlignment.Center;
textadd5.Text = "";
textboxes.Push(textadd5);
controlNames.Add(textadd5.Name);
//This is the Codes for Text_Changed since im using a dynamic format.
private void textBox3_TextChanged(object sender, EventArgs e) {
TextBox textBox3 = (TextBox)sender;
string index = textBox3.Name.Substring("textBox3".Length);
updateTotal(index);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
TextBox textBox4 = (TextBox)sender;
string index = textBox4.Name.Substring("textBox4".Length);
updateTotal(index);
}
All Codes are working if one are only call in the .Name
if i use this line of code it wont error and working but only 1 must select be declare only 1 not both
" textadd3.Name = "textBox3" + controlNames.Count; "
" textadd3.Name = "textBox3" + controlCounts; "
i need both to be declare for my codes to work.
textadd3.Name = "textBox3" + controlNames.Count + controlCounts;
Same Goes for Here textadd4.Name = "textBox4" + controlNames.Count + controlCounts; // Check
if ((textBox3.Text != "") && (textBox4.Text != "")) // in this line of Code Error: "System.NullReferenceException: 'Object reference not set to an instance of an object.' "
Upvotes: 0
Views: 30
Reputation: 3
It solve now i only need to put controlCounts also in the TextBox5 for it to formulate
textadd5.Name = "textBox5" + controlNames.Count;
textadd5.Name = "textBox5" + controlCounts;
textadd5.BringToFront();
textadd5.Size = new System.Drawing.Size(108, 24);
textadd5.TextAlign = HorizontalAlignment.Center;
textadd5.Text = "";
textboxes.Push(textadd5);
controlNames.Add(textadd5.Name);
Upvotes: 0