Results 1 to 11 of 11

Thread: Tough one - Drag n Drop / Floating forms

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Question

    I reckon this is a tough one.

    I have a text editor with a rich textbox control on it, I would like to have a list of values, possibly on a kind of floating form which I could display and turn off easily, this floating form would have perhaps a list box on it with maybe 100 text entries, and I would be able to drag one of these entries from the list box into a specified location on the rich text box.

    Could someone tell me if this might be possible, I would love to hear from someone who has done something like this before or might have an idea how I could go about it.
    Thanks.

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    The Drag & Drop is easy. Change the ListBox on the Floating form's OLEDragMode to 1 - Automatic. Then, in the RichTextBox's OLEDragDrop Event, put the following code:
    Code:
        RichTextBox1.SelText = Data.GetData
    This code puts the ListBox's Selected text on the RichTextBox

    In here you could call A procedure or something to Put something else instead

    Hope this helps

    r0ach™
    Don't forget to rate the post

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Thumbs up

    Thanks, that works nicely, with one small problem, I use sendkeys to put the rtfbox into insert mode and sure enough if I type stuff in it gets inserted, however drag and drop from the listbox, displaces all the text after it. Have you any ideas how to stop this, or even how I can enforce insert mode.

  4. #4
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Unhappy

    Nope. Sorry. I've never used SendKeys before, (or Insert Mode)

    r0ach™
    Don't forget to rate the post

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    If you're using Insert mode the way windows does where the user's typing overwrites the next character, you can use the following instead of SendKeys:
    Code:
    Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
    Dim sPortnBefNewChar As String
    Dim sPortnAftNewChar As String
    
        '--> Whatever Condition you're using to determine this
        'If In Insert Mode
            With RichTextBox1
                sPortnBefNewChar = Left(.Text, .SelStart)
                sPortnAftNewChar = Mid(.Text, Len(sPortnBefNewChar) + 2)
                
                .Text = sPortnBefNewChar & Chr(KeyAscii) & sPortnAftNewChar
                KeyAscii = 0
                .SelStart = Len(sPortnBefNewChar) + 1
            End With
        'End If In Insert Mode
    End Sub
    With this in place, the drag 'n drop from the listbox worked for me and nothing was displaced later.
    Wade

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Question

    Thanks for the code, unfortunately, I can't get that to work. I am emulating insert mode on the keypress event using:

    If KeyAscii <> &H8 Then
    RichTextBox1.SelLength = 1
    End If

    When I also try to put your code in just after this, the drag and drop still displaces the text in the rtf.
    I am dragging from a listbox on a totally seperate form.


    I'm still stuck on this one.

  7. #7
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    The following allows you to emulate insert mode based on whether the user hits Insert on the keyboard. When you drag and drop, it drops the character from the listbox to the position in the richtextbox to which you drag it -- everything before the dropped position stays in place, everything after gets pushed back by one. Do you want the text being dropped to replace any of what it's being dropped onto?
    Code:
    'In Form 1
    Option Explicit
    Private vbInsrt As Boolean
    
    Private Sub Command1_Click()
        'Show form with listbox
        Form2.Show
    End Sub
    
    Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
        'Indicate if richtextbox will use Insert Mode    
        If KeyCode = 45 Then
            vbInsrt = True
        Else
            vbInsrt = False
        End If
    End Sub
    
    Private Sub RichTextBox1_KeyPress(KeyAscii As Integer)
    Dim sPortnBefNewChar As String
    Dim sPortnAftNewChar As String
    
            
        'If In Insert Mode, Concatenate everything before the cursor 
        '+ the new character typed
        '+ everything 2 positions after the cursor 
        'replace entire text with this concatenated string
        If vbInsrt Then
            With RichTextBox1
                sPortnBefNewChar = Left(.Text, .SelStart)
                sPortnAftNewChar = Mid(.Text, Len(sPortnBefNewChar) + 2)
                
                .Text = sPortnBefNewChar & Chr(KeyAscii) & sPortnAftNewChar
                KeyAscii = 0
                .SelStart = Len(sPortnBefNewChar) + 1
            End With
        End If
    End Sub
    
    'In Form2
    Private Sub Form_Load()
    Dim bCntr As Byte
    
        'Populate listbox and set its dragmode to automatically drag
        With List1
            For bCntr = 1 To 25
                .AddItem bCntr
            Next bCntr
            .OLEDragMode = vbAutomatic
        End With
    End Sub
    [Edited by WadeD on 08-17-2000 at 10:17 AM]
    Wade

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39

    Thumbs up

    Thanks Wade, thats some nice code, actually I think I was being a little unclear, by insert mode, I mean that the stuff I drop in from the listbox overwrites any text its dropped over. The application requires that a user can edit a file but cannot alter the number of lines, characters etc.... as we later have to go a retrieve values from specific parts of the file. Its quite a mess.

  9. #9
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    Just to be clear -- the user can't type directly in, but they can drop in additional values from a listbox and any values already in the richtextbox shouldn't be able to be deleted or overwritten, correct?
    Wade

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    39
    Sorry it always takes me ages to get back to you, I think we are in different time zones (I'm in Ireland).

    Actually the file is a huge list of data values of which we need to flag certain values by typing predefined text next to them. Values which are not predefined may on occasion pop up, so we need the facility to type into the box also, but there are perhaps 100 values we know are going to be there and they are the ones in the listbox. There is a lot of garbage in the file.

    It might look a little like this

    XPos -34678.00 ypos 6677886.66 Test Run1 Val 79994933.00

    We might need the ypos value so we put in the flag $YP01 after the value like this.......

    XPos -34678.00 ypos 6677886.66$YP01t Run1 Val 79994933.00

    As you can see it overwrites some of the garbage there, they know they will always want $YP01, but may come up with another one in the future (which we find with the $ symbol), so what I need is the drag and drop and text editing to not insert extra lines or move stuff over on a line, but to always overwrite whats in the box. At the moment I can do this wit typed text but not stuff dragged and dropped.

  11. #11
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    You could probably automate the replacement, but that's a very different alternative.
    For what you're doing, there's an easy way to replace the user's selection with what's dropped onto the richtextbox:
    Code:
    Private Sub Form_Load()
        Richtextbox1.OLEDropMode = rtfOLEDropManual 
    End Sub
    
    Private Sub RichTextBox1_OLEDragDrop(Data As RichTextLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
        'If Data is Text, Replace Current Selection with text being dropped
        If Data.GetData(1) = True Then
            RichTextBox1.SelRTF = Data.GetData(1)
        End If
    End Sub
    [Edited by WadeD on 08-18-2000 at 10:43 AM]
    Wade

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