none
When you type the second word then show message ? RRS feed

  • Question

  • I want type First word and space and type second word or (more then two words) then show messagebox  in textbox use event (TextChanged)
    • Edited by srajmuneer Thursday, June 1, 2017 8:56 PM
    Thursday, June 1, 2017 8:17 PM

Answers

  • ...

    Option Strict On
    
    Public Class Form1
        Friend WithEvents TextBox1 As TextBox
        Private m_I As Integer = 0
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            If TextBox1.Focused AndAlso keyData = Keys.Space Then
                m_I += 1
    
                'dont return true here, since we want the space to appear in the textbox
            End If
    
            If m_I >= 2 Then
                MessageBox.Show("Second word finished")
    
                'remove this, if you want to let the character appear in the textbox
                Return True
            End If
    
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'usually done i  the Designer...
            Me.TextBox1 = New System.Windows.Forms.TextBox()
            Me.SuspendLayout()
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(34, 35)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(213, 20)
            Me.TextBox1.TabIndex = 0
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(284, 261)
            Me.Controls.Add(Me.TextBox1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
            Me.PerformLayout()
        End Sub
    End Class
    

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Friday, June 2, 2017 12:07 AM
    Thursday, June 1, 2017 10:38 PM
  • ... you want to let the typed characters appear???

    If so, remove the Return True statement as I said in the comment in the code...

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Friday, June 2, 2017 12:07 AM
    Thursday, June 1, 2017 11:14 PM

All replies

  • Hi

    override ProcessCommandKey and listen for the Space character. Increment a counter when space is pressed, if the counter reachers two, reset it and show the message.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey(v=vs.110).aspx

    Example eg here:

    https://stackoverflow.com/questions/10468200/key-events-processcmdkey

    Regards,

      Thorsten


    Thursday, June 1, 2017 8:20 PM
  • hi Thorsten Gudera , I use code but not work :

    Dim strOrig = TextBox1.Text If strOrig = strOrig.Split(" "c)(2) Then MessageBox.Show("complete 2 Words")

    'MessageBox or Do any think End If


    • Edited by srajmuneer Thursday, June 1, 2017 8:54 PM
    Thursday, June 1, 2017 8:53 PM
  • Hi,

    thats what I meant:

    Option Strict On
    
    Public Class Form1
        Friend WithEvents TextBox1 As TextBox
        Private m_I As Integer = 0
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            If TextBox1.Focused AndAlso keyData = Keys.Space Then
                m_I += 1
    
                If m_I = 2 Then
                    m_I = 0
                    MessageBox.Show("Second word finished")
                End If
    
                'dont return true here, since we want the space to appear in the textbox
            End If
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'usually done i  the Designer...
            Me.TextBox1 = New System.Windows.Forms.TextBox()
            Me.SuspendLayout()
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(34, 35)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(213, 20)
            Me.TextBox1.TabIndex = 0
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(284, 261)
            Me.Controls.Add(Me.TextBox1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
            Me.PerformLayout()
        End Sub
    End Class
    

    Regards,

      Thorsten

    Thursday, June 1, 2017 9:38 PM
  • thanks Thorsten Gudera your code very good thats what I meant ,

    but Can be repeated loop messagebox (complete 2 Words) With any addition (text) after Second word finished


    • Edited by srajmuneer Thursday, June 1, 2017 10:03 PM
    Thursday, June 1, 2017 10:00 PM
  • Perhaps something like the first example below is what you want to do.  This stops it no mater if the user types or pastes words with a space between them.  Using the key events will only work if the user types the space.  That is unless you disable the edit menu so the use can not paste text into the textbox as shown in the second example below.

    Public Class Form1
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Dim txt As String = TextBox1.Text
            If txt.Contains(" ") Then
                MessageBox.Show("You can only type or paste one word into this textbox with no spaces in it.")
                RemoveHandler TextBox1.TextAlignChanged, AddressOf TextBox1_TextChanged
                TextBox1.Text = txt.Remove(txt.IndexOf(" "))
                AddHandler TextBox1.TextAlignChanged, AddressOf TextBox1_TextChanged
                TextBox1.Select(TextBox1.TextLength, 0)
            End If
        End Sub
    
    End Class
    
     

     This would probably be the best method t use unless you need the user to be able to paste a word into the textbox.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.ShortcutsEnabled = False 'disables the right click edit menu and Ctrl+V so the user can not paste text into the textbox
        End Sub
    
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = " " Then
                MessageBox.Show("You can only type one word into this textbox with no spaces in it.")
                e.Handled = True
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    Thursday, June 1, 2017 10:08 PM

  • but Can be repeated loop messagebox (complete 2 Words) With any addition (text) after Second word finished

    Hi,

    what do you mean exactly with this? In my code, after each two space-presses the msgbox appears...

    Regards,

      Thorsten

    Thursday, June 1, 2017 10:16 PM
  • thanks IronRazerz , that code for your Thorsten Gudera thats what I meant , but I need repeated show  messagebox after Second word finished if any type text

     Private m_I As Integer = 0
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            If TextBox1.Focused AndAlso keyData = Keys.Space Then
                m_I += 1
    
                If m_I = 2 Then
                    m_I = 0
                    MessageBox.Show("Second word finished")
                End If
    
                'dont return true here, since we want the space to appear in the textbox
            End If
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    

    Thursday, June 1, 2017 10:19 PM
  •  No problem,  it was not clear to me what you wanted to happen exactly.  I thought you did not want the user to be able to type a second word at all from reading your original question.

    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, June 1, 2017 10:32 PM
    Thursday, June 1, 2017 10:31 PM
  • thanks Thorsten Gudera , I mean It will not stop showing messagebox after Second word finished but rather show the message with any text or space

    example :

    1. when type first word = nothing
    2. when type space = nothing
    3. when type second word = nothing ----- after this show messagebox ("Second word finished") (Your code has succeeded)
    4. when type Third word    = show messagebox ("Second word finished")
    5. when type space           = show messagebox ("Second word finished")
    6. when type Fourth word = show messagebox ("Second word finished") And so it continues  show messagebox ("Second word finished")




    • Edited by srajmuneer Thursday, June 1, 2017 10:35 PM
    Thursday, June 1, 2017 10:32 PM
  • thanks Thorsten Gudera , I mean It will not stop showing messagebox after Second word finished but rather show the message with any text or space

    example :

    1. when type first word = nothing
    2. when type space = nothing
    3. when type second word = nothing ----- after this show messagebox ("Second word finished") (Your code has succeeded)
    4. when type Third word    = show messagebox ("Second word finished")
    5. when type space           = show messagebox ("Second word finished")
    6. when type Fourth word = show messagebox ("Second word finished") And so it continues  show messagebox ("Second word finished")




    Ok, then do a check for any key and also check the counter. Code follows.

    Regards,

      Thorsten

    Thursday, June 1, 2017 10:36 PM
  • ...

    Option Strict On
    
    Public Class Form1
        Friend WithEvents TextBox1 As TextBox
        Private m_I As Integer = 0
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            If TextBox1.Focused AndAlso keyData = Keys.Space Then
                m_I += 1
    
                'dont return true here, since we want the space to appear in the textbox
            End If
    
            If m_I >= 2 Then
                MessageBox.Show("Second word finished")
    
                'remove this, if you want to let the character appear in the textbox
                Return True
            End If
    
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'usually done i  the Designer...
            Me.TextBox1 = New System.Windows.Forms.TextBox()
            Me.SuspendLayout()
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(34, 35)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(213, 20)
            Me.TextBox1.TabIndex = 0
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(284, 261)
            Me.Controls.Add(Me.TextBox1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
            Me.PerformLayout()
        End Sub
    End Class
    

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Friday, June 2, 2017 12:07 AM
    Thursday, June 1, 2017 10:38 PM
  • very good Thorsten Gudera when show message ("Second word finished")
     I want to continue typing while the message continues to appear with type each character can this ?



    • Edited by srajmuneer Thursday, June 1, 2017 10:51 PM
    Thursday, June 1, 2017 10:44 PM
  • ... you want to let the typed characters appear???

    If so, remove the Return True statement as I said in the comment in the code...

    Regards,

      Thorsten

    • Marked as answer by srajmuneer Friday, June 2, 2017 12:07 AM
    Thursday, June 1, 2017 11:14 PM
  • thank you very very much Thorsten Gudera for help
    Friday, June 2, 2017 12:07 AM