locked
Set Variable type to a textbox?

    Question

  • How do I set that a textbox should be ULong or Long or String?
    Friday, May 11, 2007 2:37 PM

Answers

  • Textboxes always contain strings,  however you can restrict it to allow certain characters to be added by putting some code in the keypress event.

    To enter only number characters into a textbox. The following example should work.

     

    Public Class Form1
        
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Char.IsNumber(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
                e.Handled = True
                MessageBox.Show("Only numeric text please!")
            End If
        End Sub
    End Class

     

    You would probably also need to put some logic in the validate the value within a numeric range.

     

    Or alternatively you could use a maskededit control which provides more control of values allowed to be entered.

     

     

    Friday, May 11, 2007 3:00 PM
  • The data ui customization that is shown in your pic is for customizing what type is used for a particular control when using databound controls from a datasource...so when you drag and drop a field from a datasource to a form it automatically useses that datatype by default...as SPotty stated thoug...the textbox ALWAYS contains a string
    Friday, May 11, 2007 3:26 PM

All replies

  • Textboxes always contain strings,  however you can restrict it to allow certain characters to be added by putting some code in the keypress event.

    To enter only number characters into a textbox. The following example should work.

     

    Public Class Form1
        
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Char.IsNumber(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
                e.Handled = True
                MessageBox.Show("Only numeric text please!")
            End If
        End Sub
    End Class

     

    You would probably also need to put some logic in the validate the value within a numeric range.

     

    Or alternatively you could use a maskededit control which provides more control of values allowed to be entered.

     

     

    Friday, May 11, 2007 3:00 PM
  •  spotty2428 wrote:

    Textboxes always contain strings, however you can restrict it to allow certain characters to be added by putting some code in the keypress event.

    To enter only number characters into a textbox. The following example should work.

    Public Class Form1
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Char.IsNumber(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
    e.Handled = True
    MessageBox.Show("Only numeric text please!")
    End If
    End Sub
    End Class

    You would probably also need to put some logic in the validate the value within a numeric range.

    Or alternatively you could use a maskededit control which provides more control of values allowed to be entered.



    Look at this!?

    http://www.stigern.net/images/variable.JPG
    Friday, May 11, 2007 3:12 PM
  • The data ui customization that is shown in your pic is for customizing what type is used for a particular control when using databound controls from a datasource...so when you drag and drop a field from a datasource to a form it automatically useses that datatype by default...as SPotty stated thoug...the textbox ALWAYS contains a string
    Friday, May 11, 2007 3:26 PM