30

How do I find an item by value in an combobox in C#?

How do I find an item by value in an combobox in C#? – Stack Overflow

Join Stack Overflow to learn, share knowledge, and build your career.

Asked 9 years ago

Viewed 92k times

In C#, I have variable, a, of type string.

How do I find item by value of a in combobox (I want find item with value no display text of combobox).

asked Apr 15 ’12 at 8:53

Duy KhanhDuy Khanh

30411 gold badge44 silver badges1111 bronze badges

2

You can find it by using the following code.

int index = comboBox1.Items.IndexOf(a);

To get the item itself, write:

comboBox1.Items[index];

answered Apr 15 ’12 at 9:13

st mnmnst mnmn

3,24533 gold badges2222 silver badges3232 bronze badges

You should see a method on the combo box control for FindStringExact(), which will search the displaymember and return the index of that item if found. If not found will return -1.

//to select the item if found: mycombobox.SelectedIndex = mycombobox.FindStringExact(“Combo”); //to test if the item exists: int i = mycombobox.FindStringExact(“Combo”); if(i >= 0) { //exists }

answered Jan 8 ’15 at 21:24

1

I know my solution is very simple and funny, but before I train I used it. Important: DropDownStyle of combobox must be “DropDownList”!

First in combobox and then:

bool foundit = false; String mystr = “item_1”; mycombobox.Text = mystr; if (mycombobox.SelectedText == mystr) // Or using mycombobox.Text foundit = true; else foundit = false;

It works for me right and resolved my problem… But the way (solution) from @st-mnmn is better and fine.

answered Feb 13 ’14 at 19:10

user3290286user3290286

58911 gold badge66 silver badges88 bronze badges

Hi Guys the best way if searching for a text or value is

int Selected = -1; int count = ComboBox1.Items.Count; for (int i = 0; (i<= (count - 1)); i++) { ComboBox1.SelectedIndex = i; if ((string)(ComboBox1.SelectedValue) == "SearchValue") { Selected = i; break; } } ComboBox1.SelectedIndex = Selected;

answered Feb 12 ’18 at 7:31

Teezy7Teezy7

51555 silver badges99 bronze badges

1

Your Answer

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved

Draft discarded

Post as a guest

lang-cs

{};’)}catch(a){return!0}}function o(){return’undefined’!=typeof googletag&&!!googletag.apiReady}function p(){o()||(googletag={cmd:l(B)})}function q(){var a=document.createElement(‘div’);a.className=’adsbox’,a.id=’clc-abd’,a.style.position=’absolute’,a.style.pointerEvents=’none’,a.innerHTML=’ ‘,document.body.appendChild(a)}function r(){return Object.keys(F.ids).filter(function(a){return’clc-cpa’!=a})}function s(a){var b=a.split(‘_’)[0],c=F.ids[b],d=F.slots[c];’function’==typeof d&&(d=d(b));return{path:’/’+C+’/’+E+’/’+c+’/’+D,sizes:d,zone:c}}function t(a){try{Array.isArray(clc.dfp.slotsRenderedEvents)||(clc.dfp.slotsRenderedEvents=[]),clc.dfp.slotsRenderedEvents.push(a);var b=a.slot.getSlotElementId(),c=[];b||c.push(‘id=0’);var d=document.getElementById(b);if(!b||d?d.hasAttribute(‘data-clc-stalled’)&&c.push(‘st=1’):c.push(‘el=0’),0!==c.length)return void G(c.join(‘&’));var e=s(b),f=e.zone;if(clc.collapse&&clc.collapse[f]&&a.isEmpty)return h(d),void d.setAttribute(‘data-clc-ready’,’true’);if(-1!==y.dh.indexOf(a.lineItemId))h(d);else if(a.lineItemId){d.setAttribute(‘data-clc-prefilled’,’true’);var j=d.parentElement;if(j.classList.contains(‘js-zone-container’)){g(j);var k=j.querySelectorAll(‘.js-report-ad-button-container’),l=k[0];switch(l.style.height=’24px’,b){case’dfp-tlb’:case’dfp-tag’:{j.classList.add(‘mb8′);break}case’dfp-mlb’:case’dfp-smlb’:case’dfp-bmlb’:{j.classList.add(‘my8′);break}case’dfp-isb’:{j.classList.add(‘mt24′);break}case’dfp-m-aq’:{j.classList.add(‘my12’),j.classList.add(‘mx-auto’);break}default:}i(j),i(d)}else i(d);if(‘dfp-msb’==b){var m=document.getElementById(‘hireme’);h(m)}}d.setAttribute(‘data-clc-ready’,’true’)}catch(a){var n=document.querySelector(‘#dfp-tsb, #dfp-isb, #clc-tsb’);n&&n.setAttribute(‘data-clc-ready’,’true’),G(‘e=1’)}}function u(a,b){‘dfp-isb’===a&&b.setTargeting(‘Sidebar’,[‘Inline’]),’dfp-tsb’===a&&b.setTargeting(‘Sidebar’,[‘Right’]);var c=s(a),d=c.path,e=c.sizes,f=c.zone,g=googletag.defineSlot(d,e,a);g.addService(b),!1;var h=a.split(‘_’);if(‘clc-cpa’==h[0]&&h[1]){var i=h[1];g.setTargeting(‘talent-company-id’,i)}}function v(b){var c=a.dfp&&a.dfp.targeting||{};’SystemDefault’===c.ProductVariant&&(window.matchMedia&&window.matchMedia(‘(prefers-color-scheme: dark)’).matches?c.ProductVariant=’Dark’:c.ProductVariant=’Light’),Object.keys(c).forEach(function(a){b.setTargeting(a,c[a])})}function w(a){var g=a.map(b).filter(e);return{eligible:g.filter(f).filter(d),ineligible:g.filter(c)}}function x(b){void 0===b&&(b=r());var c=[‘dfp-mlb’,’dfp-smlb’];if(!o())return p(),void googletag.cmd.push(function(){return x(b)});var d=w(b),e=d.eligible,f=d.ineligible;if(e.forEach(function(a){g(a)}),f.forEach(h),0!==e.length){y.abd&&q();var i=googletag.pubads().getSlots(),j=i.filter(function(a){return 0c.indexOf(a.id)}),m=e.filter(function(a){return!!y.ll&&0<=c.indexOf(a.id)});l.forEach(function(a){u(a.id,k),a.setAttribute('data-dfp-zone','true')}),googletag.enableServices(),l.forEach(function(a){googletag.display(a.id)}),y.ll&&(k.enableLazyLoad({fetchMarginPercent:0,renderMarginPercent:0}),m.forEach(function(a){u(a.id,k),a.setAttribute('data-clc-prefilled','true')}),m.forEach(function(a){googletag.display(a.id)}))}}var y=function(a){for(var b=[],c=1;c Stack Overflow works best with JavaScript enabled

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

 


How do I find an item by value in an combobox in C#?

More Story on Source:

* Source→ *

0

Publication author

offline 2 weeks

SFi Official

0
Comments: 0Publics: 1370Registration: 11-03-2021