On button click-context menu strip
-
I have 30 buttons and I am trying to implement context menu on each button click by using following method.
void OnButtonClick(object sender, EventArgs e) { Button btn = (Button)sender; btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height)); }
I have 2 context menu strip items, Add text Label and Add colour
private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
if (string.IsNullOrEmpty(btn.Text.Trim()))
{
frmAddText form = new frmAddText();
form.ShowDialog();if (frmAddText.IsTextMod) { return; } btn.Text = form.TextInfo; btn.Tag = 0; } } }
Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.
private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
DialogResult dResult = colorDialog1.ShowDialog();
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
string strBtnName = btn.Name;
if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
{
if (dResult == DialogResult.OK)
btn.BackColor = colorDialog1.Color;
} } }It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.
-
I have 30 buttons and I am trying to implement context menu on each button click by using following method.
void OnButtonClick(object sender, EventArgs e) { Button btn = (Button)sender; btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height)); }
I have 2 context menu strip items, Add text Label and Add colour
private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
if (string.IsNullOrEmpty(btn.Text.Trim()))
{
frmAddText form = new frmAddText();
form.ShowDialog();if (frmAddText.IsTextMod) { return; } btn.Text = form.TextInfo; btn.Tag = 0; } } }
Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.
private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
DialogResult dResult = colorDialog1.ShowDialog();
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
string strBtnName = btn.Name;
if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
{
if (dResult == DialogResult.OK)
btn.BackColor = colorDialog1.Color;
} } }It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.
Udayaraju wrote:
string strBtnName = btn.Name; if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase)) {
this condition will always be True and all button will be colored. Edit - you need to refactor your code. You can get the button on whom the contextmenu click happpened rather than looping through all controls.
Button btn = (sender as ContextMenuStrip).SourceControl as Button;
-
I have 30 buttons and I am trying to implement context menu on each button click by using following method.
void OnButtonClick(object sender, EventArgs e) { Button btn = (Button)sender; btn.ContextMenuStrip.Show(btn, new System.Drawing.Point(0, btn.Height)); }
I have 2 context menu strip items, Add text Label and Add colour
private void addTextLabelToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
if (string.IsNullOrEmpty(btn.Text.Trim()))
{
frmAddText form = new frmAddText();
form.ShowDialog();if (frmAddText.IsTextMod) { return; } btn.Text = form.TextInfo; btn.Tag = 0; } } }
Wen i try to do this it is implementing on last button itself. Here Ia m returning Here foreach is not acceptable.
private void addColourToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
DialogResult dResult = colorDialog1.ShowDialog();
foreach (Control item in panel2.Controls)
{
Button btn = (Button)item;
string strBtnName = btn.Name;
if (string.Equals(btn.Name, strBtnName, StringComparison.OrdinalIgnoreCase))
{
if (dResult == DialogResult.OK)
btn.BackColor = colorDialog1.Color;
} } }It is applying color to all buttos. But it should apply for only specific clicked button. Here foreach is not acceptable.
There may be a quicker way of doing it, but the code below works.
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
if(sender is ToolStripMenuItem)
{
// Get the item that was cliked
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
// Get the ContextMenuStrip the item belongs to
ContextMenuStrip strip = (ContextMenuStrip)clickedItem.Owner;
if (strip.SourceControl is Button)
{
// Get the actual button it belongs to
Button button = (Button)strip.SourceControl;
button.BackColor = Color.Blue;
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
There may be a quicker way of doing it, but the code below works.
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
if(sender is ToolStripMenuItem)
{
// Get the item that was cliked
ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
// Get the ContextMenuStrip the item belongs to
ContextMenuStrip strip = (ContextMenuStrip)clickedItem.Owner;
if (strip.SourceControl is Button)
{
// Get the actual button it belongs to
Button button = (Button)strip.SourceControl;
button.BackColor = Color.Blue;
}
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
You're welcome :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)