Click here to Skip to main content
15,860,859 members
Home / Discussions / C#
   

C#

 
Questioncan't add combobox Items (C#) Pin
Dunnewijk22-Nov-17 3:43
Dunnewijk22-Nov-17 3:43 
AnswerRe: can't add combobox Items (C#) Pin
Pete O'Hanlon22-Nov-17 4:48
subeditorPete O'Hanlon22-Nov-17 4:48 
GeneralRe: can't add combobox Items (C#) Pin
Dunnewijk22-Nov-17 5:04
Dunnewijk22-Nov-17 5:04 
GeneralRe: can't add combobox Items (C#) Pin
Dave Kreskowiak22-Nov-17 5:15
mveDave Kreskowiak22-Nov-17 5:15 
GeneralRe: can't add combobox Items (C#) Pin
Sascha Lefèvre22-Nov-17 5:20
professionalSascha Lefèvre22-Nov-17 5:20 
GeneralRe: can't add combobox Items (C#) Pin
Dunnewijk22-Nov-17 6:56
Dunnewijk22-Nov-17 6:56 
GeneralRe: can't add combobox Items (C#) Pin
Sascha Lefèvre22-Nov-17 7:55
professionalSascha Lefèvre22-Nov-17 7:55 
AnswerRe: can't add combobox Items (C#) Pin
Dave Kreskowiak22-Nov-17 5:02
mveDave Kreskowiak22-Nov-17 5:02 
Why on earth are you adding items to a combo box in a method that is named Getsomething and the method doesn't return anything?

Your GetcobBox method is also naive. It will only look in top-level controls on a form. If they are in containers, such as a Panel or GroupBox, they won't be found.

You've got a lot of learning to do. You're creating a ComboBox control in your GetcboBox method, then throwing it away. You're also not using a logical AND operator in your if statement, you're using a binary AND operator (&).

Your GetcboBox method should be something more like this:
C#
private ComboBox GetComboBoxByName(string name)
{
    foreach (Control control in this.Controls)
    {
        if (control is ComboBox && control.Name == name)
        {
            return control;
        }

        if (control.HasChildren)
        {
            return GetComboBoxByName(name);
        }
    }

    return null;
}

But, there's an even easier way to do it by having the .NET Framework do the work for you. Since you can't have two controls on the form with the exact same name, this will either return the control you're looking for, return null, or throw an exception because the control that was found isn't a ComboBox:
C#
private ComboBox GetComboBoxByName(string name)
{
    Control[] candidates = this.Controls.Find(name, true);
    
    return (ComboBox)candidates.FirstOrDefault();
}

Keep in mind, this is an example and is not meant to by used in a production application.
System.ItDidntWorkException: Something didn't work as expected.

C# - How to debug code[^].
Seriously, go read these articles.

Dave Kreskowiak

GeneralRe: can't add combobox Items (C#) Pin
Dunnewijk22-Nov-17 6:59
Dunnewijk22-Nov-17 6:59 
QuestionHow to filter like or = with a column in reportviewer C# Pin
C Sharp coder 201821-Nov-17 4:56
C Sharp coder 201821-Nov-17 4:56 
Rant[REPOST] How to filter like or = with a column in reportviewer C# Pin
Richard Deeming21-Nov-17 5:03
mveRichard Deeming21-Nov-17 5:03 
Questionhow i distinguish scan or keyboard value ? in windows ce ?? Pin
Member 1284514418-Nov-17 1:52
Member 1284514418-Nov-17 1:52 
AnswerRe: how i distinguish scan or keyboard value ? in windows ce ?? Pin
OriginalGriff18-Nov-17 2:22
mveOriginalGriff18-Nov-17 2:22 
AnswerRe: how i distinguish scan or keyboard value ? in windows ce ?? Pin
jschell20-Nov-17 6:30
jschell20-Nov-17 6:30 
GeneralNullable reference types in C# Pin
jschell17-Nov-17 10:38
jschell17-Nov-17 10:38 
GeneralRe: Nullable reference types in C# Pin
Eddy Vluggen18-Nov-17 0:54
professionalEddy Vluggen18-Nov-17 0:54 
GeneralRe: Nullable reference types in C# Pin
jschell20-Nov-17 6:10
jschell20-Nov-17 6:10 
GeneralRe: Nullable reference types in C# Pin
Eddy Vluggen20-Nov-17 6:23
professionalEddy Vluggen20-Nov-17 6:23 
GeneralRe: Nullable reference types in C# Pin
jschell20-Nov-17 6:36
jschell20-Nov-17 6:36 
GeneralRe: Nullable reference types in C# Pin
Eddy Vluggen20-Nov-17 6:37
professionalEddy Vluggen20-Nov-17 6:37 
QuestionC# and SQL application help Pin
Member 1352717817-Nov-17 6:35
Member 1352717817-Nov-17 6:35 
GeneralRe: C# and SQL application help Pin
Sascha Lefèvre17-Nov-17 7:07
professionalSascha Lefèvre17-Nov-17 7:07 
AnswerRe: C# and SQL application help Pin
OriginalGriff17-Nov-17 8:07
mveOriginalGriff17-Nov-17 8:07 
QuestionRe: C# and SQL application help Pin
Member 1352717817-Nov-17 11:13
Member 1352717817-Nov-17 11:13 
AnswerRe: C# and SQL application help Pin
OriginalGriff17-Nov-17 23:13
mveOriginalGriff17-Nov-17 23:13 

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.