-2

I try to write a program where you can select an item from a drop-down menu and it returns its serial number. The problem is that the items change and that is why I want to read the values from a file and fill the drop-down with the values.

I imagined that I would have a file that looks something like this:

Toaster;220:
Microwave;3021:

In this example, I would divide the product and the id with a semicolon and the number ends with a colon. The drop-down menu would only show the products (in this case Toaster and Microwave) and would return the values 220 or 3021 Is there any easy way to realize that in C#?

Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27

1 Answers1

1

Its really easy to do that but you don't provide much information a bout the stack of technologies that you use beside c#. Are you trying to do this in a web app such as (asp.net or asp.net core) desktop one (wpf, winforms) or uwp application. If so are you using any controls such as devexpress, infragistics, syncfusion, telerik...? There are many ways to do this if you provide some more information a bout your work environment I would be happy to help. I can give you quick example with wpf or either winforms application since you metioned that you are trying to write a program. you could go to Syncfusion.com and download their controls since they are free to use in noncommercial products and happen to have great documentation(installation is easy likewise especially if you use visual studio) then you go and create winform syncfusion project. Then look into the documentation for the events that you need in order to get on selection changed. Other workaround would be in pure winforms application here is how you can do this, first you go and create new application then you add a combo box with on selection changed event and data bound option. Then you create on load event for the form that would be used to add the items from the text file then you don't generally need but I prefer to create a structure for my new object if you managed to get here you can add a file reader to read you text then bind the information to the new list of the class that you just created. After that you bind the list of items to the combobox and create a label that would hold the displayed id. then its simple on selectionchanged event you take the selected item and cast it to the class that you created and bind the id of the class to the label and you have the functionality that you look for. you can look the code samples that I would provide

    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();
    }

this is how my controller for the winform1 :form look like if you don't want to bother adding new items to the view you can copy this to a new form with name form1 inside the initialize components which you can access with F12 key

 #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;

Generally I gave the lecture for the controls because its simply easy to work with and look better, but you can feel free to use whatever you want. Here is a link to working sample http://www.filedropper.com/windowsformsapp1_1