0

In my flow layout panel it load pic and name in user control.

I try this, which is working fine

foreach (DataRow row in dt.Rows)
{
    byte[] data = (byte[])row["Image"];
    pic = new PictureBox();
    pic.Width = 150;
    pic.Height = 150;
    pic.BackgroundImageLayout = ImageLayout.Stretch;
    pic.BorderStyle = BorderStyle.FixedSingle;
    string type = row.Table.Columns.Contains("liquidPriceId") ? "liquidPrice" : "itemMaster";
    string tag = row.Table.Columns.Contains("liquidPriceId") ? row["liquidPriceId"].ToString() : row["itemMasterId"].ToString();
    MemoryStream ms = new MemoryStream(data);
    pic.BackgroundImage = new Bitmap(ms);

    Label name = new Label();
    name.Text = row["Name"].ToString();
    name.BackColor = Color.FromArgb(45, 66, 91);
    pic.Controls.Add(name);
    flp.Controls.Add(pic);
}

THEN in my search text change I try this, my problem is I don't know how to get the name for filtering

foreach (Control c in flowLayoutPanel3.Controls)

how to get inside c of my pic and name values ?

private void txtSearchBox_TextChanged(object sender, EventArgs e)
{
    string searchValue = txtSearchBox.Text;

    try
    {
        if (txtSearchBox.Text.Length > 0)
        {
            string compareTo = String.Concat("*", txtSearchBox.Text.ToLower(), "*");

            foreach (Control c in flowLayoutPanel3.Controls)
            {
                c.Visible =(c.Name.ToLower() == compareTo); // c.Name is empty how can i get name ?
            }
        }
        else
        {
            foreach (Control c in flowLayoutPanel3.Controls)
            {
                c.Visible = true;
            }
        }
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • You are adding a `Label` to your `PictureBox` - this doesn't make it a property `Name`. You need to find the Label control of PictureBox control. – Filburt Apr 01 '21 at 16:44

2 Answers2

0

It's not clear from the code and text provided, but it seems that what you might need is to set the Name property of the control(s) you create in the first code snippet. Something like this:

//...
pic = new PictureBox();
pic.Name = "My Picture";
// ...

I would also change the comparison from this:

c.Visible =(c.Name.ToLower() == compareTo);

to this:

c.Visible = c.Name.StartsWith(compareTo, StringComparison.CurrentCultureIgnoreCase);
Jiri Volejnik
  • 1,034
  • 6
  • 9
0

When you create the PictureBox control, I would make the Name property the same as the Text property of the Label:

name.Text = row["Name"].ToString();
pic.Name = name.Text;

But, you are adding * to the beginning and end of your search string. So do you want a match if the search value is contained anywhere within the Name property?

If yes, then you could just use String.Contains():

string compareTo = txtSearchBox.Text.Trim().ToLower();
foreach (Control c in flowLayoutPanel3.Controls)
{
    c.Visible = c.Name.ToLower().Contains(compareTo);
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40