2017-09-25 16 views
-2

ドロップダウンメニューから項目を選択できるプログラムを作成しようとすると、そのシリアル番号が返されます。問題はアイテムが変更されていることです。そのため、ファイルから値を読み取り、ドロップダウンに値を入力したいのです。私は次のようになりますファイルだろうと想像ファイルからドロップダウンメニューに項目を追加

:この例では

Toaster;220: 
Microwave;3021: 

を、私はセミコロンで製品とidを分けるだろうと数は、コロンで終わります。ドロップダウンメニューは製品(この場合ToasterとMicrowave)のみを表示し、値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

これは質問は広すぎます。 –

答えて

1

これは本当に簡単ですが、C#の横にたくさんの情報を提供しているわけではありません。あなたは、(asp.netまたはasp.netコア)デスクトップの1つ(wpf、winforms)やuwpアプリケーションなどのWebアプリケーションでこれをしようとしていますか?もしそうなら、devexpress、infragistics、syncfusion、telerikなどのコントロールを使用していますか?私が手伝ってくれることを喜んであなたの仕事環境についていくつかの情報を提供すれば、これを行う多くの方法があります。私はあなたがプログラムを書くことを試みているので、wpfかどちらかのwinformsアプリケーションを使って簡単な例を与えることができます。あなたはSyncfusion.comに行き、コントロールをダウンロードすることができます。なぜなら、それらは非営利の製品で自由に使用でき、素晴らしいドキュメンテーションを持っているからです(ビジュアルスタジオを使用する場合は、特にインストールが簡単です)、winform syncfusionプロジェクトを作成します。次に、選択肢を変更するために必要なイベントのドキュメントを調べます。他の回避策は、純粋なwinformsアプリケーションになりますここではこれを行うことができます、まずは新しいアプリケーションを作成し、選択したイベントとデータバインドされたオプションでコンボボックスを追加します。次に、テキストファイルからアイテムを追加するために使用されるフォームのロードイベントを作成します。一般的には必要ありませんが、新しいオブジェクトの構造を作成したい場合は、ここでファイルを追加できます読者はテキストを読んで、作成したばかりのクラスの新しいリストに情報をバインドします。その後、コンボボックスにアイテムのリストをバインドし、表示されたIDを保持するラベルを作成します。選択したアイテムを選択してクラスにキャストし、そのクラスの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のための私のコントローラ方法です:フォームはあなたが新しいにこれをコピーすることができますビューに新しいアイテムを追加する気にしたくない場合のように見えますその単純に簡単で動作し、よりよく見ているので、あなたが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; 

にアクセスすることができ、初期化コンポーネントの内部名をForm1のフォームは、一般的に私は、コントロールのための講演を行いましたが、あなたは何でもを使用して自由に感じることができます欲しいです。ここに作業サンプルへのリンクがあります。http://www.filedropper.com/windowsformsapp1_1