question

nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 asked XingyuZhao-MSFT answered

Converting control type in a single function

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

dotnet-standard
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @nachoshaw-9496

its obviously not valid because its what im trying to achieve-

Actually, the code works for me.

     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)
         End Select
         Return _C.Text
     End Function

Could you provide more details of why it is not valid?

0 Votes 0 ·
nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered XingyuZhao-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @nachoshaw-9496 ,

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

Each control has it's specific properties. Could you provide more information about how "ProcessData" method handles these special properties?
We are waiting for your update.


0 Votes 0 ·
nachoshaw-9496 avatar image
0 Votes"
nachoshaw-9496 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @nachoshaw-9496 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.