vb6 Bahrain
  • 14 years ago

    in my project i have a function (translate()) it can translate a words frome notepade file to other notepade file, then i want to take the words in the second notepade to replace it with the words in popmenu.
    (The popmenu appear when we click right click on the word we have it in RichTextBox)

    please, help me if any body have an idea?



  • 14 years ago


    You will get a better response if you take time to describe exactly what it is you are trying to achieve.  You should give details of your problem, any ideas you have had, any research you have done and any code you have written, without this background information it is very hard for anyone to give you a useful answer!!!  There are lots of Free Online Translators which you can use to help you formulate your English.

    What exactly are you trying to do?  Do you want something to appear when you click the Right Mouse Button?

    If so, you can detect which mouse button has been pressed in the Mouse_Down event of the RichTextBox:

    Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
       If Button = vbLeftButton Then
          MsgBox "left"
       ElseIf Button = vbRightButton Then
          MsgBox "right"
       ElseIf Button = vbMiddleButton Then
          MsgBox "middle"
       End If
    End Sub

    You could maybe add a new Form to your project and make it visible when the right mouse button is pressed within the bounds of the RTB, find out the word at the position of the right-click and display a list of words in the new form.  Kind of like the Spell Checker window in MS Word.

    Firstly, declare an array which can be accessed by every form in your project:

    Add a Module to your project - Project -> Add Module -> New Module -> Open
    Declare an array in this module:
    Dim strSuggestions() As String

    Then add a new form to your project (Project -> Add Form -> New Form -> Open), this will act as your pop-up menu so resize it accordingly and set its StartUpPos property to CentreOwner.  Add a textbox to this form and set its multiline property to True.

    You can now show this form when the user right-clicks within the bounds of your RichTextBox using the following code:

    Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = vbRightButton Then
        Form2.Show vbModal
    End If
    End Sub

    You must now ascertain what word is underneath the mouse pointer at the position the user has right-clicked, the full code, including the RightClick Event Handler looks like this:

    Private Const EM_CHARFROMPOS& = &HD7
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
       Dim txt As String

       'Get the word under the mouse.
       txt = RichWordOver(RichTextBox1, X, Y)
       Form2.Text1.Text = txt
       Form2.Show vbModal
    End If
    End Sub
    Public Function RichWordOver(rch As RichTextBox, X As _
        Single, Y As Single) As String
    Dim pt As POINTAPI
    Dim pos As Integer
    Dim start_pos As Integer
    Dim end_pos As Integer
    Dim ch As String
    Dim txt As String
    Dim txtlen As Integer
    ' Convert the position to pixels.
    pt.X = X \ Screen.TwipsPerPixelX
    pt.Y = Y \ Screen.TwipsPerPixelY
    'Get the character number
    pos = SendMessage(rch.hWnd, EM_CHARFROMPOS, 0&, pt)
    If pos <= 0 Then Exit Function
          ' Find the start of the word.
           txt = rch.Text
           For start_pos = pos To 1 Step -1
               ' See if this is a word separator.
               ch = Mid$(rch.Text, start_pos, 1)
               If IsWordSeparator(ch) Then Exit For
           Next start_pos
           start_pos = start_pos + 1
           'Find the end of the word.
           txtlen = Len(txt)
           For end_pos = pos To txtlen
               ' See if this is a word separator.
               ch = Mid$(txt, end_pos, 1)
               If IsWordSeparator(ch) Then Exit For
           Next end_pos
           end_pos = end_pos - 1
           If start_pos <= end_pos Then RichWordOver = Mid$(txt, start_pos, end_pos - _
                start_pos + 1)
    End Function

    Private Function IsWordSeparator(ByVal ch As String) As _
        IsWordSeparator = (ch <= " " Or ch > "~")
    End Function

    At this point you are able to trap a right clcik in your richtextbox, open your new form and pass the word that the user clicked on as a parameter to that form.

    All ou have to do now is to use the code I gave you before to search through your Text file until you find this word, and display the contents of this file in the textbox on the pop-up form.  You can then use similar logic to trap the click event in the new form and copy the selected word back into the richtextbox at the current location.  I wont write this bit for you, you will learn more if you do it yourself!!

    Hope this helps!!!

  • 14 years ago

    Hi Rob...

    Thanks a lot for replay..



Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks