Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

TextBox control can not be converted to value system.windows.controls.button

Hi everyone,

I have the below logic for increment and decrement button in vb.net.

 Private Sub IncrementBox(ByVal sender As TextBox)
        Dim value As Integer

        If Integer.TryParse(sender.Text, value) Then
            sender.Text = (value + 1).ToString()
        End If
    End Sub

Open in new window


And I am calling the functions on the event handler something like that:

 Private Sub btnDown_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnDown.Click
        IncrementBox((DirectCast(sender, TextBox)))
    End Sub

Open in new window


On the runtime it gives me an exception system.windows.textbox can not be converted to system.windows.button.

Any idea how to resolve it Please?

Thanks
0
developer2012
Asked:
developer2012
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The sender parameter for your btnDown_Click() method will be the BUTTON itself, so you can't cast that to a TextBox and send it to your helper method.

You'll need to hard-code the TextBox into the method.  The example below is using "TextBox1":
    Private Sub btnDown_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnDown.Click
        IncrementBox(Me.TextBox1)
    End Sub

Open in new window

0
 
Julian HansenCommented:
If you want to do this dynamically for more than one button you could try the following

Private Sub btnDown_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnDown.Click
        IncrementBox(Me.Controls.Find("textboxname", true))
    End Sub

Open in new window

Where textboxname is substituted for a name made up of a combination of the button clicked name and a constant so you relate button to text box

Button = button1
TextBox = button1_text
0
 
developer2012Author Commented:
Thanks =))
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now