Converting control type in a single function

NachitoMax 411 Reputation points
2021-09-24T17:58:57.457+00:00

Hi

Rather than have a collection of functions to process data based on the control type that is passed to it, im wondering if its at all possible to convert an object to a specific control so that i can initial a Dim as the control type? Might be better for an example.. Lets say i have a range of controls that will all require the use of a function, these controls will be-

TextBox, Label, ComboBox, DataGrid, ListBox.

Each control type shares some common properties but not all. Here is an example of that id like to construct, its obviously not valid because its what im trying to achieve-

Function ProcessData(ByVal _control As Control) As String

Dim _C as Control = Nothing

Select Case _control.GetType()
   Case GetType(TextBox)
      _C = CType(_control, TextBox)

   Case GetType(Label)
      _C = CType(_control, Label)

   Case GetType(ComboBox)
      _C = Ctype(_control.ComboBox)

Return _C.Text

End Function

So the result i am after is no matter what control is passed to the function, i can assign the controls type to _C so that i can access the control Type properties relative to the Control being passed. If a TextBox is passed then _C As TextBox, if a ComboBox is passed then _C As ComboBox etc etc.

Is this achievable?

Thanks

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
{count} votes

3 answers

Sort by: Most helpful
  1. NachitoMax 411 Reputation points
    2021-09-27T14:04:27.377+00:00

    Hi

    Its not valid from the persepective of the control conversion being a true type conversion. for example:

    Label, Textbox & ComboBox controls share mostly the same properties but a ComboBox has additional properties relating to its DropDown. By using the code example i provided, I can successfully convert _C control to a ComboBox but the specific ComboBox properties are not available because the the original Dim is set as Control. I was hoping that there would be a way to specifically set the Control as a type so that it would become its intended control type like _C.Type = Combobox or something like that.

    I am using the code that i provided but can only access properties that are shared between the control types and cannot access anything that is control specific

    Thanks


  2. NachitoMax 411 Reputation points
    2021-09-28T14:39:06.117+00:00

    Hi

    They are not special properties, they are control based properties. I dont think what i was looking to do was achievable which was to convert the passing control to its native control type instead of assign a type to it.

    I have create a range of separate functions now in a class that operate in the same way but specifically for controls-

    Public Class Validate
    
    Public Sub New()
    End Sub
    
    Friend Function ValidateText(ByVal C As TextBox) As String
       'get my control specific data here
    Return  ProcessData(NewVal, OldVal)
    End Function
    
    Friend Function ValidateCombo(ByVal C As ComboBox) As String
       'get my control specific data here
    Return  ProcessData(NewVal, OldVal) 
    End Function
    
    Friend Function ValidateLabel(ByVal C As Label) As String
      'get my control specific data here
    Return  ProcessData(NewVal, OldVal) 
    End Function
    
    Private Function ProcessData(ByVal Newvalue As String, OldValue As String) As String
       'Do the data validation here
       Return result
    End Function
    
    End Class
    

    At least this way, in my controls i can simply add the call like this

    Private _VC As New ValidateClass
    
    Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
       _VC.ValidateCombo(comboBox1)
    End Sub
    

    or

    Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
        TextBox1.Text = _VC.ValidateCombo(comboBox1)
    End Sub
    
    0 comments No comments

  3. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-09-29T02:51:09.427+00:00

    Hi @NachitoMax ,
    You can take a look at the following code.

    Public Class Validate  
      
        Public Sub New()  
        End Sub  
      
        Friend Function Validate(ByVal _control As Control) As String  
            If TypeOf _control Is TextBox Then  
                '...  
            ElseIf TypeOf _control Is Label Then  
                '...  
            ElseIf TypeOf _control Is ComboBox Then  
                Dim prop As PropertyInfo = _control.GetType.GetProperty("DropDownStyle")  
                Dim v As ComboBoxStyle = CType(prop.GetValue(_control), ComboBoxStyle)  
                prop.SetValue(_control, ComboBoxStyle.DropDownList)  
            End If  
            '...  
        End Function  
      
    End Class  
    

    It can help you get and set the value of the property dynamically according to the type of control at run time
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments