Richtextbox and images

Hello...you gotta read this carefully to understand:)

When I paste an Image from the clipboard I want to add somekind of Identifyer to it, like an index.
I dont want to use the Richtextbox1.oleobjects.add , , ,
I want it directly from the clipboard.

All pasted Images are countable using the Richtextbox.oleobjects.count method.
But as I said, I want to be able to know all the pasted Images Item properties. So that I can use the Richtextbox.oleobjects.item(?) command.

Once again, I know how to add an object and that I can use the .item method with them. But I need to be able to use it with pasted images aswell..

Thanx! /Dan
neo24Asked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
The problem with your idea is that you still don't know when you've clicked on one of the images... BUT...HERE YOU GO!

This is a full example program.

1) Create a new project and add a RichTextBox and 2 command buttons to FORM1.

2) Add the following code to the DECLARATIONS SECTION of FORM1:

'-------------------------------------------------------------------------------
    Private Sub Command1_Click()
        RTFaddObj
    End Sub
    Private Sub Command2_Click()
        RTFdelObj
    End Sub
    Private Sub Form_Load()
        'INITIALIZE THE RTF CLICK ROUTINES FOR RichTextBox1
        RTFInitClick RichTextBox1
        Command1.Caption = "Insert Obj"
        Command2.Caption = "Delete Obj"
    End Sub
    Private Sub RichTextBox1_Click()
        Dim iVal As Long
        Dim jVal As Long
       
        On Error Resume Next
        RTFobjClicked = -1
        iVal = UBound(RTFobjInfo)
        If Err > 0 Or RTFobject.SelLength <> 1 Then Exit Sub
        For jVal = 0 To iVal
            If RTFobjInfo(jVal) = RTFobject.SelStart Then
                RTFobjClicked = jVal
                Debug.Print "THIS IS " & CStr(RTFobjClicked)
            End If
        Next jVal
    End Sub
    Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
        Select Case KeyCode
            Case 86 And Shift = 2, 45 And Shift = 1 'CTL-V or SHIFT-INS Paste
                RTFaddObj
            Case 46 And Shift = 0 'DELETE Key
                RTFdelObj
            Case 8 And Shift = 0 'BACKSPACE Key
                RTFdelObj True
        End Select
    End Sub
'-------------------------------------------------------------------------------



3) Add a MODULE to your project and put the following code in the DECLARATIONS SECTION of the MODULE:

'-------------------------------------------------------------------------------
    Public RTFobjInfo() As Long
    Public RTFobjClicked As Long
    Public RTFobject As Object
    Public RTFSubRunning As Boolean
    Sub RTFInitClick(RichTextBoxObject As Object, Optional EndInit As Boolean)
        RTFobjClicked = -1
        RTFSubRunning = False
        If EndInit = False Then
            Set RTFobject = RichTextBoxObject
        Else
            Set RTFobject = Nothing
        End If
    End Sub
    Sub RTFaddObj()
        Dim iVal As Long
        Dim jVal As Long
       
        On Error Resume Next
        If RTFSubRunning = True Then Exit Sub
       
        iVal = UBound(RTFobjInfo)
        If Err = 0 Then
            iVal = iVal + 1
        Else
            ReDim RTFobjInfo(0) As Long
            iVal = 0
        End If
        ReDim Preserve RTFobjInfo(iVal) As Long
        RTFobjInfo(iVal) = RTFobject.SelStart
        For jVal = LBound(RTFobjInfo) To iVal - 1
            If RTFobjInfo(iVal) <= RTFobjInfo(jVal) Then
                RTFobjInfo(jVal) = RTFobjInfo(jVal) + 1
            End If
        Next jVal
        QNumSort RTFobjInfo
        RTFSubRunning = True
        RTFobject.SetFocus
        SendKeys "^v", True
        RTFSubRunning = False
    End Sub
    Sub RTFdelObj(Optional UseBS As Boolean)
        Dim iVal As Long
        Dim jVal As Long
       
        On Error Resume Next
        If RTFSubRunning = True Then Exit Sub
        fSwitch = False
        iVal = UBound(RTFobjInfo)
        If Err > 0 Or RTFobjClicked = -1 Then Exit Sub
        For jVal = RTFobjClicked To iVal - 1
            RTFobjInfo(jVal) = RTFobjInfo(jVal + 1) - 1
        Next jVal
        ReDim Preserve RTFobjInfo(iVal - 1) As Long
        RTFobject.SetFocus
        RTFSubRunning = True
        If UseBS = True Then
            SendKeys "{BS}", True
        Else
            SendKeys "{DEL}", True
        End If
        RTFSubRunning = False
        RTFobjClicked = -1
    End Sub
    Sub QNumSort(InArray As Variant)
        'THIS FUNCTION SORTS A NUMERIC ARRAY
        Dim lLoop1 As Long
        Dim lLoop2 As Long
        Dim tBuf As Variant
        For lLoop1 = UBound(InArray) To LBound(InArray) Step -1
            For lLoop2 = LBound(InArray) + 1 To lLoop1
                If InArray(lLoop2 - 1) > InArray(lLoop2) Then
                    tBuf = InArray(lLoop2 - 1)
                    InArray(lLoop2 - 1) = InArray(lLoop2)
                    InArray(lLoop2) = tBuf
                End If
            Next lLoop2
        Next lLoop1
    End Sub

'-------------------------------------------------------------------------------



4) Run the program.

5) Copy a picture onto the clipboard from another application.

6) Click the "Insert Obj" button to paste the image into the richtextbox... Do this a couple more times, so there about 2 or 3 pictures in the richtextbox.

7) Now, click on a picture and watch the debug window... When you click on a picture the messagebox in the RichTextbox CLICK EVENT will fire and display the index number of the image. This is the index number you wanted.

8) Click on a picture and then click the "Delete Obj" button.  The selected picture will go away, and the image indexes will be reordered.  Once you have deleted a picture, repeat step 7 to see that the correct numbers show up in the debug window.

This example should do everything you wanted...  I even included code so that the user can paste an image into the richtextbox by copying the image to the clipboard and pressing CTL-V in the richtextbox.

This was ALOT of work!  Hope this works for you...



Cheers!

0
 
mcriderCommented:
If I understand your question, you have an image on the clipboard and you want to paste it into a RichTextBox and have the RichTextBox.OleObjects.Item work...

Here's how:

Copy an image to the clipboard and the execute the following statements:

     Debug.Print RichTextBox1.OLEObjects.Count
     RichTextBox1.SetFocus
     SendKeys "^v"
     Debug.Print RichTextBox1.OLEObjects.Count


Notice that before the sendkeys the number of OLEObjects in the richtextbox goes to the DEBUG window and is 0... After the sendkeys, the count is 1.

Hope this helps!


Cheers!
0
 
neo24Author Commented:
hmm...
I want be able to get somekind of index when I click on the pasted image.
or just be able to use OLEObjects.Item.index(?).remove.
Not to confuse with the oleobjects.clear
event.

Thank you anyways:)

/Dan
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MarineCommented:
Can you place these paste images into an array they you can access it probably as you like it.
0
 
neo24Author Commented:
Show me how!
0
 
mcriderCommented:
I get what you're trying to do... You are trying to identify the graphic that you are clicking on in the RichText Control, YES???

The problem is, you can't do that... And here's why:

When you add an image to the RTF by either pasting or doing an .Add, the system assigns an Item under the OLEObjects property of the RichTextBox. Let's we add two images. They become:

    richtextbox1.OLEObjects.Item(0)

                -and-

    richtextbox1.OLEObjects.Item(1)

The reason you cant identify which one of these you have clicked on is becase OLEObjects only has the following properties:  Add, Clear, Count, Item, and Remove... There IS NO INDEX PROPERTY and the only event that fires when you click on the image is the CLICK EVENT... Which does not set any variable specifying which image was clicked.

I have an Idea how to get around this, but it will take some time to code.

Tell me if I'm on the right track...  If I am on the right track, I hope you will be more generous with the points when I post the code...


Cheers!

     
0
 
neo24Author Commented:
You are defenetly on the right track!

0
 
neo24Author Commented:
Adjusted points to 175
0
 
neo24Author Commented:
I thought of something,
when I paste a picture I can always know which Item it is, cause it starts with oleobjects.item(0). At the moment of pasting, I can also assign a key to the image. RTF.Oleobjects.Item(0).Key="Whatever" . Maybe this can help you out. And I will give you more points, you can have them all.
0
 
mcriderCommented:
RE: Step 7... I said messagebox, I meant Debug.Print



Cheers!
0
 
neo24Author Commented:
Hey man Thanx. I will e-mail you and tell you how to get even more points. My friend is gonna give you 200 points aswell for this.
0
 
mcriderCommented:
neo24,

Just open a new question in this category with the title "FOR MCRIDER ONLY" and I will answer it...

Thanks for the points! Glad I could help!


Cheers!
0
 
mcriderCommented:
By the way, don't forget to mention this question Q.10297532 when you open the new question... ;-)


Cheers!
0
 
mcriderCommented:
Neo24,

Is Geo24 and Neo24 the same person? There has been a complaint... See http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=10298051 

If they are not the same person, I would go to the above thread and defend myself...

Cheers!
0
 
mcriderCommented:
Neo24, Are you still there?
0
All Courses

From novice to tech pro — start learning today.