How to show a panel dynamically on mouse enter event? - winforms

Like in web page using css we can show a div on mouse enter or hover, in the same way i want to show a panel on mouse enter event of a button, but i am unable to do this. I am trying like this.
private void btn2_MouseEnter(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = System.Drawing.Color.MistyRose; //this is executed on mouse enter
Point locationOnForm = btn.FindForm().PointToClient(
btn.Parent.PointToScreen(btn.Location));
Panel pnl = new Panel();
Label lbl = new Label();
lbl.Text = "anything";
pnl.Controls.Add(lbl);
pnl.Location = new Point(locationOnForm.X, locationOnForm.Y);
pnl.Size = new Size(500, 500);
pnl.BackColor = Color.SkyBlue;
pnl.Visible = true;
pnl.Show();
}
I am not getting how to solve this. I want to know that
1) Is this the right approach or there is any other way of doing this?
2) If this is ok then what is the mistake i am doing here ?
Thanks.

Don't create the panel on mouse enter, rather have the panel created already then just show and hide it.
private void button1_MouseEnter(object sender, EventArgs e)
{
panel1.Show();
}

You have to add the panel to the Form controls
Form1.Controls.Add(pnl);
If you plan to have a panel hover over the button like a <div> in Web, you will have to call
BringToFront() to ensure that the panel does not appear behind the button or other controls on the form -
pnl.BringToFront();
Also like the previous answer, it may be better to have a panel placed on the form already and just set visible to true or false as well as the panel location otherwise you may end up adding multiple panels to the Form controls.
If you plan on just showing plain text in the panel, it may be easier to use Tooltip control -
MSDN - Tooltip Control

Related

WPF - Clear all textboxes in All TabItem of TabControl

In WPF, I use tabcontrol have tabitems to create new Dynamic content. I have button Reset to clear all textboxes in All TabItems. My code is working with tab focus. But, When I click the other tab and click again old tab, The content show again. Please help me so that I can clear all textboxes in all tabItems
My Code here:
private void ClearControls(DependencyObject root)
{
System.Windows.Controls.TabControl controls = new System.Windows.Controls.TabControl();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
{
var control = VisualTreeHelper.GetChild(root, i);
if (control is System.Windows.Controls.TextBox)
{
(control as System.Windows.Controls.TextBox).Text = String.Empty;
}
else if (VisualTreeHelper.GetChildrenCount(control) > 0)
{
ClearControls(control);
}
}
}
private void BtnResetTurnbyTurn_Click(object sender, RoutedEventArgs e)
{
ClearControls(this);
}
Don't waste your time with tree visualizer
Design your ui in a separately usercontrol and give a name then from
Then head to your main window and give the area that is supposed to display the ui a name or just declare a grid and give it a name
Now head to your tabs and double click the one that is suppoused to show the ui you want and from the code just write the following
Just to clarify the names
I have my separately designd ui in a file called pages and it's name is myui and my usercontrol tag is name child
My viewing area is filled with a grid named
Mainview
I have double clicked the required tab and it drove me to the cs file inside the click event
My code is
` pages.myui mu =new pages.myui();
Mainview.Children.Clear();
Mainview.Cildren.Add(mu.child);`
It might be the most basic way to accomplish the mission but believe me it is a life saver
Comment for any question
I hope i have helped you
Yusuf naeem

how to select option in context menu c #

hello I'm grabbing a windows form application in c # and have a question about a context menu I have my main form within the main form I have a picturebox and I created an event as the next mouse click
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("DISPONIBLE");
cm.MenuItems.Add("RESERVAR");
cm.MenuItems.Add("OCUPADA");
pictureBox1.ContextMenu = cm;
}
}
all going well so far shows me my contextual menu when I right click but when I choose the option to "AVAILABLE" another windows form show me someone who can help me please thanks
I think this is not the way. if you did so you need to call show for the context menu, you can set that menu at the initialization of the form or the control, you create the context menu one time it will be shown automatically, you don't have to create it each time you click the control, Add the following code to the constructor of the form just to check if it works
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("DISPONIBLE");
cm.MenuItems.Add("RESERVAR");
cm.MenuItems.Add("OCUPADA");
pictureBox1.ContextMenu = cm;

Dynamically create ContextMenu in Silverlight 5

I have just started to use Silverlight this few days.
Currently I have generated some panel dynamically in a canvas. Each panel will have a shared MouseLeftButtonUp event which will display a ContextMenu.
The problem now is when I click on a first panel, the ContextMenu is able to show but when I click on a second panel, the first ContextMenu shown up instead of the second. The second panel will show its ContextMenu only when I click another time on the second panel.
Below is my code to generate the panel and the context menu:
void generatePanel()
{
StackPanel panel = new StackPanel();
panel.MouseLeftButtonUp += panel_MouseLeftButtonUp;
canvas.Children.Add(panel)
}
void panel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
ContextMenu menu = new ContextMenu();
menu.IsOpen = true;
}
Hopefully my problem is clear enough. Is there any way to solve the problem? Or if there are any event that are fired when the menu is hidden, so that I can make sure the previous context menu is disposed or cleared?
Thanks.

highlight a win forms checkbox when cursor is inside it

I have a checkbox with no text specified. Now whenever I tab down to this checkbox, it doesnot get highlighted.I even tried setting focus in checkbox_Enter() event. I checked for focus in this event and focus is there in this checkbox. How to get it highlighted so that user can know that the cursor is there in checkbox.
Try putting just a space into the Checkbox
Or
setting AutoSize to false
Setting the size of the Checkbox
Then there may be somewhere for WinForms to draw the focus ret.
Otherwise you have to to custom draw the Checkbox, or draw the focus rec round the Checkbox yourself.
Whatever you do it will not look that good, as users expect the focus rec to be round the label of the checkbox, and you wish to have a checkbox with no label.
I managed to do it by the below mentioned way
Use a panel.Push the checkbox inside that panel.Set the dimensions of a panel as such that it looks like a rectangle around the checkbox.In checkbox_enter() event set the border
BorderStyle.FixedSingle;
And in checkbox_Leave() event set the border again to
BorderStyle.None;
So this way it will tell user that focus in inside the checkbox.
To make the checkbox appear highlighted I had it change color on got focus and change back on lost focus.
this part is in the Form1.Designer.cs:
private void InitializeComponent()
{...
ckBox1.GotFocus += new System.EventHandler(checkBox_Highlight);
ckBox1.LostFocus += new System.EventHandler(checkBox_EndHighlight);
ckBox1.MouseHover += new System.EventHandler(checkBox_Highlight);
ckBox1.MouseLeave += new System.EventHandler(checkBox_EndHighlight);
}
This part is in the Form1:
private void checkBox_Highlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
control.FlatStyle = FlatStyle.Flat;
control.ForeColor = Color.Blue;
}
private void checkBox_EndHighlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
if (!control.Focused)
{
control.ForeColor = DefaultForeColor;
}
}
While tabbing, this puts a light blue shadow beneath the CheckBox on Enter and on Leave - at least on my Windows7:
...
checkBox1.Enter += new System.EventHandler(check_Enter);
checkBox1.Leave += new System.EventHandler(check_Leave);
...
private void check_Enter(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = SystemColors.Highlight;
}
private void check_Leave(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = Color.Transparent;
}
Actually the highlights comes on the text of the checkbox not the checkbox itself
So, if I was you, I would put any control in the background of my check box and give it the focus when my checkbox is focused, to have the same look of default controls highlights.
which will be shown to end users as checkbox highlights.

ToolStrip Buttons flashes

I have a windows form with a toolstip in it with several buttons.
When the mouse is over a button of the toolstip then the toolstrip button starts to flash... looks like it gets and loses focus every second.
That results for the click to do nothing if the user click at the time that the button has no focus therefore the user has to click the button again and again util he gets the timing correct.
Does anyone knows anything about this?
I rally need some answers as soon as possible...
Thank you very much
I have found the reason...
The toolstrips in windows forms have by default the tooltips set to Auto and if the tooltip opens on the taskbar then the toolstrip loses focus.
The solution to this is to either disable the tooltips or to set it to manual and show the tooltip at another place.
Here is the code for showing the tool tip above the item manually:
private readonly ToolTip currentToolTip = new ToolTip();
private void ToolStripItem_MouseEnter(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Show(item.ToolTipText, item.Owner, item.Bounds.X, -20);
}
private void ToolStripItem_MouseLeave(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Hide(item.Owner);
}
You have to add the event handlers to all your ToolStripItems and set the ToolStrips' ShowItemToolTips to false.

Resources