C# Forms Mouse Hover Not firing in controls? - c#

I am trying to write a code on Mouse_Hover for Button
private void Button_MouseHover(object sender, EventArgs e){
Button b = (Button)sender;
toolTip1.Show("Click ME!!", b);
}
But toolTip1 is not showing!!
after this I try to use MouseHover In another controls and it did not work and I try To use
MessageBox , but it did not work even , and I double check Events for Button (and other controls)
in properties tab.

You don't have to wire up controls to use the mouse hover event in order to use a tooltip. MSDN has some useful guidance, the essence of which is "add a tooltip to your form, assign the tooltip to the button"

This is a way to solve your problem:
public partial class Form1 : Form
{
private System.Windows.Forms.ToolTip toolTip1;
public Form1()
{
InitializeComponent();
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
Button myBtn = new Button();
this.Controls.Add(myBtn);
myBtn.Location = new Point(10, 10);
myBtn.MouseEnter += new EventHandler(myBtn_MouseEnter);
myBtn.MouseLeave += new EventHandler(myBtn_MouseLeave);
}
void myBtn_MouseEnter(object sender, EventArgs e)
{
Button btn = (sender as Button);
if (btn != null)
{
this.toolTip1.Show("Hello!!!", btn);
}
}
void myBtn_MouseLeave(object sender, EventArgs e)
{
Button btn = (sender as Button);
if (btn != null)
{
this.toolTip1.Hide(btn);
}
}

Related

DragDrop button text in C# (WinForm)

My goal is to have multiple buttons whose text can be switched on the screen through dragging the text from one button to another (it's not necessary that the buttons themselves be switched, only the text on them, although if it's easier to switch the buttons themselves, that's fine). I have tried to follow this MSDN article but as soon as I actually begin to drag, I get a "no" symbol (an O with a / through it). Am I missing something? (Code below)
Thanks in advance!
public partial class Form1 : Form
{
Button button1 = new Button();
Button button2 = new Button();
public Form1()
{
InitializeComponent();
button1.Text = "Button 1";
button2.Text = "Button 2";
button2.Location = new Point(100, 0);
this.Controls.Add(button1);
this.Controls.Add(button2);
button1.MouseDown += new MouseEventHandler(button_MouseDown);
button2.MouseDown += new MouseEventHandler(button_MouseDown);
button1.DragEnter += new DragEventHandler(button_DragEnter);
button2.DragEnter += new DragEventHandler(button_DragEnter);
button1.DragDrop += new DragEventHandler(button_DragDrop);
}
private void button_MouseDown(object sender, MouseEventArgs e)
{
((Button)sender).DoDragDrop(((Button)sender).Text, DragDropEffects.Move);
}
private void button_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void button_DragDrop(object sender, DragEventArgs e)
{
//I'm not sure what goes here, but I can figure that out through experimentation
}
}
So it turns out I just had to do some more research. Adding button.AllowDrop = true for both buttons solved the issue

How to create the event handler for editing dynamic labels in C#?

I have created a dynamic label on button click on Windows Form. And then on a right click on the label. I'm showing a context menu "cm". I obviously want to add functionality to the context menu items. But what I don't understand is how do I reference the "lbl" object inside the event handler? How can I edit the properties of the labels from inside the event handlers named MarkedImportant and EditLabel?
public void btnMonSub_Click(object sender, EventArgs e)
{
string s = txtMonSub.Text;
Label lbl = new Label();
lbl.Text = s;
lbl.Location = new System.Drawing.Point(205 + (100 * CMonSub), 111);
CMonSub++;
lbl.Size = new System.Drawing.Size(100, 25);
lbl.BackColor = System.Drawing.Color.AliceBlue;
this.Controls.Add(lbl);
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Mark Important", MarkImportant);
cm.MenuItems.Add("Edit", EditLabel );
lbl.ContextMenu = cm;
}
private void MarkImportant(object sender, EventArgs e)
{
// imp..
}
private void EditLabel(object sender, EventArgs e)
{
// edit..
}
Or is there a better way to do this? Like dynamically adding the event handler itself?
Thanks in advance.
The ContextMenu has a property called SourceControl and MSDN says it
Gets the control that is displaying the shortcut menu.
So your event handler could reach the ContextMenu from the MenuItem passed as the sender parameter in this way
private void MarkImportant(object sender, EventArgs e)
{
// Convert the sender object to a MenuItem
MenuItem mi = sender as MenuItem;
if(mi != null)
{
// Get the parent of the MenuItem (the ContextMenu)
// and read the SourceControl as a label
Label lbl = (mi.Parent as ContextMenu).SourceControl as Label;
if(lbl != null)
{
....
}
}
}

Enable/Disable SubmenuItems on Mdi Parent form in C# using Windows Application?

on Mdi parent form i am calling my child forms using menu items. on Child form Load my Menu item should be disabled on child form closed it will again Enabled.., i try FormClosing event handler i get the answer..,
private void btnMn1_Click(object sender, EventArgs e)
{
Forms.Cnblfrm cnbfrm = new Cnsmblfrm();
cnsmbfrm.MdiParent = this;
cnsmbfrm.Text = btnMn1.Text;
cnsmbfrm.Show();
this.btnMn1.Enabled = false;
cnbfrm.FormClosed += new FormClosedEventHandler(cnsmbfrm_FormClosed);
}
void cnbfrm_FormClosed(object sender, FormClosedEventArgs e)
{
btnMn1.Enabled = true;
//throw new NotImplementedException();
}
by using the above code i am getting the answer but i have morethan 20 ChildForms. By using this method My coding is increasing...., there is any method instead of this....,
If I understand you right: you have 20 buttons where every button opens a specific form, right?
If so, you can set the tag-property of each button to the form it opens. then you have to iterate through all buttons and set the click-event. All buttons have the same click-event. (let's call it btn_click)
the code of btn_click can look like:
private void btn_click(object sender, EventArgs e)
{
Button button = sender as Button;
if(button == null)
return;
Form form = button.Tag as Form;
if(form == null)
return;
form.MdiParent = this;
form.Text = button.Text;
form.Show();
button.Enabled = false;
form.Tag = button;
form.FormClosed += FormClosed;
}
private void FormClosed(object sender, FormClosedEventArgs e)
{
Form form = sender as Form;
if(form == null)
return;
Button button = form.Tag as Button;
if(button == null)
return;
button.Enabled = true;
}

Button text bold on click

I made a quiz game and I want the text on the button to be bold when clicked. This code works:
button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Bold);
The problem I'm having is when I click on the 'Next' button to go to the next question, the text is still bold even though the answer hasn't been clicked. How do I solve this?
Just do this on "Next" button click
button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Regular);
You need to un-bold everything when you click Next. The code below should help (it also includes a possibly cleaner bolding implementation).
// usage
foreach(var button in GetAnswerButtons())
{
button.Click += OnClickToBold;
button.Click += OnClickSetPropertyBasedOnCorrectness;
}
nextButton.Click += NextClick;
// implementations
private void OnClickToBold(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
}
private void OnClickSetPropertyBasedOnCorrectness(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
button.WhateverProperty = IsCorrectAnswer(button)
? valueWhenCorrect
: valueWhenWrong;
}
private void NextClick(object sender, EventArgs e)
{
foreach(var button in GetAnswerButtons())
{
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Regular);
UnsetPropertyBasedOnCorrectness(button);
}
}
private IEnumerable<Button> GetAnswerButtons() { ... }
private void UnsetPropertyBasedOnCorrectness(Button b) { ... }

Dynamicly added Controls [e.g Button] : How to add events and Access

At my program i dynamicly add Buttons to my form
{
...
Button bt = new Button();
bt.Text = "bla bla";
bt.MouseClick += new MouseEventHandler(bt_MouseClick);
myPanel.Controls.Add(bt);
...
}
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ??? // I want to get the Button's text ! this.Text returns me the
//main form.Text
}
How can access my dynamic Buttons properties ? How can I understand whick button is
clicked either getting its text.
Thanks.
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ((Button)sender).Text;
}
When an EventHandler delegate is invoked, the sender parameter is the component that raised the event, and the e parameter is a subclass of EventArgs that provides any additional component/event specific information for the event.
Therefore you can establish which button the event fired on by casting the sender parameter to Button:
void bt_MouseClick(object sender, MouseEventArgs e)
{
var button = (Button)sender;
TabPage _tab = new TabPage();
_tab.Text = button.Text;
}

Categories