Results 1 to 15 of 15

Thread: Possible to count selection / highlighted text

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Exclamation Possible to count selection / highlighted text

    Anyone have any idea on how to COUNT how many "words" (Not character count) are in a selection? I want to be about to have a paragraph in a text field and highlight part of it and get an automatic count displayed in a label but just highlighting it. I don't want to have any buttons to push for this action..

    This is what I have right now that counts the full amount of words in a text box (Have to push button to have that displayed)
    I want to keep this in because I still want a button to calculate how many words are total in a text box.

    Code:
            Dim strInput As String
            strInput = RichTextBox1.Text
            Dim strSplit() As String
            strSplit = strInput.Split(CChar(" "))
            Label7.Text = strSplit.Length

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    Run that code in the textbox's MouseUp event handler. Then rather than using the entire text, set strInput to the textbox's Selection property
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    Not sure if I know where to start with that or not..

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    Create the MouseUp event handler by selecting the MouseUp declaration in the IDE....
    Name:  Untitled.jpg
Views: 310
Size:  16.9 KB

    vb Code:
    1. Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
    2.         'put the code here
    3. End Sub
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    Is this what yours saying I need to do? If so its not quite working but feels close. it is updating as words are typed but how do I get it so when I paste a paragraph in and highlight half it gives me a count on that..?

    Code:
        Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
            Dim strInput As String
            strInput = RichTextBox1.Text
            Dim strSplit() As String
            strSplit = strInput.Split(CChar(" "))
            Label7.Text = strSplit.Length
    
            Dim t As Integer = CStr((strSplit.Length / TextBox1.Text)) ' assuming the value represent seconds...
            Dim ts As New TimeSpan(0, 0, t)
            Label5.Text = (ts.ToString)
        End Sub

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    That code will run right after you complete the highlight sequence (i.e. when you MouseUp). If label7 is updating while you type, then you are setting it elsewhere in your code. Not sure we the timeSpan bit is there. You should go back and look at my comments in your other thread
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    so what exactly is the highlight sequence? did I miss that?

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    Quote Originally Posted by bdenn View Post
    so what exactly is the highlight sequence? did I miss that?
    MouseDown in the text
    MouseDrag to highlight the text
    MouseUp to complete the highlight sequence
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    I must be becoming an annoyance by now..

    So each one of those needs to be RichTextBox1.Mouse Down and so on?

    This sequence is what tells the program that what is highlighted will be counted and them displayed into the label as a number value?

  10. #10
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    No. It's all good.

    The highlight sequence is what I am referring to as the user sequence of events to highlight the text. All you should be dealing with is the selected text after it has been selected. So you should be be running your code in the MouseUp event handler because the text will only be highlighted after the mouseUp event occurs. Once it does occur, you can get the highlighted text using the textbox's Selection property.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    ok. so as of right now what is happening is I paste in the paragraph and when I click in the RichTextBox1 it updates the label. What code do I need to write so that it reads only what is highlighted? I'm assuming that I can do both actions here one being I highlight one section and it tells me the length and another that tells me the length of everything in RichTextBox1

  12. #12
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    this does what you want.
    vb Code:
    1. Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
    2.  
    3.         If Me.RichTextBox1.SelectedText.Length > 0 Then
    4.             Dim strInput As String = RichTextBox1.SelectedText.Trim
    5.             Dim strSplit() As String = strInput.Split(" "c)
    6.             Label1.Text = strSplit.Count & " words"
    7.         End If
    8.  
    9.     End Sub
    Alternatively, you could put the code in the MouseMove event and check for the left mouse button and update the label dynamically as the user drags the mouse
    vb Code:
    1. Private Sub RichTextBox1_Mouse(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseMove
    2.  
    3.         If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.RichTextBox1.SelectedText.Length > 0 Then
    4.             Dim strInput As String = RichTextBox1.SelectedText.Trim
    5.             Dim strSplit() As String = strInput.Split(" "c)
    6.             Label1.Text = strSplit.Count & " words"
    7.         End If
    8.     End Sub
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    I got that working thanks! so it looks like the following code. Now how do I incorporate it converting to time (Code Below)

    Code:
        Private Sub RichTextBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles RichTextBox1.MouseUp
            If Me.RichTextBox1.SelectedText.Length > 0 Then
                Dim strInput As String = RichTextBox1.SelectedText.Trim
                Dim strSplit() As String = strInput.Split(" "c)
                Label9.Text = strSplit.Count
                Label10.Text = CStr((strSplit.Length / TextBox1.Text))
            End If
    
        End Sub
    Code:
            Dim t As Integer = CStr((strSplit.Length / TextBox1.Text)) ' assuming the value represent seconds...
            Dim ts As New TimeSpan(0, 0, t)
            Label5.Text = (ts.ToString)

  14. #14
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Possible to count selection / highlighted text

    you start by turning on Option Strict.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Possible to count selection / highlighted text

    Thanks so much for your help. It is all operational right now. I need to fix up the code though. Next step will be at some point to tell it when it sees the word VO or SOT or PKG it calculates everything under that. problem I would never know if VO or SOT or PKG comes first its always different.

Tags for this Thread

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