Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using like operator in if operator in C#

Tags:

c#-4.0

I have a textbox for search (that is, textBox1) A user, for example, enters "aba" in textBox1. "abandon" puts in datagridiew1. The user clicks on datagriview1:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    richTextBox_MWE.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    if ("richTextBox_MWE.Text like '%" + textBox1.Text + "%'")
    {
        label5.BackColor = Color.Green;
    }
}

I want if "abandon" is such as "aba" in textBox1, label5.BackColor becomes Green.

like image 243
user2111639 Avatar asked Oct 25 '25 18:10

user2111639


2 Answers

Simple way is use the textBox1(where actually filter content going to change) change event

if(!String.IsNullOrEmpty(richTextBox_MWE.Text) && richTextBox_MWE.Text.Trim().Contains(textBox1.Text.Trim()))
{
  label5.BackColor = Color.Green;
}
like image 175
Vasistan Avatar answered Oct 27 '25 14:10

Vasistan


You want to use some kind of mix of C# and sql :) You can use the String.Contains method to achieve what you want.

if(richTextBox_MWE.Text != null
    && richTextBox_MWE.Text.Contains(textBox1.Text.Trim())
{
    ...
}
like image 22
Hamlet Hakobyan Avatar answered Oct 27 '25 16:10

Hamlet Hakobyan