Reputation: 37
I was wondering how can I get this done since I'll use it in notifying the user whether a certain tab has values in it.
So what I wanted to happen is that the image will appear when it has values, and when it is empty, the image will be hidden.
Note that the image will reappear when a certain tab has values in it.
Upvotes: 0
Views: 777
Reputation: 37
I've done this by having the image appear when a certain table has values in it, unless it is empty, then the image in the menutoolstrip won't come out
Here's my code:
private void Home_Load(object sender, EventArgs e)
{
if (GetPurchase())
{
forApprovalToolStripMenuItem.Image = Properties.Resources.sign_check_icon;
}
else
{
forApprovalToolStripMenuItem.Checked = false;
}
itemRequestsToolStripMenuItem.Enabled = true;
forApprovalToolStripMenuItem.Enabled = false;
}
bool GetPurchase()
{
bool withPending;
using (SqlConnection con = new SqlConnection(Helper.GetConnection()))
{
con.Open();
string query = @"SELECT POID FROM purchaseorder_table WHERE status='Pending'";
using (SqlCommand cmd = new SqlCommand(query, con))
{
return withPending = cmd.ExecuteScalar() == null ? false : true;
}
}
}
Upvotes: 0
Reputation: 29
i would recomend you to use the DisplayStyle property of a ToolStripMenuItem.
You can test this by just adding the mouseenter and mouseleave event to your control like in this example. This way you do not delete the image (by setting it to null) like proposed in one of the comments above.
private void testToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
(sender as ToolStripMenuItem).DisplayStyle = ToolStripItemDisplayStyle.Text;
}
private void testToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
(sender as ToolStripMenuItem).DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
}
I hope this helps you further.
Cheers
Upvotes: 1