Click here to Skip to main content
15,885,216 members
Home / Discussions / C#
   

C#

 
GeneralRe: Aborting a Thread. Pin
Luc Pattyn26-Dec-09 3:00
sitebuilderLuc Pattyn26-Dec-09 3:00 
QuestionOpen picture from application and edit with default picture edit application ? Pin
Yanshof25-Dec-09 10:27
Yanshof25-Dec-09 10:27 
AnswerRe: Open picture from application and edit with default picture edit application ? Pin
Luc Pattyn25-Dec-09 11:21
sitebuilderLuc Pattyn25-Dec-09 11:21 
Questionvisual studio and UML diagrams and unit testing Pin
Ryan Minor25-Dec-09 5:46
Ryan Minor25-Dec-09 5:46 
AnswerRe: visual studio and UML diagrams and unit testing Pin
Dimitri Witkowski25-Dec-09 12:20
Dimitri Witkowski25-Dec-09 12:20 
QuestionLabel control that set the focus to the related Textbox on the mousehover event Pin
ArjenGroeneveld25-Dec-09 4:23
ArjenGroeneveld25-Dec-09 4:23 
AnswerRe: Label control that set the focus to the related Textbox on the mousehover event Pin
Md. Marufuzzaman25-Dec-09 8:04
professionalMd. Marufuzzaman25-Dec-09 8:04 
AnswerRe: Label control that set the focus to the related Textbox on the mousehover event Pin
Martin#14-Jan-10 9:48
Martin#14-Jan-10 9:48 
Hello,

Just came across your interesting question.

If I got it write, I think you need to deal with inherited controls and a customzied property editor.

I will try to give you an idea of how you can solve your problem, but will not sneak into the time costing details (I will mark them for you).
ArjenGroeneveld wrote:
like to create a Label control

What you need first is a custom label which derives from Forms.Label.
public class LabelExtended : System.Windows.Forms.Label
{
   ...
}

ArjenGroeneveld wrote:
One the respective control is set in Design time,

To make it easy (for me), I will use a string property to hold the name of this selected control (how we do this will be next).
private string selectedControlName = string.Empty;

[
CategoryAttribute("Arjens Properties"),
DescriptionAttribute("Holds the name of the selected control."),
DefaultValueAttribute(""),
EditorAttribute(typeof(SelectControlEditor), typeof(UITypeEditor)) // this is the key
]
public string SelectedControlName
{
    get { return selectedControlName; }
    set { selectedControlName = value; }
}

ArjenGroeneveld wrote:
an property in which I can select the available controls on the Form (except for the Label controls).

We just added an EditorAttribute to the property. This is a type of SelectControlEditor and derives from UITypeEditor, which we have to create now.
using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing.Design;
...
    public class SelectControlEditor : UITypeEditor
    {
        ...
    }

What I want to show you is a DropDown editor, where you are able to select a control out of a list of control names.
First we give the editor class the information about the DropDown, by overriding the GetEditStyle method like this:
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
    if( context != null ) return UITypeEditorEditStyle.DropDown;
    return base.GetEditStyle(context);
}

Now we need to tell the editor how to edit your property, by overriding the EditValue method.
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
    if ((context != null) && (provider != null))
    {
        // Access the Property Browser's UI display service
        IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        if (editorService != null)
        {
            Control owner = context.Instance as Control; // get the control which is the owner of your property
            if (owner != null)
            {
                // Create an instance of the UI editor control
                SelectControl selectControlEditor = new SelectControl(editorService, owner); // your CustomControl which is shown in the designer (will be descriped next)

                // Pass the UI editor control the current property value
                selectControlEditor.SelectedControl = (string)value;

                // Display the UI editor control
                editorService.DropDownControl(selectControlEditor);

                // Return the new property value from the UI editor control (over the SelectedControl property),
                // but only if the Valid property if the custom control is set to true!
                // Else return the old value.
                if (selectControlEditor.Valid)
                {
                    return selectControlEditor.SelectedControl;
                }
                else
                {
                    return value;
                }
            }
        }
    }
    return base.EditValue(context, provider, value);
}

Now we create an UserControl named SelectControl, which inherits Forms.UserControl.
On this UserControl we add a ComboBox named "cbSelectControls", and a Button named "bOk" (for the button we handle the Click Event).
I will make additional commends in the code:
public partial class SelectControl : UserControl
{
    IWindowsFormsEditorService editorService = null;

    public SelectControl(IWindowsFormsEditorService editorService, Control ownerControl)
    {
        InitializeComponent();

        this.editorService = editorService;
        if (ownerControl != null)
        {
            // get parent Control of the properties owner control.
            Control parentControl = ownerControl.Parent;
            if (parentControl != null)
            {
                // iterate over all controls in the parentControl Collection
                foreach (Control c in parentControl.Controls)
                {
                    // if the control is not of the type LabelExtended
                    if (!c is LabelExtended)
                    {
                        // add the name to the Items Collection of the combobox.
                        cbSelectControls.Items.Add(c.Name);
                    }
                }
            }
        }
    }

    // is set initial by the UITypeEditor,
    // and holds the selected Text from the combobox.
    public string SelectedControl
    {
        get { return cbSelectControls.Text; }
        set { cbSelectControls.Text = value;}
    }

    bool valid = false;
    // default set to false.
    // only true if the ok button was clicked.
    public bool Valid
    {
        get { return valid; }
        set { valid = value; }
    }
 
    // click event sets Valid to true and closes the dropdown. (finish edit of the property)
    private void bOk_Click(object sender, EventArgs e)
    {
        Valid = true;
        editorService.CloseDropDown();
    }
}

What we now have done is:
If you select your label in the designer and edit the SelectedControlName over the "..." button, an dropdown will be shown, with your UserControl on it.
On this UserControl you will hopefully find a combobox filled with the controls names, which are in the same collection as your label is.
If you now select a controls Name and click the ok Button, it should appear at the SelectedControlName property of your label.
ArjenGroeneveld wrote:
this control will get the focus in Runtime when the user moves with the mouse over the Label control

For this task we could override the OnMouseEnter Method of your LabelExtended class, like this:
protected override void OnMouseEnter(EventArgs e)
{
    // not in DesignMode
    if (!DesignMode)
    {
        // only if the is a valid control name set
        if (!string.IsNullOrEmpty(SelectedControlName))
        {
            Control parentControl = this.Parent;
            if (parentControl != null)
            {
                // you know this from the code above ...
                foreach (Control c in parentControl.Controls)
                {
                    // if the actual controls name is equal to the slected name
                    if (c.Name.Equals(SelectedControlName))
                    {
                        c.Focus();  // set Focus!
                    }
                }
            }
        }
    }
    base.OnMouseEnter(e);
}


Please note:
.)I haven't fully tested the code and didi some changes during editing with no compilation run.
.)The code as it is will only take care of controls which have the same parent Control than your label has. So some (not too hard I guess) work is still open. Don't forget to create unique names if you consider to look in subcontainer.

Please also note:
.) I had great fun looking for a solution for your problem, so thank you for asking!

I hope it helps you. (Would be nice to get a feeback)

All the best,

Martin

QuestionRichTextBox - Text Pin
dataminers25-Dec-09 1:35
dataminers25-Dec-09 1:35 
AnswerRe: RichTextBox - Text Pin
santhosh-padamatinti26-Dec-09 7:17
santhosh-padamatinti26-Dec-09 7:17 
GeneralRe: RichTextBox - Text Pin
dataminers29-Dec-09 22:45
dataminers29-Dec-09 22:45 
QuestionDisplay user control in place of default tooltip Pin
billu 225-Dec-09 1:16
billu 225-Dec-09 1:16 
AnswerRe: Display user control in place of default tooltip Pin
Abhijit Jana25-Dec-09 1:42
professionalAbhijit Jana25-Dec-09 1:42 
GeneralRe: Display user control in place of default tooltip Pin
billu 225-Dec-09 1:53
billu 225-Dec-09 1:53 
QuestionSynchronisation in Multi Server environment Pin
c0der200925-Dec-09 1:11
c0der200925-Dec-09 1:11 
AnswerRe: Synchronisation in Multi Server environment Pin
Jimmanuel25-Dec-09 7:07
Jimmanuel25-Dec-09 7:07 
GeneralRe: Synchronisation in Multi Server environment Pin
c0der200925-Dec-09 20:36
c0der200925-Dec-09 20:36 
GeneralRe: Synchronisation in Multi Server environment Pin
Jimmanuel26-Dec-09 2:35
Jimmanuel26-Dec-09 2:35 
GeneralRe: Synchronisation in Multi Server environment Pin
c0der200927-Dec-09 18:14
c0der200927-Dec-09 18:14 
GeneralRe: Synchronisation in Multi Server environment Pin
Jimmanuel28-Dec-09 0:51
Jimmanuel28-Dec-09 0:51 
Questionis there any binary to decimal convertor class Pin
hotthoughtguy25-Dec-09 0:15
hotthoughtguy25-Dec-09 0:15 
Answerrepost Pin
Luc Pattyn25-Dec-09 1:19
sitebuilderLuc Pattyn25-Dec-09 1:19 
GeneralRe: repost Pin
#realJSOP25-Dec-09 22:51
mve#realJSOP25-Dec-09 22:51 
GeneralRe: repost Pin
dan!sh 25-Dec-09 23:02
professional dan!sh 25-Dec-09 23:02 
QuestionAccess the path geometry using C# Pin
sajib_bd24-Dec-09 21:22
sajib_bd24-Dec-09 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.