Click here to Skip to main content
15,896,207 members
Home / Discussions / C#
   

C#

 
GeneralOR Pin
Ennis Ray Lynch, Jr.14-Jan-10 11:25
Ennis Ray Lynch, Jr.14-Jan-10 11:25 
AnswerRe: Need help for these questions Pin
Pete O'Hanlon14-Jan-10 10:51
mvePete O'Hanlon14-Jan-10 10:51 
QuestionDynamically referencing a control - how do I do this (better)? Pin
GuyThiebaut14-Jan-10 10:00
professionalGuyThiebaut14-Jan-10 10:00 
AnswerRe: Dynamically referencing a control - how do I do this (better)? Pin
OriginalGriff14-Jan-10 10:32
mveOriginalGriff14-Jan-10 10:32 
GeneralRe: Dynamically referencing a control - how do I do this (better)? Pin
GuyThiebaut14-Jan-10 10:42
professionalGuyThiebaut14-Jan-10 10:42 
GeneralRe: Dynamically referencing a control - how do I do this (better)? Pin
Eddy Vluggen14-Jan-10 11:58
professionalEddy Vluggen14-Jan-10 11:58 
GeneralRe: Dynamically referencing a control - how do I do this (better)? Pin
GuyThiebaut14-Jan-10 19:07
professionalGuyThiebaut14-Jan-10 19:07 
AnswerRe: Dynamically referencing a control - how do I do this (better)? Pin
Luc Pattyn14-Jan-10 13:12
sitebuilderLuc Pattyn14-Jan-10 13:12 
Hi,

You have two nested loops, which is one too many for the job at hand.
You either drop the loop over controls (that is what Griff suggested), or even simpler, you drop the one looping over the bubbles, like so:

foreach (Control ctrl in this.groupBox8.Controls) {
    pb = ctrl as PictureBox;
    if (pb != null && LeftRightMid.Left(ctrl.Name, 4) == "thmb") {
        int i = Convert.ToInt32(LeftRightMid.Right(ctrl.Name, 1));
        pb.ImageLocation = bubble.thumbs[i];
    }
}


Note:
- I used "as" instead of "is", removing the need for later casts
- I dropped some ToString() calls, as the object already was a string
- I use "i" as an index, no need to repeat that in a loop
- this code should be faster than Griff's one, as his Controls[name] lookup takes much more effort than a simple array index operation.

BTW: I doubt ANY of the versions shown will work correctly for N>=10 as you only are using one character to extract the thmb number from the control's name. The fix for that would be to take the substring that starts at the position (hence counting from the left side, not the right side); string.SubString() is what you would use then.

Smile | :)

Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

Happy New Year to all.
We hope 2010 soon brings us automatic PRE tags!
Until then, please insert them manually.


GeneralRe: Dynamically referencing a control - how do I do this (better)? Pin
GuyThiebaut14-Jan-10 19:05
professionalGuyThiebaut14-Jan-10 19:05 
GeneralRe: Dynamically referencing a control - how do I do this (better)? Pin
Luc Pattyn15-Jan-10 0:41
sitebuilderLuc Pattyn15-Jan-10 0:41 
Questionspeeech recognition project Pin
salman_kh14-Jan-10 8:14
salman_kh14-Jan-10 8:14 
AnswerRe: speeech recognition project Pin
Rick Shaub14-Jan-10 8:38
Rick Shaub14-Jan-10 8:38 
GeneralRe: speeech recognition project Pin
salman_kh14-Jan-10 8:54
salman_kh14-Jan-10 8:54 
GeneralRe: speeech recognition project Pin
Ennis Ray Lynch, Jr.14-Jan-10 9:18
Ennis Ray Lynch, Jr.14-Jan-10 9:18 
AnswerRe: speeech recognition project Pin
Saksida Bojan14-Jan-10 8:42
Saksida Bojan14-Jan-10 8:42 
AnswerRe: speeech recognition project Pin
dan!sh 14-Jan-10 8:45
professional dan!sh 14-Jan-10 8:45 
JokeRe: speeech recognition project Pin
Richard MacCutchan14-Jan-10 9:01
mveRichard MacCutchan14-Jan-10 9:01 
AnswerRe: speeech recognition project Pin
GuyThiebaut14-Jan-10 10:27
professionalGuyThiebaut14-Jan-10 10:27 
SuggestionRe: speeech recognition project Pin
Sreekanth12345678927-Jan-12 3:25
Sreekanth12345678927-Jan-12 3:25 
QuestionFoxPro To SQL Server Pin
427748014-Jan-10 5:21
427748014-Jan-10 5:21 
AnswerRe: FoxPro To SQL Server Pin
The Man from U.N.C.L.E.14-Jan-10 6:31
The Man from U.N.C.L.E.14-Jan-10 6:31 
QuestionC# CODE SNIPET FOR RUN AS ADMINISTRATOR Pin
Omoniyi Ogunderu14-Jan-10 3:00
Omoniyi Ogunderu14-Jan-10 3:00 
AnswerRe: C# CODE SNIPET FOR RUN AS ADMINISTRATOR Pin
Mirko198014-Jan-10 3:14
Mirko198014-Jan-10 3:14 
AnswerRe: C# CODE SNIPET FOR RUN AS ADMINISTRATOR Pin
dan!sh 14-Jan-10 3:14
professional dan!sh 14-Jan-10 3:14 
AnswerRe: C# CODE SNIPET FOR RUN AS ADMINISTRATOR Pin
Not Active14-Jan-10 4:50
mentorNot Active14-Jan-10 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.