How do I determine when to show a tooltip? - c#

I'm writing a calendar control in .Net WinForms that will show a tooltip for each date.
What's the best way to determine when to show the tooltip?
Showing it immediately in MouseMove would make it get in the way, so I'd like it to show when the mouse hovers over each date cell.
The MouseHover event only fires on the first hover after MouseEnter, so I can't use it.
What's the best way to do this?
EDIT:I'm using WinForms

The time delay between Enter and Hover is specified in SystemInformation.MouseHoverTime.
If for some reason the built-in tooltip handling code for whichever UI framework you're using isn't sufficient, you could just spin up a Timer after each MouseMove and show a Tooltip when it fires. Obviously, you'd need to reset the Timer each time the mouse is moved to prevent a long series "rain of tooltips".

It would be helpful to know which technology you are using (ASP.NET? Forms? WPF?) because they all have different ways of implementing tooltips:
With ASP.NET, you can simply set the
ToolTip property of a control (such
as a Label control that shows a number in your calendar), and it
will automatically show a tooltip
after a slight delay when hovering
over the control.
In Forms, I think you have to
actually create a ToolTip object then
attach a control to it.
In WPF, you can add a Label.ToolTip
element to your XAML code.
In all cases, though, there's a built-in way to do it, so it might not be necessary for you to write your own code at all.
If your situation is so custom that you do need to write your own code, I'd really need to know more about how you are representing the numbers in your calendar to help you out.
Last thing: you didn't really ask this--and it may not be under your control--but you might want to ask yourself whether a tooltip is the best way to show calendar information in the first place. If space is really tight, then the answer might be "yes", but if you have enough space to show the calendar events (or even the first few words of each event), this would save the user from having to "scrub the whole calendar" (i.e., roll over each date individually).
-Dan

Have a look at the AutoPopDelay, InitialDelay and ReshowDelay properties of the ToolTip class, as they control the behaviour of the tooltip.
I generally play around with the values until I get something that 'feels' right. It's annoying when a tooltip shows immediately, and for short tooltips, it's also annoying when they disappear too soon. For really long tooltips, say, several paragraphs (yes, poor design decision, but if there's a lot of info to read, at least let me read it!) then it should stay open as long as my mouse is stationary.
A tooltip example from MSDN gives the following values:
AutoPopDelay = 5000;
InitialDelay = 1000;
ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
ShowAlways = true;
As mentioned in a comment, the poster wants to trigger the tooltip programatically. For that, ToolTip.Show( ) needs to be called. To get a delay effect, you'll likely want to have a timer running which counts the time that the mouse is stationary. Whenever the mouse enters, leaves or moves within the control, this time should be reset.

Related

How to set Custom control as ToolTip for Button or Label?

I have created a custom control of my own and i am in a need of making this custom control as a ToolTip for the Labels or Buttons.
But i could not find a way to set the Custom control as ToolTip.
Anyone please help me on setting the Custom control as ToolTip.
Note:
I don't need solution with showing the Custom Control in mouse_hover events of controls.
Please suggest me ideas to make the custom control as default ToolTip in standard way.
Regards,
Amal Raj
I assume that you already know about overriding the paint event, so I won't go into that. If you want anything a bit more complicated, deriving from the ToolTip control to extend it for your purposes won't make much sense since you'll run into restrictions quite fast.
What you should do is implement your own ToolTip control by reusing some important bits from the original one. If you're feeling adventurous you could follow these steps to get started. I'm going to refer to your custom control as tooltip from now on:
If you want to show custom text or something else for each control that uses your tooltip, you need to implement IExtenderProvider in your class. Here's more about it.
You need to keep track of controls that are using your tooltip and the custom values you've set for them. Internally, Windows Forms tooltip uses a HashTable for that purpose. Key is the control showing your tooltip and value is the tooltip text (or something else you want to tie to your tooltip).
If you want to have more than just one string to show (title, description, image etc), you can have multiple HashTables.
When adding the tooltip to a control, subscribe to mouse events (enter, move, leave) to keep track of when to show the tooltip. If you want to have a delay before showing the control, you need to use an internal timer to keep track of time.
You'll most likely want the tooltip to extend outside the main form's boundaries. You could wrap your tooltip inside a headerless form or an alpha blended form to allow other shapes than rectangle.
Those are the very generalized first steps. In reality, there's quite a bit more to be done. It's been a few years since I implemented my custom ToolTip control so I might be forgetting something crucial. However, if you spend some time poking around the code of Windows Forms's ToolTip class, you'll get quite a good idea of what's going on behind the curtains.
I haven't reviewed the code myself but judging from the ratings, this article will give you a good starting point too: A ToolTip with Title, Multiline Contents, and Image. It's in VB.NET but you can easily convert it to C# by using Telerik's converter or any other.

How do I make a button invisible but still functional?

I am currently trying to make a program where I have a picture of a calendar and when the user clicks on any given Friday a message box will appear. The current method I have been trying to do this is by placing a button over the dates, but I can't seem to find a way so that the button will be invisible and functional at the same time.
This is all in C# Windows Forms Application.
Any ideas?
There are several solutions:
Use an empty PictureBox instead of a Button. This is the nearest and quickest solution to what you say you want. Note that this secondary PictureBox needs to be a child of the calendar's one (with its background color set to Transparent). Transparency in Windows.Forms only applies to direct parents (it's more complex than this, but let's simplify).
Use the MouseUp event on the PictureBox where you are showing the calendar, and use the MouseEventArgs supplied as arguments to the event handler to find the X and Y position of the mouse within that control when the button was clicked.
Use a decent calendar/datepicker control instead of showing an image of one
Matter of fact: I don't endorse #1, and just put it there since it's what you seem to be asking for. I'd rather go with #2 or #3 (specially #3)
PS: if you want to really simulate Click, you should need to handle both MouseDown and MouseUp (a click usually means pressing a mouse button down on a control then releasing it within the same control)

Balloon pop up over control mouse enter/exit

Hello,
Above is the program I am writing. On the right panel is basically two custom controls (the blue rectangle area) I created and just added them as controls to the background panel control when this winform program loads.
I used MS paint to draw out the pop up balloon that I want to see when my mouse enter this control's area. I want to do the following:
1. If mouse enter the control area, the yellow area balloon pop up and populate with the information of that specific control
2. If mouse move out of the control area, the pop up balloon disappear.
Can this be done with Winform application? I looked around and found out about Tooltip class but so far from researching I don't know if it does what I want to do.
I could be wrong but googling around gave me the impress that Tooltip offers very little in term of style. Ideally I want to make this pop up balloon into almost like a border-less pop up window where I can put image , font ect.....at will. Also Tooltip works if you hover over a button or specific field whereas I want the entire control area.
Can this be done? I appreciate if you can point me to any work around if there is one.
I wrote a comment, but I figure I'll expand it into a full answer. This is assuming you want a new control, which isn't a tooltip, for maximum customizability. I did something similar to this for work recently, to act as a non-modal info popup that disappears when clicked.
Creating a Custom Popup Form
What you want is essentially a floating popup that appears over your form, which means you'll want to define a new Form object, rather than a UserControl, as it won't actually be embedded within your other form.
Give it a multiline, non-editable textbox that you can fill with the information you want to populate, then simply call a new instance of the form on your Mouse_Enter event. Close it upon Mouse_Leave.
Adjusting The Style
You'll have to play with it a bit to get it to actually act like a popup and not just a window. I'd recommend setting it to a non-modal popup, and removing the border. You can write a function to automatically size it to its contents. I don't imagine you'll want the user manually resizing it.
Some other things to look into would be overriding the CreateParams property that comes with the basic Form object. You can force DropShadows and TopMost forms without making the form modal. Overriding ShowWithoutActivation to always return true will prevent the form from stealing focus when it pops up.
I'm not sure if you can pull off rounded edges like you have in your mockup. Perhaps you can pull it off with some wizardry in the OnPaint() function, but I couldn't tell you how to do it.
It might be a bit of a pain for fiddling around with, but you can get some good functionality and appearance out of it. If you think you can pull it off acceptably with the ToolTip class, go for it. It took me about a week to get my notifications where I wanted them (though I added several features that you probably don't need to worry about).
Examples
Some keywords to look up in related searches would be Toast Notification and Non-Modal Popup. This might be some use:
http://www.codeproject.com/Articles/442983/Android-Style-Toast-Notification-for-NET
Since you already have implemented custom user controls you might want to try it again. Make a control that is that style and color, changes it's size based on it's text. You can feed it information (such as the text to display) from your existing user control object. You can also have the mouse enter/leave code reside in your first user control.
If you're not sure how to make a rectangle with round corners you can either make it on the fly using a graphics object (which will turn into a bitmap on the screen) or make it how you want it to look in GIMP (or photoshop if you have it) then use that image as the background on your user control. Make the default background transparent (so your voids above the round corners are not grey). If you make a pre loaded image you'll need to be aware you will only be able to scale it equally in Y and X directions. unequal scaling will make it look distorted.
Can you use the Mouse_Enter event on the control?

How can I write my own ContextMenu? C#

I feel quite limited by the default ContextMenuStrip, as it only can contain buttons, and no Controls.
I was wondering that for a long time, and I already tried it, using forms, but it never really worked out.
I already have I idea on how to set the whole thing up, with events and items. The only problem I have is the paint method.
When you open a ContextMenu (ContextMenuStrip) you can set its position on the mouse cursor, and it will be there, even if that means that it goes beyond the active form. (So I can't use the Controls Class as inheritance, as they can only draw themself as a part of a form.
Now I thought to use the Form Class as a base for my ContextMenu, but those where placed on the screen randomly.
So what I actually need is a class (or something similar) that can draw itself, without problems, and can be placed accurately on the screen.
Any hint would be nice, thanks.
Greg the Mad
Your first statement is false -- you can have a TextBox or a ComboBox in a ContextMenuStrip.
MSDN ToolStripComboBox
MSDN ToolStripTextBox
From the designer there is a small drop-down arrow when your mouse is in the "Type Here" box (sometimes hard to click) that will allow you to change the type.
If you are looking to allow for any type of control to be displayed in a top down fashion inside of a container to be positionable... you could always make a custom control using FlowLayoutPanel. With it's properties FlowDirection=TopDown and WrapContents=False to keep a vertical approach. This will handle your "menu" basics and your new control can expose whichever events you wish from each Control. You will have to handle the logic of showing the panel and positioning with it's Location property as well.
I forgot to address the issue with drawing outside of the parent form. Notice that ContextMenus are smart and when they reach a boundary of their parent they draw away from it. You should logically be able to draw in the correct direction (Up/Down or Left/Right) from any right mouse click. Per your attempt with a Form, set StartPosition=Manual then prior to calling Show() or ShowDialog() set it's Location property respective to the X and Y parameters provided in the event args of MouseClick.

Can I set an infinite AutoPopDelay for a tooltip in a .NET Windows Forms window?

I have a requirement to not have the standard .NET Windows Forms tooltip automatcially hide - that is, I need them to remain visible until the mouse moves off the control that has the tooltip. I'd like to avoid having to use specific MouseEnter and MouseLeave events for all controls with a tooltip. I'm happy to hear about third-party solutions if they'd solve the problem.
As it has been posted countless times and is known from the .NET Framework early days, you cannot set the AutoPopDelay time higher than an Int16.MaxValue (i.e. 32767) and have it working. Using the tooltip Show() method leads to the same result. Any value higher than 32767 leads the timer to be reset to 5000 ms.
Even an attempt to set the AutomaticDelay to a value that leads to an AutoPopDelay value higher than 32767 will cause the tooltip not to work. Furthermore, using the StopTimer() method doesn't yield any positive results.
The only way I was succesful in displaying a tooltip with infinite timeout was using the tooltip Show() method that passes the displayed position. tooltip.Show(text, control, position). In conclusion, the tooltip control has the most bizarre behavior I have yet encountered in a control and the documentation about setting the delay times is quite misleading.
I was searching for a solution to the problem
The popup baloon disappears too fast, not allowing me to read its content
The popup baloon does not appear again when hovering into that region.
I found in the forums the following answer, which does not solve Problem2, but lets me read the text for more than the time I need when I set its value to 32766 and is thus - in my opinion - a kind of 'work around':
Actually, I found out my problem was that I assigned a value higher than 32767 (32768 and higher will have an effect of 5 sec) really
weird, since AutoPopDelay is supposed to take an Integer (32 bit) as
parameter, and 32767 is really a signed 16 bit ...
But I have to admit, it still sucks...
But setting it to 32000 (32 seconds) seems to work better. No clue why this is but maybe it helps someone.
In my application, the tooltip text has to update every 10 seconds. What works for me is to deactivate and activate the tooltip every time this text is updated. When I set the AutoPopDelay to more than 10 seconds, the tooltip will effectively stay forever. So you might use a timer to repeatedly activate and deactivate the tooltip:
private void timer1_Tick(object sender, EventArgs e)
{
toolTip1.Active = false;
toolTip1.Active = true;
}
To minimize the flicker that happens on reactivating the tooltip, set the InitialDelay to 1. When you set the AutoPopDelay to 30000 and the timer interval also to 30000 you'll see a very short flicker only once every 30 seconds. For my application this short flicker is acceptable. Btw, don't forget to turn on the timer! (At first I thought it didn't work, until I discovered I forgot to turn on the timer ;-))
Of course this is a cure for the symptoms and not an elegant solution to the problem.
Try myToolTip.ReshowDelay = 0.
You just reshow the tooltip immediately after its delay expires. It worked for me.
This is not going to be the answer you want to hear... Roll your own. I once had scenario where I wanted to simulate the new Office's ribbonbar tooltip behavour for shortcut keyboard commands and I found that the standard tooltip API is just too limited.
Make a small, simple and border-less form instance for every control that you need a tooltip for. Then show and hide them non-modally as you do a mouse-enter or -leave event. Have the constructor for this window receive the UI control instance and let it query the control for it's events so that it can attach itself to the mouse-enter and -leave events, so that you wouldn't have to wire them up manually.
In my limited experience, I have found that setting the showAlways property to true has the side effect of making the tip display "infinitely" (until the cursor exits the control). This is not what the documentation says it will do, but that's what I have experienced. (However, I am using the Janus Supertip component, and it's possible that affects the behavior).
I think you are trying to cure the symptoms and not the cause of your problem.
You are trying to force a ToolTip to be something different from a ToolTip. A tooltip is, by definition, "a small "hover box" with information about the item being hovered over".
If the user can't read what the tooltip is trying to suggest in 30 seconds, then it isn't really a "tip" but an entire chapter from the help file. Put lengthy instructions into the Manual and do not attempt to force it into a ToolTip that stays open forever.
So, I ended up de-activating the tooltip, but I am still using it to hold the text for the tooltip, because it neatly adds itself to all the controls. I made my own panel and label inside it for the tooltip and dynamically positioned, made visible and populated the text on the mouse enter and mouse leave events of all the controls.
I also wrote a recursive function to add the mouse events to each label control (the only ones I have tooltips on), so I didn't have to manually to it and won't have to remember to do it when I add a new label. New developers on the project may not ever realise that the tooltip control is not actually rendering the tips.
Why a label in a panel? - so really big tooltips can scroll - although this is still not working nicely.
It is not real pretty, and I am still looking for a good answer if you've got on (or a critique of the method I've employed).
I came across http://www.binarymission.co.uk/, but their tooltip did not seem to render properly in my .NET 3.5 application. I am still talking to them.
The ToolTip.StopTimer method might help. From MSDN:
The ToolTip class has an internal timer that it uses to set the display duration for ToolTips. The duration associated with this timer is set through the AutoPopDelay property. The StopTimer method will stop this internal timer, causing any displayed ToolTip to be shown modally until the Hide method is called, or the parent form is minimized, hidden, or closed.
Set the ToolTipService.ShowDuration attached property to int.MaxValue:
<Button
Height="23" Width="75"
Content="Click Me"
ToolTip="My ToolTip" ToolTipService.ShowDuration="2147483647"
/>

Categories

Resources