Results 1 to 10 of 10

Thread: help with vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    help with vb.net

    here is my dilemma i have a text box, a command button that when activated goes through a series of "if then" traps persay that give and individual message if that trap is set off persay. the one i am concerned with is when i put in sets of numbers that look like this: 123-456-1234, numbers such as phone numbers or numbers with this form along with 2 hyphens. when i put a number like this in the text box a msgbox should pop up with whatever message i give it. the problem is that i dont know how to do that and i need help with it. any help will be appreciated. all i need to do is to be able to put a number like this:123-456-1234 and display a message. thats all. again any help would be appreciated.
    sincerely,
    Charlie
    my email is howard2@excite.com so if anyone wants to send there please go ahead.

  2. #2
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: help with vb.net

    Use this:

    VB Code:
    1. Format(Textbox.Text,"000-000-000").

    So that whenever a user types any number, it will always be in that format.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: help with vb.net

    so where exactly in the code do i put it. i know to put it under the click event but there are other "if then statements" and do i ned to put the code in a certain place? thanks by the way for the help

  4. #4
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: help with vb.net

    Show your code and I will show you where to put it.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,452

    Re: help with vb.net

    So you're saying that you want to detect when the user enters a number in that format, as opposed to forcing them to enter a number in that format? If so then you should be using regular expressions through the Regex class.

  6. #6
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: help with vb.net

    Quote Originally Posted by jmcilhinney
    So you're saying that you want to detect when the user enters a number in that format, as opposed to forcing them to enter a number in that format? If so then you should be using regular expressions through the Regex class.


    I don't know that.

    That's why I love VB Forums.


  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,452

    Re: help with vb.net

    Sorry. I wrote that quickly and didn't have time to post more details. Regular expressions is a big subject all of its own. If you want to use them regularly ( ) then you should do some reading on the subject.

    http://www.regular-expressions.info/
    http://gnosis.cx/publish/programming...pressions.html

    There are more sites on the Net that would be useful too. For your immediate issue you can use something like this:
    VB Code:
    1. If System.Text.RegularExpressions.Regex.IsMatch(Me.TextBox1.Text, "^\d{3}-\d{3}-\d{4}$") Then
    2.             MessageBox.Show("The text is in the format '###-###-####'.")
    3.         End If
    Here's a quick breakdown of that pattern:

    ^ matches the start of the string.
    \d matches any digit.
    {3} indicates the preceding pattern should be matched exactly three times.
    - matches a literal dash.
    $ matches the end of the string.

    So that pattern matches any string that begins with three digits, then a dash, then three digits, then a dash and then ends with four digits. You could remove the numbers in braces and use "^\d\d\d-\d\d\d-\d\d\d\d$" instead.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: help with vb.net

    here is my code hope it can be helped:

    Public Class Form1

    Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents txtnbr As System.Windows.Forms.TextBox
    Friend WithEvents cmdbtn As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.txtnbr = New System.Windows.Forms.TextBox
    Me.cmdbtn = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'txtnbr
    '
    Me.txtnbr.Location = New System.Drawing.Point(112, 48)
    Me.txtnbr.Name = "txtnbr"
    Me.txtnbr.TabIndex = 0
    Me.txtnbr.Text = ""
    '
    'cmdbtn
    '
    Me.cmdbtn.Location = New System.Drawing.Point(120, 88)
    Me.cmdbtn.Name = "cmdbtn"
    Me.cmdbtn.TabIndex = 1
    Me.cmdbtn.Text = "cpt"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.cmdbtn)
    Me.Controls.Add(Me.txtnbr)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    #End Region

    Private Sub cmdbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbtn.Click
    Dim strphon As String
    Dim errtext As String
    Dim p As String

    errtext = ""
    strphon = txtnbr.Text


    If Len(strphon) <> 12 Then
    errtext = "please enter a twelve digit phone number, including dashes"
    End If

    p = String.Format(txtnbr.Text, "000-000-0000")










    'Dim regex As String
    ' Dim Expression As Regex =("\d{3}-\d{3}-\d{4}"),RegexOptions.Compiled)
    ' Dim input As String
    ' Dim result As String = regex.Replace(input, ("\d{3}-\d{3}-\d{4}")
    'Option Strict Off
    'Option Explicit On


    'Namespace Regulator

    ' Public Class RegularExpression

    ' Private Sub Test()
    ' Dim regex As String = ""
    ' Dim options As System.Text.RegularExpressions.RegexOptions = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace Or System.Text.RegularExpressions.RegexOptions.Multiline) _
    ' Or System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    ' Dim reg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regex, options)
    ' End Sub
    'End Class
    'End Namespace
    'digits: [some digit]
    'Dim charset As Double

    'digits: charset("0123456789")

    'If InStr(strphon, "???-??-????") = 0 Then
    'errtext = "please enter dashes after the appropriate prefixes"
    'ElseIf InStr(strphon, "\d{3}-\d{3}-\d{4}") = 0 Then
    'errtext = "you have entered the right combination"
    'End If

    txtnbr.Text = InStr(strphon, "-")


    If IsNumeric(strphon) = False Then
    errtext = "please enter only numerical values"
    End If

    'Do
    'InStr(strphon, "\d{3}-\d{3}-\d{4}")
    'errtext = "you have entered the right combination"
    'Loop Until txtnbr.Text = "\d{3}-\d{3}-\d{4}"








    If errtext <> "" Then
    MsgBox(errtext, MsgBoxStyle.OKOnly, "error")
    End If



    End Sub
    End Class

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,452

    Re: help with vb.net

    1. Please use the appropriate tags to make your code more readable.

    [vbcode]Your code here.[/Highlight]

    2. Why have you posted all that code that is commented out? It can serve only to confuse.

    3. If the user has to enter a value in that format then you should just use a masked field. Please specify what version you're using in future. .NET 2.0 has the MaskedTextBox. In previous versions you will have to use the ActiveX MaskedEditControl. Using either of those you can specify a mask so that the user can't enter text in any other format and the only thing you have to test for is that they have entered the required number of characters. The .NET MaskedTextBox has properties specifically for that, although I'm not sure about the ActiveX version.

  10. #10
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: help with vb.net

    Private Sub cmdbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbtn.Click
    Dim strphon As String
    Dim errtext As String
    Dim p As String

    errtext = ""
    strphon = txtnbr.Text


    If Len(strphon) <> 12 Then
    errtext = "please enter a twelve digit phone number, including dashes"
    End If

    p = String.Format(txtnbr.Text, "000-000-0000")

    txtnbr.Text = InStr(strphon, "-")


    If IsNumeric(strphon) = False Then
    errtext = "please enter only numerical values"
    End If

    'Do
    'InStr(strphon, "\d{3}-\d{3}-\d{4}")
    'errtext = "you have entered the right combination"
    'Loop Until txtnbr.Text = "\d{3}-\d{3}-\d{4}"








    If errtext <> "" Then
    MsgBox(errtext, MsgBoxStyle.OKOnly, "error")
    End If



    End Sub
    End Class[/QUOTE]

    Well I see that you did your own code and decided that you want to use the format command.

    We'll do this in steps first so that I can get your desired code.

    First I assume you want to display first in the messagebox if the user typed in the correct format.

    Here's the code:

    VB Code:
    1. If Len(strphon) <> 12 Then
    2.             errtext = "please enter a twelve digit phone number, including    
    3.                           dashes"
    4.      Else
    5.          msgbox txtnbr.text
    6.      End If

    I hope this is what you are looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width