开发者

Programming issue while applying text to the third party control

开发者 https://www.devze.com 2022-12-28 05:26 出处:网络
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 .textproperty of all the controls on the form.

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号