Save item in Combobox [closed]

1

I have a combobox with some items added via form, by the Items (Collection) property.

I would like to be able to leave to the user, typing new texts in this combobox, which in my case, is a combobox of product categories.

I created a code that adds the typed text to the combobox, but it does not stay there when the form is restarted, my question is, can you add an item and make it stay in the items?

Note: There is no way I can use a database for this.

My code:

private void btnIncluirItens_Click(object sender, EventArgs e)
{
     // para inserir na última posição

    int cnt = cbCategoriaProduto.Items.Count;
    if (cbCategoriaProduto.Text != String.Empty)
    {
        cbCategoriaProduto.Items.Insert(cnt, cbCategoriaProduto.Text);

    }
    else
    {
        cbCategoriaProduto.Items.Insert(cnt, "Item " + cnt);
    }
    cbCategoriaProduto.Text = ""; //limpa a caixa de texto após a inclusão

}
    
asked by anonymous 20.04.2015 / 00:28

1 answer

0

Check if this code is enough for what you want.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace drawing {
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        ComboBox comboBox1 = new ComboBox();
        private void Form2_Load(object sender, EventArgs e)
        {

            comboBox1.Location = new Point(20, 60);
            comboBox1.Name = "comboBox1";
            comboBox1.Size = new Size(245, 25);

            comboBox1.Items.Add("A");
            comboBox1.Items.Add("B");
            comboBox1.Items.Add("C");
            comboBox1.Items.Add("D");
            comboBox1.Items.Add("Add");

            this.Controls.Add(comboBox1);
            comboBox1.SelectedValueChanged += OnComboBox1SelectionChanged;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void OnComboBox1SelectionChanged(object sender, EventArgs e)
        {
            ComboBox temp = (ComboBox)sender;
            if (temp.SelectedItem.ToString() == "Add")
            {
                temp.DropDownStyle = ComboBoxStyle.DropDown;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Add(comboBox1.Text);
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }
    }
}
    
20.04.2015 / 11:26