Is there a dynamic-creation-friendly LinkLabel Alternative? - c#

I'm trying to dynamically create a link from a Windows Form to our website when certain conditions are met (it's a warning message with further information in our online manual).
Currently I'm finding LinkLabel quite unwieldy to use in this situation: having to set up LinkClicked handlers on the fly for a straightforward hyperlink seems inelegant.
Is there a wrapper or alternative that fulfills the following requirements?:
Inherits from System.Windows.Forms.Control (so I can use it in a TableLayoutPanel)
Has reasonably low setup (no strange LinkClicked function pre-visit checking, for example)
Isn't bound to a specific browser

What is your problem with the LinkClicked event handler? You would have to do the same for almost any control in order to do anything useful.
Anyway, it would be trivial to implement yourself - create a class that inherits from LinkLabel, add a string URL property (you may need an attribute to make this show in the designer properties panel if you want to set it that way) and provide an event handler that opens the browser with that URL.
Then you can just add the control in the designer (or at runtime), set the URL property and it will work without having to set event handlers.

Did you use the LinkClicked event instead of OnClick? Then you can use this in the event handler:
(sender as LinkLabel).LinkVisited = true;
System.Diagnostics.Process.Start("http://example.com");
It's not bound a specific browser - opens in the user's default browser. The setup is low - just instantiate the LinkLabel, add an event hookup to LinkClicked (which is one two-line method) and add it to the page. What's unwieldy about this approach?

In the end I used LinkLabel.Links.Add to modify the link destination dynamically..

Related

VCL's CustomControl equivalent in Windows Forms

Delphi's VCL has a very useful class, CustomControl. This class is a direct base class of Control, which is equivalent to C# WF's UserControl.
For those, who are not familiar with VCL, CustomControl differs very little from Control; the main difference is that most of properties are protected; when implementing the new control, developer may decide, which ones does he want to publish and which ones shall remain hidden.
I'm developing my own control for Windows Forms and I want to hide some properties and events. For instance, I don't want to expose the MouseDown event - instead I allow capturing clicking on control's elements.
Is there an equivalent of VCL's CustomControl in Windows Forms? If not, how can I hide unwanted public properties and events in my control?
In response to answers:
This is not a matter of security, but rather a matter of code elegance. In Delphi I can derive from CustomControl, leave the OnMouseDown event protected (as in C#'s protected) and say to the control's user:
You cannot use OnMouseDown, because there is none. If you want to react to user clicking on control, simply use OnElementClicked - you'll even get detailed information about which element was clicked and what was its state.
I may disable calling the MouseDown event as Hans Passant suggested, but then I would have to include the following in the control's user's manual:
Please do not use the MouseDown event, because I've overridden the OnMouseDown method, such that it won't call the MouseDown event. This is because the control's logic is designed in such way, that you should use OnElementClicked rather than OnMouseDown. Please don't criticize the control because of MouseDown not working. Please don't report it as a bug, because it is by design. Please don't post messages in forums or create blog entries explaining how to fix the MouseDown problem by inheriting the class and manually calling the MouseDown event, because it would break the control's logic. Pleas don't... damnit, told you so!
If someone actually inherits from my control - I assume then, that he knows, what he's doing (also because one would then gain access to my control's internal logic as well). But if someone just uses my control, I would give him only these properties, events and methods, that I'm sure will work as designed.
I hope it explains my motives :)
There's a fundamental difference between hiding, what you asked for, and making it inaccessible, what I assume Delphi does. Hiding is simple, just repeat the declaration and apply attributes:
using System;
using System.ComponentModel;
using System.Windows.Forms;
class MyControl : Control {
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
private new event MouseEventHandler MouseDown;
}
Which prevents the event from being displayed in the Properties window, it won't show up in the IntelliSense dropdown and generates a compile error when he tries to assign the event in code anyway.
It is however not an absolute guarantee that the client programmer couldn't work around the restriction anyway. By casting or overriding your class for example. But the ultimate backdoor is implementing the IMessageFilter interface, nothing you can do about that. So this ought to be good enough if elegance is the goal.

Silverlight toolkit - intercepting drag start

I can't find a way to intercept the beginning of the drag operation in SLToolkit; I need that in order for my custom controls to indicate the regions where the item could be dropped. Unfortunately, there is no IsDragInProgressChanged event; I looked at the sources and the only way I found was to subclass all DragDropTarget<,>s and override OnItemDragStarting method, which, in my opinion, is way too complicated and intrusive. Anyone knows of a better method?
Both the PanelDragDropTarget and ListBoxDragDropTarget have an event named ItemDragStarting. Adding an event handler in the XAML and handling it in the code-behind should be all you need.

Show Lync ContactCard in Silverlight 4 by code

I would like to be able to show ContactCard/PopupContactCard control on MouseEnter event of a control.
Does anyone know how to show the contact card?
So far I was not able due to internal/protected modifiers of vital methods.
I can do this with the PresenseIndicator control, but not from my custom code in the event handlers.
So any ideas?
You could create either a new page, or a popup section of your existing page, and host a contact-card object in there. Then, on the mouse-enter, pass through the SIP you want to display and show the popup (/show the other page)
The ContactCard control I would probably choose to display is this one: http://msdn.microsoft.com/en-us/library/microsoft.lync.controls.contactcard_di_2_lyncctrlslmref.aspx
Or do you mean that you wish to expand the ContactCard? I'm not sure if this is possiblem, is the IsExpanded property writable?

Silverlight business application initialize check

I am using the Business Silverlight application. I have incorporated some MVVM into this and were off an running with it. We are using some telerik controls, mostly the ribbon control and the docking. We register all the telerik ribbon controls in the about.xaml.cs file, the method is DisplayUI - its here where we register the docking control then we register the ribbon after this. What happens is that when you click the ABOUT link it shows our first tab with buttons(perfect). when you click the HOME link next to the ABOUT link, we go back to the home page..but when you click the ABOUT link again it registers the controls again so we end up with two tabs that are the same.
Is there a way to check to see if this about.xaml.cs file has already been initialized? Im guessing that is has a handle on the first call in memory as I am able to see the first tabs rendering..
Thanks
here is the about code
public About()
{
InitializeComponent();
DisplayUI();
this.Title = ApplicationStrings.AboutPageTitle;
}
that display UI does all the work in registering the dockpanel and the ribbons. We'd like to not have the DisplayUI() called if this has already been rendered once.
If you do it by event handler can you unsubscribe from the event at the end of the method? Without seeing some code it's hard to work out what to change.
It's not the nicest way of doing it, but if this code needs to run once and only once then you could have a static boolean variable on the class set to false and when you call DisplayUI you check the value of this. If it's false you set it to true and run the method, and if it's true you just return.

What winforms control to display message with minor html formatting?

Is there a control I can use to display a short message that contains minor html formatting (eg one or more links). I'd prefer not to use the WebBrowser control (suggested here) as it's a bit heavy for what I want, so any other suggestions welcome.
If a user does click a link from my message I want it to be opened in their default browser, not within my application.
I do use the infragistics controls so one of those would be fine but I don't see any that will do this.
You can use the controls in this article on CodeProject or you can use them as an example for how to roll your own.
I have implemented an HTML control for .NET which may do what you want: see http://www.modeltext.com/html for details.
The control can display HTML including links, and doesn't use a browser.
What happens when the user clicks on a link is up to you: the control generates an event, which can be handled by the application in which the control is embedded; so, your application would instantiate the control, fill it with HTML, install an event handler, and launch the browser when its event handler is told that the user has clicked on a link.
If all you need is the link functionality you can use the LinkLabel control. Assuming that you have set the LinkLabel.Tag property to the url the following code will open the default browser and open the specified web page:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start((string) ((sender as LinkLabel).Tag));
}
I think the best solution in my case might be to use RichTextBox, which has a property DetectLinks and a LinkClicked event that tells you which link is clicked. I'll look into this further...

Categories