2017-09-25 69 views
-2

我尝试编写一个程序,您可以从下拉菜单中选择一个项目并返回其序列号。问题在于项目发生更改,这就是为什么我想从文件中读取值并将值填入下拉列表中。将项目添加到文件中的下拉菜单

我想象,我将有一个看起来是这样的文件:

Toaster;220: 
Microwave;3021: 

在这个例子中,我会分裂的产品和ID用分号,并用冒号数结束。下拉菜单将只显示产品(在这种情况下是烤面包机和微波炉),并将返回值220或3021 有什么简单的方法来实现在C#中?

+0

您可以创建文本(csv)-datasouce,并将其与文件链接。 –

+0

你应该进一步阅读string.split并阅读这个答案:https://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source –

+0

This问题太广泛了。 –

回答

1

它确实很容易做到这一点,但是您不会提供很多关于您在c#旁边使用的技术的信息。你是否试图在Web应用程序(如asp.net或asp.net核心)桌面应用程序(wpf,winforms)或uwp应用程序中执行此操作。如果是的话,你使用任何控件,如devexpress,infragistics,syncfusion,telerik ...?如果您提供更多关于您的工作环境的信息,我很乐意提供帮助,有很多方法可以做到这一点。因为您提到您正在尝试编写程序,所以我可以为您提供wpf或winforms应用程序的快速示例。你可以去Syncfusion.com并下载他们的控件,因为它们可以在非商业产品中免费使用,并且碰巧有很好的文档(安装非常容易,特别是如果你使用visual studio),那么你去创建winform syncfusion项目。然后查看所需事件的文档,以便更改选择。其他解决方法将在纯WinForms应用程序中这里是你如何做到这一点,首先你去创建新的应用程序,然后你添加一个组合框与选择更改事件和数据绑定选项。然后你创建加载事件的表单,将用于从文本文件中添加项目,然后你通常不需要,但是我更喜欢为我的新对象创建一个结构,如果你设法到达这里你可以添加一个文件阅读器读取文本,然后将信息绑定到刚创建的类的新列表。之后,您将项目列表绑定到组合框,并创建一个可以显示标识的标签。那么它在selectionchanged事件上的简单事件就是将所选项目转换为您创建的类,并将类的id绑定到标签,并且具有您所需的功能。你可以看看代码示例,我会提供

private List<FileLine> Source { get; set; } 

    public class FileLine 
    { 
     public string Text { get; set; } 
     public int Id { get; set; } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Source = GetFiles(); 
     comboBox1.Items.AddRange(Source.ToArray()); 
    } 

    public List<FileLine> GetFiles() 
    { 
     var files = new List<FileLine>(); 
     int counter = 0; 
     string line; 

     // Read the file and display it line by line. 
     System.IO.StreamReader file = 
      new System.IO.StreamReader("Items.txt"); 
     while ((line = file.ReadLine()) != null) 
     { 
      var item = line.Split(';').ToList(); 
      files.Add(new FileLine { Text = item.FirstOrDefault(), Id = int.Parse(item.LastOrDefault()) }); 
      counter++; 
     } 

     file.Close(); 
     return files; 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var item = comboBox1.SelectedItem as FileLine; 
     IdLabel.Text = item.Id.ToString(); 
    } 

这是我的winform1控制器如何:形式样子,如果你不希望打扰添加新项目的看法,你可以把它复制到一个新的与初始化组件内部名称Form1窗体,您可以用F12键

#region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.comboBox1 = new System.Windows.Forms.ComboBox(); 
     this.IdLabel = new System.Windows.Forms.Label(); 
     this.SuspendLayout(); 
     // 
     // comboBox1 
     // 
     this.comboBox1.DisplayMember = "Text"; 
     this.comboBox1.FormattingEnabled = true; 
     this.comboBox1.Location = new System.Drawing.Point(87, 64); 
     this.comboBox1.Name = "comboBox1"; 
     this.comboBox1.Size = new System.Drawing.Size(121, 21); 
     this.comboBox1.TabIndex = 0; 
     this.comboBox1.ValueMember = "Id"; 
     this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); 
     // 
     // IdLabel 
     // 
     this.IdLabel.AutoSize = true; 
     this.IdLabel.Location = new System.Drawing.Point(87, 128); 
     this.IdLabel.Name = "IdLabel"; 
     this.IdLabel.Size = new System.Drawing.Size(0, 13); 
     this.IdLabel.TabIndex = 1; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 261); 
     this.Controls.Add(this.IdLabel); 
     this.Controls.Add(this.comboBox1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Load += new System.EventHandler(this.Form1_Load); 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

    } 

    #endregion 

    private System.Windows.Forms.ComboBox comboBox1; 
    private System.Windows.Forms.Label IdLabel; 

访问一般来说,我给演讲的控制,因为它简单容易的工作,并期待更好的,但你可以随意使用任何你想。这里是工作示例的链接http://www.filedropper.com/windowsformsapp1_1

相关问题