2017-09-25 72 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

相關問題