Fill multiple comboboxes with identical information
-
Good morning, As part of my conduit fill program, I have 8 comboboxes that require the same data filled into each one. They all have the same naming convention, but numerically different. cbo120VACType_1 cbo120VACType_2 cbo120VACType_3 cbo120VACType_4 cbo120VACType_5 cbo120VACType_6 cbo120VACType_7 cbo120VACType_8 How can I setup the naming to be dynamic in my program, so that it will fill each one, one at a time.
Richard Disable Vet Grandfather Pain in the @ss
-
Good morning, As part of my conduit fill program, I have 8 comboboxes that require the same data filled into each one. They all have the same naming convention, but numerically different. cbo120VACType_1 cbo120VACType_2 cbo120VACType_3 cbo120VACType_4 cbo120VACType_5 cbo120VACType_6 cbo120VACType_7 cbo120VACType_8 How can I setup the naming to be dynamic in my program, so that it will fill each one, one at a time.
Richard Disable Vet Grandfather Pain in the @ss
The simplest way is to use an array / List of ComboBox items and preload it with the boxes to be filled. Then a simple
foreach
will do what you want."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Good morning, As part of my conduit fill program, I have 8 comboboxes that require the same data filled into each one. They all have the same naming convention, but numerically different. cbo120VACType_1 cbo120VACType_2 cbo120VACType_3 cbo120VACType_4 cbo120VACType_5 cbo120VACType_6 cbo120VACType_7 cbo120VACType_8 How can I setup the naming to be dynamic in my program, so that it will fill each one, one at a time.
Richard Disable Vet Grandfather Pain in the @ss
Is this the same data you were previously considering how to store? A lot depends on how much data, how much the data changes, and in how many places you need the data. Suppose this one form with the eight combo boxes is the only place where you will need this data. You had mentioned building some kind of calculator or estimator, so it could conceivably consist of just one form. If that's the case, you might consider just manually entering the data in the form designer. Each combo box lets you type in the choices in the list. A step up from that would be to put the values in an array. Then use that array as the data source to a BindingSource. Then use that binding source as the data source to the first combo box. Repeat the process for the other seven combo boxes. You need one array, but you need a binding source for each combo box. This is because a binding source has only one current position, so if you use the same binding source for more than one combo box, setting one combo box will immediately set all the other combo boxes to the same selected item. Here's a similar question with code. c# - Populate a combobox with array information - Stack Overflow[^]
-
Good morning, As part of my conduit fill program, I have 8 comboboxes that require the same data filled into each one. They all have the same naming convention, but numerically different. cbo120VACType_1 cbo120VACType_2 cbo120VACType_3 cbo120VACType_4 cbo120VACType_5 cbo120VACType_6 cbo120VACType_7 cbo120VACType_8 How can I setup the naming to be dynamic in my program, so that it will fill each one, one at a time.
Richard Disable Vet Grandfather Pain in the @ss
If it's "exactly the same", you need to create only one list in memory; you can then assign that same list to all combo boxes at the same time (.ItemSource). The CBO's will navigate the same source independently. Yes, you could use an array to assign the .ItemSource, but with 8 CBO's is not worth the effort. [c# - Bind a List<string> to a combobox - Stack Overflow](https://stackoverflow.com/questions/7025234/bind-a-liststring-to-a-combobox)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Is this the same data you were previously considering how to store? A lot depends on how much data, how much the data changes, and in how many places you need the data. Suppose this one form with the eight combo boxes is the only place where you will need this data. You had mentioned building some kind of calculator or estimator, so it could conceivably consist of just one form. If that's the case, you might consider just manually entering the data in the form designer. Each combo box lets you type in the choices in the list. A step up from that would be to put the values in an array. Then use that array as the data source to a BindingSource. Then use that binding source as the data source to the first combo box. Repeat the process for the other seven combo boxes. You need one array, but you need a binding source for each combo box. This is because a binding source has only one current position, so if you use the same binding source for more than one combo box, setting one combo box will immediately set all the other combo boxes to the same selected item. Here's a similar question with code. c# - Populate a combobox with array information - Stack Overflow[^]
He's using WPF; "BindingSource" is Windows Forms (navigation).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Good morning, As part of my conduit fill program, I have 8 comboboxes that require the same data filled into each one. They all have the same naming convention, but numerically different. cbo120VACType_1 cbo120VACType_2 cbo120VACType_3 cbo120VACType_4 cbo120VACType_5 cbo120VACType_6 cbo120VACType_7 cbo120VACType_8 How can I setup the naming to be dynamic in my program, so that it will fill each one, one at a time.
Richard Disable Vet Grandfather Pain in the @ss
You should say if you're using Windows Forms or WPF or UWP, etc.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You should say if you're using Windows Forms or WPF or UWP, etc.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You may be right. I thought WPF (probably because I looked at another WPF post first. I hate when I do that).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You may be right. I thought WPF (probably because I looked at another WPF post first. I hate when I do that).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
I was just surprised to hear WPF doesn't use Binding Source. I guess I'll find out what it uses when they pry WinForms out of my cold, dead hands. :)
The WPF CB's have an ItemSource property which can share a collection and still maintain their own positioning. So it's easier. :)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I