如何获取combobox选中的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取combobox选中的值相关的知识,希望对你有一定的参考价值。
1.添加combobox后做如下设置2.根据自己的需要在类中重写如下方法
// ==========================================================
// Combo box data source methods
// ==========================================================
- (NSInteger) numberOfItemsInComboBox: (NSComboBox *) aComboBox
#pragma unused (aComboBox)
return ([dataSource count]);
- (id) comboBox: (NSComboBox *) aComboBox objectValueForItemAtIndex: (NSInteger) index
#pragma unused (aComboBox)
return [dataSource objectAtIndex:index];
- (NSUInteger) comboBox: (NSComboBox *) aComboBox indexOfItemWithStringValue: (NSString *) string
#pragma unused (aComboBox)
return [dataSource indexOfObject:string];
- (NSString *) firstGenreMatchingPrefix: (NSString *) prefix
NSString *string = nil;
NSString *lowercasePrefix = [prefix lowercaseString];
NSEnumerator *stringEnum = [dataSource objectEnumerator];
while ( string = [stringEnum nextObject] )
if ([[string lowercaseString] hasPrefix:lowercasePrefix] )
return (string) ;
return (nil);
// firstGenreMatchingPrefix
- (NSString *) comboBox: (NSComboBox *) aComboBox completedString: (NSString *) inputString
#pragma unused (aComboBox)
// This method is received after each character typed by the user, because we have checked the "completes" flag for
// genreComboBox in IB.
// Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete
// string.
NSString *candidate = [self firstGenreMatchingPrefix:inputString];
return (candidate ? candidate : inputString);
这些方法实现combobox在选择选项、自动完成时的处理方式。其中dataSource为NSArray。
3.设置combobox的datasource为实现上述方法的对象即可。 参考技术A string bianlian; //定义一个string 类型的变量
bianlian=combobox.selecteditem.text ;把下拉列表框中的值读出赋给变量名bianliang本回答被提问者和网友采纳 参考技术B
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
//添加数据项
comboBox1.Items.Add("apple");
comboBox1.Items.Add("banana");
private void button1_Click(object sender, EventArgs e)
MessageBox.Show(comboBox1.SelectedItem.ToString());
以上是关于如何获取combobox选中的值的主要内容,如果未能解决你的问题,请参考以下文章