Programming issue when applying text to a third party control
I have used some 3rd party controls in my windows app.
There is a snippet that is used in our code that reinitializes the entire .text property of all controls on the form.
Everything works fine except for the control. This control is similar to the Windows panel, except that it looks like a drop-down list. This control has a .Caption property instead of an associated .Text property.
This causes a problem when I use codes like this
foreach (Control oControl in this.Controls)
{
if (oControl is DropDownPanel)
{
{
oControl.Text = rm_ResourceManager.GetString(oControl.Name + ".Text");
}
}
}
The text is not set for the DropDownPanel control in the above manner. Because .Text is not available for the DropDownPanel control.
I cannot do the following:
((DropDownPanel)oControl).Caption = rm_ResourceManager.GetString(oControl.Name + ".Text");
Because this should throw an exception if I try to use an oControl named DropDownPanel
Any ideas how I can overcome such a condition.
Hello
a source to share
Is this a Telerik control? Its DropDownPanel class does not inherit from Control and cannot be added to the Controls collection. This explains why the signature is not installed and why you cannot tell the difference.
Check out the API documentation, there must be some other collection class that allows you to iterate over the RadElements that are present on the form. The best place to find other programmers who have used this product is in the support forum for it.
a source to share
By 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 the DropDownPanels and the Text property on the TextBoxes. If you need to do this or any other type of control, you need to add additional / if / else blocks, but I would not recommend it.
I would suggest reconsidering the approach. You may need a list of controls that need cleaned-up text, or you can use some other template, but we are unable to report with the limited information you provided.
a source to share
A more oo solution would be to use an adapter around the DropDownPanel. This adapter implements the entire management interface, redirecting it to the DropDownPanel, except for the property Text
, which is implemented in terms of the adapttee property Caption
.
Then you have to wrap the DropDownPanel in an adapter when building your gui.
This way you can handle the controls anyway, keeping your code cleaner and your linking below: it is the gui-buider's responsibility to ensure an equal interface for each component, and the responsibility foreach
to do something with all components.
a source to share