|
In addition to the article already linked - here's[^] a quick Tip that shows how to do it concisely
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hello,
I am looking for ways to reposition the cursor when I am clicked on the first menu item from a form menu. It should create a jumping effect so the user doesn't have to scroll through the other menus which don't have a child menu.
for example.... one file menu has 4 items. Namely 1, 2, 3, 4 now only item 2 has child menu items. namely 2.1, 2.2, 2.3. I want the cursor to be on 2.1 when the user brings the mouse on 2.
Please let me know how to do that. I am trying the following:
private void toolStripMenuItem2_MouseHover(object sender, EventArgs e)
{
toolStripMenuItem2.ShowDropDown();
Point LocalMousePosition_1 = this.PointToClient(Cursor.Position);
System.Windows.Forms.Cursor.Position = new Point(500, 500);
MessageBox.Show(LocalMousePosition_1+"Happy New Year to DOTNETSPIDER.COM guys");
}
|
|
|
|
|
That sounds user-hostile -- I wouldn't use your application if it did that.
|
|
|
|
|
It doesn't matter if it's hostile or not, I need it now for some reason... let's just say all the supervisors in the MSc. program are crazy...... if you know a solution please help me ...otherwise please mind your own business....
|
|
|
|
|
|
well it doesn't let me add that... i have tried that... it says :
Error 1 'System.Windows.Forms.ToolStripMenuItem' does not contain a definition for 'PointToClient' and no extension method 'PointToClient' accepting a first argument of type 'System.Windows.Forms.ToolStripMenuItem' could be found (are you missing a using directive or an assembly reference?)
I am sure that the child menu item has been shown before that.... please help....
Thanks for replying......
|
|
|
|
|
Ohhh my god, what was i thinking, ofcause you cannot do that... sorry m8. Let me get back to you in a moment...
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
|
skhan17 wrote: all the supervisors in the MSc. program are crazy
I can imagine.
I have no idea, but will Focus have any effect?
|
|
|
|
|
I don't know how to use focus.....can you please let me know how to use mouse focus... then I can use it..maybe
|
|
|
|
|
Hi,
Handle the DropDownOpening or DropDownOpened event that occurs when your child menu appears. The Location property will get the screen coordinates of the top left corner of the menu. Apply an offset to put the cursor into the centre of the first menu item.
private void MenuItem_DropDownOpening(object sender, EventArgs e) {
ToolStripMenuItem menu = (ToolStripMenuItem)sender;
Point p = menu.DropDown.Location;
Rectangle r = menu.DropDown.Items[0].Bounds;
p.Offset(r.Width / 2, r.Height / 2);
Cursor.Position = p;
}
Alan.
|
|
|
|
|
THANK YOU SO MUCH..... although I have figured out a manual way.... but this works like a charm....
|
|
|
|
|
PIEBALDconsult wrote: That sounds user-hostile
Agree completely...
Tell me something, how will you go to menu no 3? when you are moving your mouse down to item 1, and then to item 2, and boom... your mouse has gone to item 2.1
HORRIBLE
|
|
|
|
|
I agree with you too...it's horrible.... and since we both agree on this issue, there is a little chance that you'll also agree that some people always see the glass as half empty...... I chose not to be one of them.
Finally thank you Alan N for helping me out.
|
|
|
|
|
Dude!!
I am not seeing the glass as half empty buddy... There is a fine line between optimism and hallucination.
I am always there standing next to you on optimism and I would always prefer to see the glass as half full...
I only reasoned my doubt that how would you go to Menu Item number 3 in your example. Think about it. It is practically just not possible. By implementing this (so called) feature you are inviting a fault in your menu functionality.
Now, the optimism part:
May be your requirement is that you do not want your user to reach Item 3 and 4. Then, this could be one of the innovative ways. However, in that case, I would have simply preferred disabling the items.
skhan17 wrote: Finally thank you Alan N for helping me out.
Welcome... I do think i have helped you... Provided you understand the problem here.
|
|
|
|
|
In any case, I am sure you don't believe me. So here is a code made for you. Run it and you will see it.
public class Form2 : Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.item1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.item1ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.item2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.item3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.item4ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.subItem1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.subItem2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.item1ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(491, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
this.item1ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.item1ToolStripMenuItem1,
this.item2ToolStripMenuItem,
this.item3ToolStripMenuItem,
this.item4ToolStripMenuItem});
this.item1ToolStripMenuItem.Name = "item1ToolStripMenuItem";
this.item1ToolStripMenuItem.Size = new System.Drawing.Size(77, 20);
this.item1ToolStripMenuItem.Text = "MainMenu";
this.item1ToolStripMenuItem.MouseMove += new System.Windows.Forms.MouseEventHandler(this.item1ToolStripMenuItem_MouseMove);
this.item1ToolStripMenuItem1.Name = "item1ToolStripMenuItem1";
this.item1ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
this.item1ToolStripMenuItem1.Text = "Item 1";
this.item2ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.subItem1ToolStripMenuItem,
this.subItem2ToolStripMenuItem});
this.item2ToolStripMenuItem.Name = "item2ToolStripMenuItem";
this.item2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.item2ToolStripMenuItem.Text = "Item 2";
this.item3ToolStripMenuItem.Name = "item3ToolStripMenuItem";
this.item3ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.item3ToolStripMenuItem.Text = "Item 3";
this.item4ToolStripMenuItem.Name = "item4ToolStripMenuItem";
this.item4ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.item4ToolStripMenuItem.Text = "Item 4";
this.subItem1ToolStripMenuItem.Name = "subItem1ToolStripMenuItem";
this.subItem1ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subItem1ToolStripMenuItem.Text = "SubItem1";
this.subItem2ToolStripMenuItem.Name = "subItem2ToolStripMenuItem";
this.subItem2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subItem2ToolStripMenuItem.Text = "SubItem2";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(491, 388);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form2";
this.Text = "Form2";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem item1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem item1ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem item2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem subItem1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem subItem2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem item3ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem item4ToolStripMenuItem;
public Form2()
{
InitializeComponent();
SetMenuDetails(item1ToolStripMenuItem);
}
private void SetMenuDetails(ToolStripMenuItem menu)
{
foreach (ToolStripMenuItem item in menu.DropDownItems)
{
UpdateItem(item);
}
}
private void UpdateItem(ToolStripMenuItem item)
{
foreach (ToolStripMenuItem subItem in item.DropDownItems)
{
UpdateItem(subItem);
}
item.MouseMove += new MouseEventHandler(item_MouseUp);
}
void item_MouseUp(object sender, MouseEventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
if (item.DropDownItems.Count > 0)
{
ToolStripMenuItem subItem = (ToolStripMenuItem)item.DropDownItems[0];
item.ShowDropDown();
Point location = subItem.Bounds.Location;
Cursor.Position = new Point(Cursor.Position.X + location.X + item.Bounds.Width/2, Cursor.Position.Y + location.Y );
}
}
private void item1ToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.Location.ToString();
}
}
|
|
|
|
|
I wanted to create a custom control property that includes checkboxes, just like ShortCutKeys property for ToolStripMenuItem. How would I create something like this?
thank you
|
|
|
|
|
Let me see if i get this right, you want to put some custom control into the ToolStrip or MenuStrip right ???
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
ok, sorry for not being more clear. No, I have a custom control. And for this control, I need to create a custom property that has checkboxes in my customs control's property grid. Similar to 'Dock' property that displays a clickable custom graphic on the dropdown. Or if you look at 'ShortCutKeys' property for 'ToolStripMenuItem' control, you'll see a drop down with checkboxes.
So lets say I have a custom Button control with custom 'Text' property. And when a user sets this 'Text' property, in the property grid dropdown for this property, I need to display checkboxes with values.
|
|
|
|
|
|
A good answer. It helps me to resolve my question.
|
|
|
|
|
Does any one know how much storage space a Type object requires? For example, an instance of a Type contains a multitude of properties. Are these properties stored in memory or retrieved when needed?
Also, does anyone know how fast the "is" function is evaluated? I've been using the "is" function a lot more frequently and I don't want to inadvertently be slowing down my code.
Example:
if (variable is ISomeInterface1)
{}
else if (variable is ISomeInterface2)
{}
else if (variable is ISomeInterface3)
{}
...
If my answer has helped you, one of my articles may also be a help. Also remember that your best friend's name is google.
|
|
|
|
|
AFAIK there is no "Type" object - instead every object has a type, which defines what properties and methods the object has. As such, a "Type object" has no specific size - it depends on what kind of object it is!
As for the "how fast is "is ", why would it matter - how would your code work if you didn't check what type you are receiving, unless it is a single type of object? I.e. if your code is similar to a click event
void ClickIt(object sender, EventArgs e) then you have to use either
if (sender is MyClass)... or
MyClass mine = sender as MyClass in order to use the parameter.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I assume he means if you do System.Type t = o.GetType() ; , how much space will be used.
Thinking of it that way... do you just get a reference to the class in the loaded assembly?
|
|
|
|
|
Dunno - but this is making my brain hurt! (I had forgotten about the System.Type class; I try to avoid using reflection if I can.)
What is he doing that makes the size of a Type important, I wonder?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|