代码 c# 中的 MessageBox

MessageBox in code c#

如何添加消息框 "Item Not Found" 这个代码? 谢谢!

if (comboBox1.Text == "Search")
        {
            foreach (DataGridViewRow dgvr in dataGridView_produkty.Rows)
            {
                if (dgvr.Cells[0].Value != null)
                {
                    if (dgvr.Cells[0].Value.ToString().Contains(textBox_szukaj.Text))
                    {
                        dgvr.Visible = true;
                        continue;                                             
                    }
                    dgvr.Visible = false;
                }
            }
        }
if (comboBox1.Text == "Search")
{
    var itemFound = false;

    foreach (DataGridViewRow dgvr in dataGridView_produkty.Rows)
    {
        if (dgvr.Cells[0].Value != null)
        {
            if (dgvr.Cells[0].Value.ToString().Contains(textBox_szukaj.Text))
            {
                dgvr.Visible = true;
                itemFound = true;
                continue;                                             
            }
            dgvr.Visible = false;
        }
    }

    if (!itemFound)
    {
        MessageBox.Show("Item not found");
    }
}

您可以使用MessageBox class and use it's Show函数:

MessageBox.Show("Item not found");

用一面旗帜,一切顺利。

if (comboBox1.Text == "Search")
{
    bool itemFound = false;
    foreach (DataGridViewRow dgvr in dataGridView_produkty.Rows)
    {
        if (dgvr.Cells[0].Value != null)
        {
            if (dgvr.Cells[0].Value.ToString().Contains(textBox_szukaj.Text))
            {
                dgvr.Visible = true;
                itemFound = true;
                continue;                                             
            }
            dgvr.Visible = false;
        }
    }
    if (!itemFound)
    {
        MessageBox.Show("Item not found");
    }
}

如果 textBox_szukaj.Text 不包含在任何行中,下面的代码将显示消息框。

if (comboBox1.Text == "Search")
{
    bool found = false;
    foreach (DataGridViewRow dgvr in dataGridView_produkty.Rows)
    {
        if (dgvr.Cells[0].Value != null)
        {
            if (dgvr.Cells[0].Value.ToString().Contains(textBox_szukaj.Text))
            {
                dgvr.Visible = true;
                found = true;
                continue;                                             
            }
            dgvr.Visible = false;
        }
    }
    if(!found) MessageBox.Show("Item not found");
}