• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Fast access for your controls (C# code).

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
Well if you place all the text boxes in own container you could probably save a cycle or two by avoiding additional conversion while calling the Read method or not call additional method altogether. Also you should probably kill the loop if your iterations are exceeded to not go thru controls that are irrelevant to your measurement count value. Not critical when you have relatively few controls but still why waste CPU time:


Code:
foreach ( TextBox iBox in this.MyTBPanel.Controls ){
    try {
        // Add value to sum
        sum += Double.Parse(iBox.Text);
    } catch {
        // Set flag 4 invalid entry
        flag = false;
        // Quit here - no point going further
        break;
    }
    if ( index++ > measurements )
        // Quit loop here - no point iterating further
        break;
}
if ( flag ){
    // Something for the flag
} else {
    // Something for error
}

Similarly to clear things up


Code:
foreach ( TextBox iBox in this.MyTBPanel.Controls )
    // Reset box value
    iBox.Text = "";

// Reset combobx value
comboBox.Text = "1";
 
Last edited:
Tacoman667 said:
Are you dynamically creating textboxes in your form or are they all created at design time?
No I don't but thanks for suggestion it is advanced for me cos I started learning Windows programming two years ago honestly I am not a professional programmer.
If I change the next edition of the code as you mentioned then its size will be smaller.
AtomicMonkey thanks for the code. :)
My approach is to finish as soon as possible (not enough free time) the most parts and then I will tweak it more.
 
Back