How to use tooltip on toolstripbutton - c#

I'm trying to apply a tooltip to a toolstripbutton but it keeps giving me this error:
Operator '==' cannot be applied to operands of type 'System.Windows.Forms.Control' and 'System.Windows.Forms.ToolStripButton'
Any clue on how to solve this?
UPDATE:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (e.AssociatedControl == tBtn1)
{
using (Font f = new Font("Tahoma", 9))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTip1.GetToolTip(e.AssociatedControl), f);
}
}
}

ToolStripButton derives from ToolStripItem which has a ToolTipText property.
As already explained, the ToolStripItem does not derive from the Control class so provides its own implementation to render tool tips. This post may help you with customising the tooltip.

UPDATE: After trying this out in a Winform project and not having success, I searched other threads on SO, this is one that may be helpful to you:
Showing a tooltip on a non-focused ToolStripItem
The problem with the first is you can't set it to the button directly,
it doesn't inherit from Control, and the tooltip won't show up unless
you're over the strip but not over a button.
elsewhere
I was trying to do the same thing and determined it was going to be
pretty challenging and not worth it. The reason is that internally,
the .NET code is specifically designed to only show the tooltip if the
window is active - they are checking this at a Win32 level so its
going to be hard to fake the code out.
The user never accepted any of the answers as true, and it does seem at a glance that this may be a lot of work for little reward. Is this a project from scratch? If so, maybe you can do it using WPF, which is much more flexible than winforms.

Strangely enough, toolstripbutton class doesnt inherit from Control class unlike other System.Windows.Forms gui components. Perhaps in your code e.AssociatedControl is meant to be used with System.Windows.Forms controls. In short, I think MS hasnt decided to provide a tooltip for strip controls. I do not know your exact requirement, but for some alternative that might click, see this link.

Related

Functions like Focus() and properties like Focusable not available for silverlight, only .NET?

Short version: Solutions like the following: How Do I Give a Textbox Focus in Silverlight?
Don't seem to work because functions like Focus and properties like Focusable DON'T EXIST for silverlight and only exist for .net apparently.
Background/Long Version: So I've been trying to get my XNA game to work on silverlight. Porting it was a nightmare but I managed to do it somehow. Currently I want to use ViewBox's so my game is as big as a players browser instead of a set size. When I tried to do it, it VISUALLY worked, but it was impossible to send any keyboard commands to the game. I'm pretty sure its preventing me from focusing it. When I google how to give focus, it gives me links like these where people are saying to use functions like .focus() which DON'T EXIST on silverlight 5 only .NET apparently. For example go here: http://msdn.microsoft.com/en-us/library/system.windows.controls.control(v=vs.110).aspx
It shows the .NET 4.5 version. If you change it to silverlight at the top, all the nice functions and settings disappear. Am I missing something here? How do I access the .Net versions of these classes in silverlight? If its not possible why are all those answers mentioned above use Focus() etc?
Assuming C#...
On your silverlight page, select yourPage_Created from write code menu
Add the following:
partial void YourPage_Created() // this line should be autogenerated
{
this.FindControl("YourControl").ControlAvailable += YourFieldAvailable;
}
private void YourFieldAvailable(object sender, ControlAvailableArguments e)
{
((System.Windows.Controls.Control)e.Control).GotFocus += YourRoutine;
}
private void YourRoutine(object sender, System.Windows.RoutedEventArgs e)
{
// do your focus specific code in here
}
You will now have a custom focus event for the specified control.
Hope this helps :)

How to find the id of dragged control

I am implementing drag and drop functionality among lables. I want to find the ID of a dragged control like label, button, etc so that I can assign a text to it.
I am not sure how to get that data through the events. Any suggestion will help.
you can use Drag_Enter and DragOVer Events, to achieve the goal DragEnter Doucmentation
and DragOver Documentation
alternativily you can check following tutorials
Code project : Drag and Drop in Windows Forms
Code Project : Drag and Drop UI in WinForms
check out System.Windows.Forms.DragEventArgs e
void MyControl_DragDrop(object sender, DragEventArgs e)
{
var controlBeingDrag = (Label)sender; // cast from object
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
}
the control sending drag event is object sender
Assuming you've done your research to figure out how to do drag and drop of controls on your form (and your question is really only limited to your question title: How to find the id of dragged control) most standard WinForm events provide a parameter (object sender) which represents the control used to invoke the event. You should be able to get its ID as you would normally.
Apparently it is less obvious how to consistently get the ID from a WinForm control. Luckily, Brian McMaster has a (fairly old meaning only possibly relevant) MSDN blog post for doing just that. In .NET 3.5 I'd probably use this old post as the start to an extension method for control objects.
If your question is broader than that then you may benefit from #Ravi's links, but on SO we generally expect that you do your own research. Please be sure to do so before asking questions.

How to hide textboxes, labels and buttons C# WPF

I would like to hide several textboxes, a label and a button as soon as a button is clicked... however, for some reason, my code doesn't seem to cause this effect. Nothing appears to happen. I'm using WPF.
Here is my code:
private void doSomething_Click(object sender, RoutedEventArgs e)
{
Name.Visibility = Visibility.Hidden;
}
This code doesn't seem to work.. any ideas?
I believe Visibility.Collapsed is what you need and not Visibility.Hidden.
EDIT: Did you try follow up this code with UpdateLayout() method of parent element/component?
Your code seems to work fine, the "Signing in..." label appears after everything else disappear. I suggest you to just copy all your code from the .xaml.cs file and the .xaml file into a new project, but make sure you don't copy the first line"<Window x:Class="..." because it could generate an error if the class name isn't the same in the new project.
For the xaml code I suggest you not think the same as you design windows forms applications. WPF has the layout system, which re-orientates or re-sizes its elements when re-sizing the window. So you should not specify exact numbers in the margin property as if they where coordinates. Create a grid, create rows or columns for each element and then just set the horizontal or vertical alignment or margins. Think different than the old windows forms way.
I've run your code... and it's working great for me. I've not changed anything (except the variable names) so I guess it's a bug from VS.
As said nikolamm94 try to add this.UpdateLayout(); at the end of connect_Click it might help. I tried and it is still working fine. Or maybe create a new VS projet, it already worked for me a few times.
Sorry my answer is not the most helpful, I wanted to put a comment instead but I don't have enough reputation :/
Please refer: https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx
Set to Visible: tb1.Visibility = System.Windows.Visibility.Visible;
Set to Hide: tb1.Visibility = System.Windows.Visibility.Hidden;
You can hide a textbox by going to properties->appearance->visibility, then setting it to "hidden"

Is there something like an "OnPaint" method in Silverlight?

Is there something like an "OnPaint" method in Silverlight?
Back when I was writing C++, I found that it was easy to use the OnPaint event to customize the display of a class to the screen?
Is there an equivalent in Silverlight? If I want to do something when a UserControl is displayed on the screen, what method would I override?
I noticed this post:
C# WPF OnPaint method alternative?
but it seems that in Silverlight, thre is no "OnRender" method for a UserControl class.
OnPaint was a workaround... to allow you to customise the appearance of controls. That was because you did not have much control over the default appearance of any controls in WinForms applications.
With Silverlight that all changes. Every control is now effectively skinned, using templates and styles, and there are few limitations on how you can customise them. There are far too many links so I just grabbed a couple for you.
Get yourself a good book on Silverlight and learn the proper way to work with it (not around it). This one is one of my favorites.
If you have specific things you are trying to do, to the appearance of user controls, best to list those instead and find out the best way to do it the Silverlight way. :)
You haven't specified what you're trying to do. If you just want to know when a frame is being rendered, the CompositionTarget.Rendering Event will tell you that. If you actually want to draw on the frame being rendered, you cannot do so.
It is LayoutUpdated.
As in:
...
this.LayoutUpdated += new EventHandler(LayoutUpdated);
}
void LayoutUpdated(object sender, EventArgs e)
{}

Interactive Design-Time User Control

I apologise if the title was confusing, it took me nearly 5 minutes to finally think of a title for this one...
Okay, you know how in Visual Studio Express when you add a TabControl to the Form, and you can click on the right-arrow on the top right of the TabControl and it will add a new TabPage, or remove one?
Well, I'm creating a User Control where I need people to be able to switch between Panels (my user control is made up of several Panels). I know this is possible as I've used a Ribbon Control in the past and you could add new buttons etc in the Designer View.
Can somebody please provide any suggestions/advice on how I might go about acheiving this?
Thank you
If I understand your question correctly, you're talking about smart tags.
The process is a little bit involved, so I'm not going to try to post a complete sample. Instead, I'll refer you to this tutorial on the subject. To make a long story short, you have to create a custom designer, and register one or more custom actions. You can use this to create a combo box listing the available panels and switch between them when the selected item is changed.
(Note - the term "smart tags" has two distinct meanings in Visual Studio - I'm specifically talking about the visual designer smart tags, not smart tags in the code editor).
When you make a control that is inherited from Control, you have to make use of a couple of properties such as IsDesignMode, you can then construct event handlers especially for within Design Mode:
if (IsDesignMode){
// Handle the interactivity in Design mode, such as changing a property on the
// Properties toolbox
}
Suppose the control has an event such as MouseClick, you can do this:
private void control_MouseClick(object sender, MouseEventArgs e){
if (IsDesignMode){
// Do something here depending on the Click event within the Designer
}else{
// This is at run-time...
}
}
Another I can think of is 'ShouldSerialize' followed by a publicly accessible property in order to persist the property to the designer-generated code, suppose for example a Control has a boolean property Foo
public bool Foo{
get{ return this._foo; }
set{ if (this._foo != value){
this._foo = value;
}
}
}
public bool ShouldSerializeFoo(){
return true; // The property will be persisted in the designer-generated code
// Check in Form.Designer.cs...
}
If ShouldSerializeFoo returned false, no property is persisted, its the opposite when true, it will be buried within the Form.Designer.cs code...
Hope this helps,
Best regards,
Tom.

Categories