【问题标题】:Is it possible to select datatype in comobox in c# web application是否可以在 C# Web 应用程序的组合框中选择数据类型
【发布时间】:2014-05-15 01:47:10
【问题描述】:

我是c# 的初学者,正在使用 Visual Studio-2010 中的 Silver Light-5 进行 Web 开发。我有我的 GUI 运行我的代码,它的 GUI 由 xaml 创建,并且按钮点击在 c# 中处理。

现在我要做的是:(我有 2 个问题)

(1) 第一个是: 我正在尝试创建一个 GUI,在其中我使用组合框,其中将包含这样的选项(请参阅此链接)http://prntscr.com/36l58s 在此链接中,我在给定的 5 个datatypes 中选择一种数据类型(字节,sbyte,短,整数,长)。之后,我想在 c# 代码中将此数据类型分配给variable,如下所示:(假设我在其中选择了“short”)

comboBox1.Items.Add("short");
var itemType = comboBox1.SelectedItem.GetType(); //This "itemType" contains "short" now
itemType variable = 10; //  **THIS LINE GIVES ERROR**

如何在combo box 中为这个“vraibale”分配选定的数据类型?

(2) 第二个是: 当我选择“短”(或任何数据类型)时,它会再次重复在组合框中添加数据类型。例如,当我选择“短”(或任何)时。我得到了这个http://prntscr.com/36l6om,如果我再次选择“long”,我会选择这个http://prntscr.com/36l75y

我要实现的xml代码是这样的

    <ComboBox Height="19" HorizontalAlignment="Left" Margin="25,209,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectionChanged" Grid.ColumnSpan="3">
        <ComboBoxItem />
        <ComboBoxItem />
    </ComboBox>

而c#代码是:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            comboBox1.Items.Add("byte");
            comboBox1.Items.Add("sbyte");
            comboBox1.Items.Add("short");
            comboBox1.Items.Add("int");
            comboBox1.Items.Add("long");

            var itemType = comboBox1.SelectedItem.GetType();   
        }

提前感谢您的帮助。

【问题讨论】:

  • 我对您的帖子有几个问题:1) 您有一个包含一些选项的组合框。为什么在 SelectionChanged 事件处理程序 comboBox1_SelectionChanged 上,您试图在组合框控件的 Items 集合中添加这些选项? 2)您想在哪里使用组合框的选定选项?我的意思是你想获得组合框的选定选项。这很清楚。但是您以后想在哪里使用它。谢谢
  • @ChristosPaisios 感谢您的回复。答案是(1)实际上我是 webapplication 的新手(以及comobox),我只是想创建一个 GUI,我可以在运行时使用组合框选择变量的数据类型。然后将此选定的数据类型分配给我在 c# 中的代码中的一个变量。(实际上我正在读取我的二进制文件,我必须选择存储在 16/32/64 位整数中的字节读取,必须由 COMBO Box 选择)。你能帮我做吗?

标签: c# silverlight web-applications combobox silverlight-5.0


【解决方案1】:

首先,

comboBox1.SelectedItem.GetType();

会给你Typestring

一个出路可能是一个开关盒

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        switch( comboBox1.SelectedItem as string  )
        {
            case "byte"://Create  a variable of byte and use it. 
                break;
            case "sbyte"://Create  a variable of sbyte and use it.
                break;
            case "short"://Create  a variable of short and use it.
                break;
            case "int"://Create  a variable of int and use it.
                break;
            case "long"://Create  a variable of long and use it.
            default:
                break;
        }
}

其次,将Items 添加到InitializeConstructor 方法中的combobox1,而不是SelectionChanged 事件,这会导致重复添加。

【讨论】:

  • 让我试试,我会尽快回来标记它。
  • 感谢第二部分,但第一部分不起作用,因为我必须使用在 comboBox 中选择的数据类型来分配给变量以使其成为该特定数据类型(在运行时由组合框)。如果我在您的任何 switch 语句中执行此操作“comboBox1 variable =10;”我的意思是变量被分配了一个由 ComboBox 在运行时选择的数据类型。有可能做到这一点吗? (请参阅我问题第一部分的最后一行代码,您将了解我想要做什么。(这个:“itemType variable = 10;”))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
相关资源
最近更新 更多