tags:

views:

28

answers:

3

Hello

I have used some third party controls in my windows application.

There is a snippet which is being used in our code which re-initializes all the .text property of all the controls on the form.

Everything works fine except for a control. This control is similar to the Windows Panel except for it has a dropdown appearance. This control has .Caption property instead of .Text property associated to it.

This causes the problem whenever i use such codes

foreach (Control oControl in this.Controls)
{
    if (oControl is DropDownPanel)
    {
       {
          oControl.Text = rm_ResourceManager.GetString(oControl.Name + ".Text");
       }
    }
}

The text is not set here for the DropDownPanel control in the above method. Since .Text is not available for DropDownPanel control.

I cannot do the following either ..

((DropDownPanel)oControl).Caption = rm_ResourceManager.GetString(oControl.Name + ".Text");

Cos it shall throw exception if i try to cast oControl with that of DropDownPanel

Any ideas how can i overcome such a condition.

Regards

+2  A: 

Using the 'as' keyword, you can do something like this.

foreach (Control oControl in this.Controls) 
{ 
    DropDownPanel ddp = oControl as DropDownPanel;

    if (ddp != null) 
    { 
        ddp.Caption = rm_ResourceManager.GetString(oControl.Name + ".Text"); 
    }
    else
    {
        TextBox tb = oControl as TextBox;

        if (tb != null)
        {
            tb.Text = rm_ResourceManager.GetString(oControl.Name + ".Text"); 
        }
    }
} 

This ONLY sets the Caption property on DropDownPanels and the Text property on TextBoxes. If yu need to do thisor any other type of control you'll need to add further as/if/else blocks but I wouldn't recommend it.

I would suggest rethinking the approach. You may need an expicit list of controls that need their text cleared or you may be able to use a some other pattern but we can't tell with the limited information you've presented.

Daniel Renshaw
Should it not be ddp.Caption instead of oControl.Caption?
Jens Granlund
Yes, As i explained before oControl is of type Windows.Forms.Control and it does not have .Caption property.
srivatsa
@Jens - thanks, I've fixed that bug
Daniel Renshaw
But im not able to cast the object oControl (Windows.Form.Controls) to that of type DropDownPanel(Third party windows control.)I get compilation error if i use the 'as' keyword
srivatsa
'as' has been part of .NET since 1.0. It will try to cast the object as directed but return null if the cast fails. No runtime exception should occur. What is the compilation error number/message?
Daniel Renshaw
Hello DanielCannot convert type 'System.Windows.Forms.Control' to 'Telerik.WinControls.UI.RadPanelBarGroupElement' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
srivatsa
I don't think that compiler error is coming from the code you've pasted - it's a separate issue. Hans Passant has given an answer that addresses this other issue.
Daniel Renshaw
A: 

A more oo solution would be using an adapter around the DropDownPanel. This adapter would implement the whole Control interface by forwarding it to the adaptee DropDownPanel, except for the Text property, which is implemented in terms of the adaptee's Caption property.

You should then wrap the DropDownPanel into the adapter when instantiating your gui.

That way you can treat the controls all the same, keeping your code cleaner, and your coupling lower: it's re responsibility of the gui-buider to guarantee an equal interface for each component, and it's the responsibility of the foreach loop to do something to all components.

xtofl
+1  A: 

Is this a Telerik control? Its DropDownPanel class doesn't inherit from Control, it cannot be added to the Controls collection. Which explains why the caption doesn't get set and why you cannot cast.

Review the API documentation, there has to be some kind of other collection class that allows you to iterate RadElements that are present on the form. The best place to find other programmers that have used this product is in the support forum for it.

Hans Passant