Fast access for your controls (C# code).

It'd be more efficient to do it like this:
Code:
foreach (TextBox textBox in Controls)
{
    // ...
}
I can't work out why you'd want to iterate over every control and explicitly check the type name against a hard coded string... :confused:

Why would you want to enumerate all of your textboxes in the first place though?

edit: no animate sigs allowed here ;)
 
Last edited:
I am a new user here so you don't know me :)
I share a freeware program for o/c=OcBible anyway the forms of OcBible include many textboxes.
For example Variation Analysis has 700! :eek:
Thus it is boring to write a code
Code:
.
.
.
do_this(textBox1);
do_this(textBox2);
.
.
.
do_this(textBox700);

Inquisitor said:
no animate sigs allowed here ;)
OK I removed the shark!
It was a smile from other forum.
 
Last edited:
you still have to iterate over ALL of the controls on your form looking for controls that are TextBoxes and then use the values therein. The most efficient way of accessing a subset of the controls on a form is to add them to a separate collection somewhere and then access them through that rather than the controls collection.

Your code is definitely improving as the linked thread progresses though, keep it up ;) Have you tried running any of your code through a profiler to see if there is any difference in the speed of it? you can always set up a clock in another thread and print the current time to a debug file in every loop and see which is faster. Be very careful that your debug code doesn't slow the whole application down though :eek:

Paul
 
happytechie said:
The most efficient way of accessing a subset of the controls on a form
I think (that's why I added a panel --> the 11th post at Aoaforums) is the same solution or am I wrong? :confused:
Paul the most important thing is the source code size.
The forms are being smaller and smaller.
 
MrSeanKon said:
I think (that's why I added a panel --> the 11th post at Aoaforums) is the same solution or am I wrong? :confused:
Paul the most important thing is the source code size.
The forms are being smaller and smaller.

source code size ????

source code size is irrelevant to just about everything.

yes you care only going through the controls in the panel, but you are still going through all of the controls, labels, text boxes, buttons and everything else. It would be much more efficient to keep track of the values that you need for your algorithm somewhere else rather than just checking all of the values in a collection of text boxes.

Have you thought about my other sugestions? profiling will tell you that none of these changes will actually make a blind bit of difference to the performance of your form. what I would do is grab the text in the text box as it was typed in and add it to the variable in question, this way every time the user leaves a text box, the value can be updated and the time the user has to wait for the answer is reduced to 0.

Most issues like this can be solved by better design rather than trying to optimise a bad solution.
 
If you want fast code do not use for each use simple for loop.

Also if you have say 700 textboxes they no doubt are created dynamically at runtime, so you could easily at creation put them in an array and use this for reading the textboxes. It will be much faster than using for each on the control collection.
 
eriedor said:
If you want fast code do not use for each use simple for loop.
Thanks for tip.

eriedor said:
Also if you have say 700 textboxes they no doubt are created dynamically at runtime
WOW a trick!
I will play with this dude thanks again.
But I am not a professional programmer like you I just started learning Windows programming two years ago.

happytechie said:
Have you thought about my other sugestions?
No mate I have not but I will do this in the future versions.
Thanks all for your suggestions :)
 
Last edited:
Code:
	private void button1_Click(object sender, System.EventArgs e)
	{
		double sum=0.0;

		sum=Read_textboxes(textBox1);        // Instead of four calls how we can do the same thing
		sum+=Read_textboxes(textBox2);      // using a for loop? For this example this is not a
		sum+=Read_textboxes(textBox3);      // problem but if the program has 700 different 
		sum+=Read_textboxes(textBox4);      // textboxes or 1000 comboboxes too many calls GKR...
		MessageBox.Show("Summary is " + sum.ToString(),"WOW!");
	}
	private double Read_textboxes (TextBox txtbox)
	{
		try
		{
        			return double.Parse(txtbox.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
		}
		catch
		{
			return 0.0;
		}
	}

Is that a joke? :D Using floating point arithmetic to access a textbox? Seriously? Just iterate through the Form's control collection... something like:

Code:
List<TextBox> GetAllTextBoxesOnForm(Form myForm) {
   List<TextBox> results = new List<TextBox>(myForm.Controls.Count);

   foreach (Control tmp in myForm.Controls) {
      TextBox tb = tmp as TextBox;
      if (tb != null)
         // ooh we've found a text box! let off some fireworks!
         results.add(tb)
   }//foreach

   return results;
}//method
 
As said above though an index based loop would be quicker and depending on what you really want to do with the controls an index based array will be quicker than a list.
 
NathanE said:
Code:
	private void button1_Click(object sender, System.EventArgs e)
	{
		double sum=0.0;

		sum=Read_textboxes(textBox1);        // Instead of four calls how we can do the same thing
		sum+=Read_textboxes(textBox2);      // using a for loop? For this example this is not a
		sum+=Read_textboxes(textBox3);      // problem but if the program has 700 different 
		sum+=Read_textboxes(textBox4);      // textboxes or 1000 comboboxes too many calls GKR...
		MessageBox.Show("Summary is " + sum.ToString(),"WOW!");
	}
	private double Read_textboxes (TextBox txtbox)
	{
		try
		{
        			return double.Parse(txtbox.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
		}
		catch
		{
			return 0.0;
		}
	}

Is that a joke? :D Using floating point arithmetic to access a textbox? Seriously? Just iterate through the Form's control collection... something like:

Code:
List<TextBox> GetAllTextBoxesOnForm(Form myForm) {
   List<TextBox> results = new List<TextBox>(myForm.Controls.Count);

   foreach (Control tmp in myForm.Controls) {
      TextBox tb = tmp as TextBox;
      if (tb != null)
         // ooh we've found a text box! let off some fireworks!
         results.add(tb)
   }//foreach

   return results;
}//method

Totally agree there mate

Stelly
 
NathanE said:
Is that a joke? :D Using floating point arithmetic to access a textbox? Seriously? Just iterate through the Form's control collection... something like:

Code:
List<TextBox> GetAllTextBoxesOnForm(Form myForm) {
   List<TextBox> results = new List<TextBox>(myForm.Controls.Count);

   foreach (Control tmp in myForm.Controls) {
      TextBox tb = tmp as TextBox;
      if (tb != null)
         // ooh we've found a text box! let off some fireworks!
         results.add(tb)
   }//foreach

   return results;
}//method

I don't know why this has turned into such a drama. This is only really necesary when using a large number of textboxes which would suggest they are created at runtime like
Code:
for (int i=0;i<700;i++)
{
   textbox txtnew = new textbox();
   txtnew.name = txt + i.ToString();
   Form1.Controls.add(txtnew);
}

That code is untested as it's off the top of my head so might be slightly diff. All you would need to add is also add them to an array to allow quick access, giving.
Code:
Textbox[] textboxarray = new Textbox[700]
for (int i=0;i<700;i++)
{
   textbox txtnew = new textbox();
   txtnew.name = txt + i.ToString();
   Form1.Controls.add(txtnew);
   textboxarray[i] = txtnew;
}
Now to access textbox 567 it is simple
Code:
txtboxarray[567].Text = "Blah";
Don't need to worry about the controls list. Also if you want to search for a textbox based on its contents or position you could use a predicate and Array.Find() etc.
 
Thanks again all people for the new tips. :)
NathanE I am not a professional programmer like you.
I mentioned this above. :D
 
Last edited:
Back
Top Bottom