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;
Related
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
I've read a few articles on SO:
How to detrmine the control that cause ContextMenuStrip
Getting the control of a context menu
and a couple others that suggested use of the SourceControl property.. but none work in this context:
I have a ContextMenuStrip that has a child ToolStripMenuItem - this code from the windows forms designer generated section:
// _tileContextMenuStrip
//
this._tileContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tileKindToolStripMenuItem,
this.forceWidthScalingToolStripMenuItem,
this.forceHeightScalingToolStripMenuItem});
this._tileContextMenuStrip.Name = "_tileContextMenuStrip";
this._tileContextMenuStrip.Size = new System.Drawing.Size(184, 70);
//
// tileKindToolStripMenuItem
//
this.tileKindToolStripMenuItem.Name = "tileKindToolStripMenuItem";
this.tileKindToolStripMenuItem.Size = new System.Drawing.Size(183, 22);
this.tileKindToolStripMenuItem.Text = "Tile Kind";
So the context menu strip and the menu item first in the list are fixed at design time. At runtime, the TSMI has child TSMIs added to it in a loop based on an enum:
foreach(TileKind t in typeof(TileKind).GetEnumValues()) {
ToolStripMenuItem tsmi = new ToolStripMenuItem(t.ToString("g"));
tsmi.Tag = t;
tsmi.Click += tsmi_Click;
tileKindToolStripMenuItem.DropDownItems.Add(tsmi);
}
Later I have 20 checkboxes on my form and I set their .ContextMenuStrip to be the same thing:
foreach(Thing t in someDataSource){
CheckBox c = new CheckBox();
c.Text = t.SomeData;
c.ContextMenuStrip = this._tileContextMenuStrip;
myPanelBlah.Controls.Add(c);
}
Great, so now I have all my checkboxes and they all show the context menu when I right click them, but when I choose one the sub-menu items, I just can't find out the control that fired the context menu...
//this the click handler for all the menu items dynamically added
void tsmi_Click(object sender, EventArgs e)
{
ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
(tsmi.OwnerItem //the parent node in the menu tree hierarchy
.Owner as ContextMenuStrip) //it's a ContextMenuStrip so the cast succeeds
.SourceControl //it's always null :(
}
I can reliably get ahold of the contextmenustrip either by routing up from the event handler sender, or even just by referencing the ContextMenuStrip itself as a form instance variable, but SourceControl is always null
Any ideas what to try next?
I see the problem, quacks loudly like a bug. There's a workaround, you can subscribe the ContextMenuStrip's Opening event. At that point, well before you start navigating into the sub-items, the SourceControl property is still valid. So store it in a field of the class so you'll have it available in the Click event handler. Roughly:
private Control _tileCmsSource;
private void _tileContextMenuStrip_Opening(object sender, CancelEventArgs e) {
_tileCmsSource = _tileContextMenuStrip.SourceControl;
}
void tsmi_Click(object sender, EventArgs e)
{
ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
// Use _tileCmsSource here
//...
}
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.
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
I have been looking up on google to find out how to use linklabel to link to a Windows Form. All the tutorials that I've come across only link you to a URL or you local disk. Any help? I don't want to use buttons.
Use the LinkLabel as you would be using a button, by executing code in the click event. Open the form (or bring it to the front if it is alreay open) programmatically when the user clicks the label. You will not be able to set an URL to a form.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form2 form2 = Application.OpenForms.OfType<Form2>().FirstOrDefault();
if (form2 == null) {
form2 = new Form2();
form2.Show();
} else {
form2.BringToFront();
}
}