user3237403
user3237403

Reputation: 163

Check state of combobox and change textbox readonly

I have a form that displays a datagridview and a textbox and combobox. When a column name is selected in the combobox, and a search typed in the textbox it filters and displays the searched data. How can I detect if the combobox has nothing selected, and change the textbox readonly state to true, and when something is selected, change it back to false so I can search?

DataTable dt;
        private void searchForm_Load(object sender, EventArgs e)
        {
            SqlCeConnection con = new SqlCeConnection(@"Data Source=|DataDirectory|\LWADataBase.sdf;");
            SqlCeDataAdapter sda = new SqlCeDataAdapter("select * from customersTBL", con);
            dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            comboSearch.Items.Add("[First Name]");
                comboSearch.Items.Add("Surename");
            comboSearch.Items.Add("[Address Line 1]");
            comboSearch.Items.Add("[Address Line 2]");
            comboSearch.Items.Add("County");
            comboSearch.Items.Add("[Post Code]");
            comboSearch.Items.Add("[Contact Number]");
            comboSearch.Items.Add("[Email Address]");



        }



        private void searchTxt_TextChanged(object sender, EventArgs e)
        {


            {
                DataView dv = new DataView(dt);
                dv.RowFilter = "" + comboSearch.Text.Trim() + "like '%" + searchTxt.Text.Trim() + "%'";
                dataGridView1.DataSource = dv;
            }



        }

Upvotes: 0

Views: 876

Answers (4)

NetInfo
NetInfo

Reputation: 597

Add an empty item to the combobox and then

if(comboSearch.SelectedItem =="")
{
searchTxt.ReadOnly = true;
}
else
{
searchTxt.ReadOnly = false;
}

After executing your code add this line:

comboSearch.SelectedIndex =0;

Make sure your empty item is the first item.

Upvotes: 0

user3237403
user3237403

Reputation: 163

I added a SelectedIndexChanged event:

private void comboSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            searchTxt.ReadOnly = false;
        }

And changed the TextChanged event to:

private void searchTxt_TextChanged(object sender, EventArgs e)
        {
            if (comboSearch.SelectedItem == null)
            {
                searchTxt.ReadOnly = true;
                MessageBox.Show("Please select a search criteria");
            }



            else
            {
                searchTxt.ReadOnly = false;
                DataView dv = new DataView(dt);
                dv.RowFilter = "" + comboSearch.Text.Trim() + "like '%" + searchTxt.Text.Trim() + "%'";
                dataGridView1.DataSource = dv;
            }
        }

and it now functions how I'd like it to

Upvotes: 0

puddinman13
puddinman13

Reputation: 1408

Add a row in your FormLoad to the comboSearch control for a blank line. Make sure it's about the [First Name] item.

comboSearch.Items.Add(string.Empty);
comboSearch.Items.Add("[First Name]");

Add an event handler to comboSearch for SelectedIndexChanged. Inside the event, set the searchTxt control's ReadOnly property. It may look like this:

private void comboSearch_SelectedIndexChanged(object sender, EventArgs e)
{
    searchTxt.ReadOnly = comboSearch.SelectedIndex != 0;
}

Upvotes: 0

user3222297
user3222297

Reputation: 206

You can try this

if( ComboBox.SelectedItem == null ) {
   // do something
TextBox.ReadOnly = true;       //Using the TextBox.ReadOnly property

}
else{
Textbox.ReadOnly=false;
}

hope it helps you!

Upvotes: 0

Related Questions