views:

66

answers:

1

Hello

I have a "canvas" (which is just a Panel Control), and the user can click a button to add certain controls to the canvas i.e. labels, link labels, images etc... And then they can edit those controls, like they can edit the text of the label they just added...

But I'm trying to let them choose a new font and a new color for the control that they clicked on, but it doesn't always work, even though it should be...

the code i have is:

private string SelectedControl;

when i click on a control:

private void label_Click(object sender, EventArgs e)
{
    Label label = (Label)sender;
    SelectedControl = label.Name;
}

when the user selects a font:

private void setfont()
{
    foreach(Control control in Canvas.Controls)
    {
        if(control.Name == SelectedControl)
        {
            control.Font = selectedfont;
        }
    }
}

So, This code does work BUT just not all the time. Does anybody know of any other way to somehow keep track of the Last-Clicked control, so it can be referenced later?

+2  A: 

instead of storing the string name, why not store a reference to the last clicked control itself?

so your code becomes:

private Control SelectedControl;

private void label_Click(object sender, EventArgs e)
{
    Control ctrl = sender as Control;
    if(ctrl != null)
        SelectedControl = ctrl;
}

private void setfont()
{
    SelectedControl.Font = selectedfont;
}

It costs very little to store a reference, and you can access the reference directly.

EDIT: By changing the reference to being a Control, you are able to reference many different types of controls without having to typecast around. The reason I do this bit of code:

Control ctrl = sender as Control;
if(ctrl != null)
        SelectedControl = ctrl;

is it's a safer typecast. If the sender, for some reason, doesn't inherit from Control, then it won't be typecast and label will be null, but an exception won't be raised.

You could have a single click event handler for all of the controls that you want to be able to be changed via being selected. This will work much easier if you are creating the controls in code and hooking up the events manually.

You may want to look into doing some border or effect to show which control has been selected. This could be performed by doing this:

if(ctrl != null)
{
    Deselect(SelectedControl); //Deselects the old control
    SelectedControl = ctrl;
    Select(SelectedControl); //Selects the new control
}

where Deselect and Select do some fancy effects or border.

Hope this helps.

Alastair Pitts
thank you heaps AP Erebus :D that was very helpful
baeltazor
i do have one question though.... because there are many different types of controls... how do i know 'which' one was clicked by using your way?
baeltazor
I'm not sure what your asking. If you have only Label's click event being handled by label_Click then you won't have an issue with other types of controls being set as the SelectedLabel.Or are you looking for a way to somehow show the user which label was clicked?
Alastair Pitts
no no, the user will be adding more than one control onto the canvas. like a label, a few linklabels, a button etc. and when ONE of them gets clicked, i need to be able to change the font for that particular control that was clicked
baeltazor
Ah, helps if I read the original q properly. To make this change, then instead of storing a Label as the SelectedLabel, change it to a Control, which is the base class of all controls.I've updated my answer.
Alastair Pitts