请教c#WinForm 中ComboBox取值

请教c#WinForm 中ComboBox取值,第1张

以下方法实现获取ComboBox中被选中项的值:

(1)在Visual Studio中新建一个“Windows 窗体应用程序”项目

(2)在项目中添加一个类MyItem。这个类有两个用途:

在ComboBox中显示

用于检索被选中项的值

MyItemcs代码

namespace WindowsFormsApplication1

{

class MyItem

{

public MyItem(string name, int value)

{

Name = name;

Value = value;

}

public string Name { get; private set; }

public int Value { get; private set; }

}

}

(3)在Form1上布置一个ComboBox、一个Label

(4)窗体代码 Form1cs

using System;

using SystemCollectionsGeneric;

using SystemWindowsForms;

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

// 清空lable1

label1Text = stringEmpty;

// 列表集合将作为comboBox1的数据源

List<MyItem> list = new List<MyItem>();

listAdd(new MyItem("张三", 10));

listAdd(new MyItem("李四", 20));

listAdd(new MyItem("王五", 30));

// 绑定

comboBox1DataSource = list;

// 在comboBox1中显示MyItem的Name属性

comboBox1DisplayMember = "Name";

}

// 获取被选中项的Value值

private void comboBox1_SelectedIndexChanged(

object sender,

EventArgs e)

{

// 将被选中的项目强制转换为MyItem

MyItem item = comboBox1SelectedItem as MyItem;

// 显示被选中项的值

label1Text = stringFormat("Value = {0}", itemValue);

}

}

}

(5)运行

程序启动后

改变comboBox1选择

简单说下:

命名空间:using SystemXml;

程序里加载XML文档

XmlDocument document = new XmlDocument();

documentLoad(string filename);

读取节点内容:

private void RecurseXmlDocument(XmlNode root,int indent)

{

//make sure root is null,do nothing;

if (root == null)

return;

if (root is XmlElement) //if root is XmlElement;

{

//first,print name

listBoxXmlNodesItemsAdd(rootNamePadLeft(rootNameLength + indent));

//then,check if there are any child nodes and if there are

//call this method again to print them

if (rootHasChildNodes)

RecurseXmlDocument(rootFirstChild, indent + 2);

//finally check if there are any siblings,if there are

//call this method again to print them

if (rootNextSibling != null)

RecurseXmlDocument(rootNextSibling, indent);

}

else if(root is XmlText)

{

//print text

string text = ((XmlText)root)Value;

listBoxXmlNodesItemsAdd(textPadLeft(textLength + indent));

}

else if(root is XmlComment)

{

string text = ((XmlComment)root)Value;

listBoxXmlNodesItemsAdd(textPadLeft(textLength + indent));

if (rootHasChildNodes)

RecurseXmlDocument(rootFirstChild, indent + 2);

//finally check if there are any siblings,if there are

//call this method again to print them

if (rootNextSibling != null)

RecurseXmlDocument(rootNextSibling, indent);

}

}

如果你要程序示例,我可以发给你.希望能帮到你.

checkedListBox1SelectedItemToString();

checkedListBox1SelectedItems[0]ToString();

这两种方式都可以,不懂可以追问或HI聊,但请不要关闭问题,谢谢!

以上就是关于请教c#WinForm 中ComboBox取值全部的内容,包括:请教c#WinForm 中ComboBox取值、c# winform 取得xml的值、在c#winform中怎样获取checklistbox选中的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9799279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存