Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat control as either ComboBox or TextBox

Tags:

c#

.net

generics

What is the best way to solve the following problem?

foreach (Control control in this.Controls)
{
    if (control is ComboBox || control is TextBox)
    {
        ComboBox controlCombobox = control as ComboBox;
        TextBox controlTextbox = control as TextBox;

        AutoCompleteMode value = AutoCompleteMode.None;

        if (controlCombobox != null)
        {
            value = controlCombobox.AutoCompleteMode;
        }
        else if (controlTextbox != null)
        {
            value = controlTextbox.AutoCompleteMode;
        }

        // ...
    }
}

You see it's complicated enough to get the AutoCompleteMode-property. You can assume that it is guaranteed that I have either a ComboBox or a TextBox.

My first idea was to use generic with multiple types for T, but it seems that this is not possible in .NET:

public string GetAutoCompleteModeProperty<T>(T control) where T: ComboBox, TextBox // this does not work, of course

Sadly the both Controls don't have a common base class.

Note: This is meant to be a more general question used with a minimized example. In my case, I also want to access / manipulate other the AutoComplete*-proprties (which also both Controls have in common).

Thanks for ideas!

like image 245
The Wavelength Avatar asked Apr 30 '13 07:04

The Wavelength


People also ask

What is the function of combo box?

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down.

What is list box and combo box?

Generally, a combo box is appropriate when there is a list of suggested choices, and a list box is appropriate when you want to limit input to what is on the list. A combo box contains a text box field, so choices not on the list can be typed in. The exception is when the DropDownStyle property is set to DropDownList.


2 Answers

dynamic currentControl =  control;
string text = currentControl.WhatEver;

But, it throws an exception (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) if currentControl doesn't have WhatEver property

like image 138
Bogdan M. Avatar answered Nov 15 '22 18:11

Bogdan M.


Use Type.GetType(). You just have to input a string representation of your property in.

if (sender is ComboBox || sender is TextBox)
{
  var type = Type.GetType(sender.GetType().AssemblyQualifiedName, false, true);
  var textValue = type.GetProperty("Text").GetValue(sender, null);
}

This also allows you to set the value of your properties too.

type.GetProperty("Text").SetValue(sender, "This is a test", null);

You could move this to a helper method to save rewriting code.

public void SetProperty(Type t, object sender, string property, object value)
{
  t.GetProperty(property).SetValue(sender, value, null);
}
public object GetPropertyValue(Type t, object sender, string property)
{
  t.GetProperty(property).GetValue(sender, null);
}

There is also room for exception handling by using this method.

var property = t.GetProperty("AutoCompleteMode");
if (property == null)
{
  //Do whatever you need to do
}
like image 33
LukeHennerley Avatar answered Nov 15 '22 16:11

LukeHennerley