Share via


Insert Pictures in Rich Text Box

Question

Friday, January 29, 2010 12:30 PM

I have some images say A.png, B.png,C.png that I want the user to retrieve on the screen on keydown events A,B,C say like this

( http://img141.imageshack.us/img141/5687/problemcr.jpg )

But the thing is I want the pictures to be retrieved in the Rich Text Box so that I can have save/open/print/cut/copy/paste operations on my document.

Basically, somewhat like Microsoft Word where we can insert pictures, cut/copy/paste and then also save/retrieve them.

How can I proceed?

All replies (21)

Friday, January 29, 2010 2:06 PM ✅Answered | 3 votes

try this to insert image in richtextbox

Dim img As Image = Image.FromFile(filename )
Dim orgData = Clipboard.GetDataObject
Clipboard.SetImage(img)
Me.RichTextBox1.Paste()

Clipboard.SetDataObject(orgData )

kaymaf
If that what you want, take it. If not, ignored it and no complain

CODE CONVERTER SITE : http://www.carlosag.net/Tools/CodeTranslator/.


Friday, January 29, 2010 6:27 PM ✅Answered

optimus_prime , this code is quite similar to your other thread of adding images to a picturebox..

Public Class Form1

  Dim myDir As String = "C:\Users\trujade\Desktop\temp\"
  Dim myImg As Image

  Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    Try '-- since you are working w/files, use the Try..Catch not to freeze/crash your app
      Select Case e.KeyCode
        Case Keys.A
          myImg = Image.FromFile(myDir & "A.png")
        Case Keys.B
          myImg = Image.FromFile(myDir & "B.png")
        Case Keys.C
          myImg = Image.FromFile(myDir & "C.png")
        Case Else
          Exit Sub
      End Select
    Catch ex As Exception
      MsgBox(ex.Message)
      Exit Sub '-- to skip trying to add a "no image" to the clipboard.
    End Try
    Clipboard.SetImage(myImg)
    RichTextBox1.Paste()
  End Sub

  '-- block letters from being typed
  Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
    e.Handled = True
  End Sub

End Class

With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Wednesday, February 3, 2010 3:41 AM ✅Answered

  '-- load file
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ofd As New OpenFileDialog
    ofd.Filter = "the only format (*.tru)|*.tru"
    If ofd.ShowDialog = DialogResult.OK Then
      RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText)
    End If
  End Sub

  '-- save file
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim sfd As New SaveFileDialog
    sfd.Filter = "the only format (*.tru)|*.tru"
    If sfd.ShowDialog = DialogResult.OK Then
      RichTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText)
    End If
  End Sub

With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Friday, January 29, 2010 3:51 PM

Thanks for your reply kaymaf :) The code worked for me but I had one more thing to ask. I have lots of pictures to be displayed in the rich text box. Indeed I intend to insert images in the rich text box for every keydown event of the keyboard. A class or something in that case would be helpful or what else? and what functionality to be added in the class?


Friday, January 29, 2010 4:59 PM

for this only goal, it isn't necessary a new class 'MyRichTextbox'. It's sufficient the KeyDown event handler. But you'd better implement a method to get and set the image from file to clipboard. So you can write:

If e.Control Then ' if you must verify the user wants to put an image and not a character
  GetTheImage(e.KeyCode)
  Directcast(sender, RichTextbox).Paste()
Else
  'normal handle
End If

and in the GetTheImage function you put the code by Kaymaf

Private Sub GetTheImage(ByVal k As Keys)
Dim fileName as string
' build filename
' Dim img... ' etceteraplease, mark this as answer if it is THE answer

Diego Cattaruzza
Microsoft MVP - Visual Basic: Development
blog: http://community.visual-basic.it/Diego
web site: http://www.visual-basic.it


Saturday, January 30, 2010 2:52 AM

1) Since, I will capture the keystrokes and also the combination of Shift + Keys (for capital letters), I would use too much of Select/Case statements.

Would that be fine or the coding can be improved by some method?

2) How can I detect if Caps Lock is on/off on a keyboard?

3) Although, I don't need here in Rich Text Box, how can I detect Backspace keydown?


Saturday, January 30, 2010 3:43 AM | 1 vote

3)

  Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Back Then
      MsgBox("you pressed backspace key.", MsgBoxStyle.Information)
    End If
  End Sub

...........
try deleting back from Keys.Back and you will get a dropdown from the vb.net intelisense.  should help out quite a bit when coding. ;o)
With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Saturday, January 30, 2010 4:25 AM

Although, I am getting pictures in the rich text box but my motive to get the images in it was its ability to save/print etc.

How to use Save Dialog to save the images in the Rich Text Box?

And cut/copy/paste operations would also change. Because of the format checked and retrieved from clipboard.

Would be thankful for guidance.


Saturday, January 30, 2010 4:41 AM | 1 vote

to save rtb from a savefile dialog.

    Dim sfd As New SaveFileDialog()
    If sfd.ShowDialog() = DialogResult.OK Then
      RichTextBox1.SaveFile(sfd.FileName & ".rtf", RichTextBoxStreamType.RichText)
    End If

about using so many select case , should not be a problem..
you can check out this link about using breakpoints , and create a breakpoint on the line Private Sub RichTextBox1_KeyDown(ByVal.. , start your app and press "c", then press the step into button to see that it goes right to the case "C" of the keycode, and it does not read every line as a If/ElseIf would do.
With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Saturday, January 30, 2010 4:52 AM

if I would have to save it in my own format?


Saturday, January 30, 2010 4:54 AM

i might have just found something that could fix your so many select cases, but i might have given you some extra work. ;o)

  Dim myDir As String = "C:\Users\trujade\Desktop\temp\"
  Dim myImg As Image

  Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    Me.Text = "Key pressed: " & e.KeyCode
    Try
      myImg = Image.FromFile(myDir & e.KeyCode & ".png")
      Clipboard.SetImage(myImg)
      RichTextBox1.Paste()
    Catch ex As Exception
    End Try
  End Sub

the first line in the event ( should be removed ) will get you the correct key ascii to display as the form's text.. i think it is ascii..
well, in any case, click your keyboard to get the correct numbers and save your images to the appropriate numbers..

for ex. i have 3 images, a.png, b.png, c.png, which i had to reaname a.png to 65.png, b.png to 66.png, and c.png to 67.png. With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Saturday, January 30, 2010 5:16 AM

isnt' there suppose to be different ascii code for A and a?

A=65 ?

and a= 97 ?

but I am getting 65 both for A and a.

may be detecting Caps Lock would solve the problem?


Saturday, January 30, 2010 5:36 AM | 1 vote

i'm currently trying to figure out the capslock.. not much luck so far, but not giving up hope either. ;o)

about the shift key , you can check out the following code.. on shift key up/down it sets an integer. quite simple.

  Dim myDir As String = "C:\Users\trujade\Desktop\temp\"
  Dim myImg As Image
  Dim keyShift As Integer

  Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
    If e.KeyCode = Keys.ShiftKey Then keyShift = 1
    Try
      If keyShift = 1 Then
        myImg = Image.FromFile(myDir & e.KeyCode & "lc.png")
      Else
        myImg = Image.FromFile(myDir & e.KeyCode & ".png")
      End If
      Clipboard.SetImage(myImg)
      RichTextBox1.Paste()
    Catch ex As Exception
    End Try
  End Sub

  Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
    If e.KeyCode = Keys.ShiftKey Then keyShift = 0
  End Sub

i just renamed all the lowercase images to end w/lc..
ex. capital letter "A" image would be 65.png and lowercapital "a" would be 65lc.png, lc for lower capital. With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Saturday, January 30, 2010 5:43 AM

ok, i gave up on the capslock. lol..

for determining the CapsLock, if on/off, start a new thread, since it will make a great addon to the msdn library for the forum if solved.
good luck.
With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Saturday, January 30, 2010 5:46 AM

naah! man you never give up!

you always talk in CODE :D

fun talking to you....

btw, how can I save in my own format?


Saturday, January 30, 2010 5:57 AM

lol.

i think i might have solved it in your new thread .
in your rtb's keydown event, determine if the caps is either true/false before adding images..

it could get a little tricky w/the shift key and capslock, but i am sure you can figure it out.  good luck.  and good night for me. ;o) With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Tuesday, February 2, 2010 1:37 PM

1) How to use cut/copy/paste operations for these pictures in the Rich Text Box?

Its something different from coying/pasting/cutting text in RTB :-/

2) I learnt to use save option to save these pictures in Rich Text Box but wasn't able to use the open dialog to open the same file again in my application. Please help.

3) I also wanted help in using Print Dialog in my application.


Tuesday, February 2, 2010 4:04 PM | 1 vote

optimus_prime , start a new thread for your #'s 1 and 3..
about #2) , you have to save the rtb in richtextformat and load it the same.. here is an example..

  '-- load file
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ofd As New OpenFileDialog
    If ofd.ShowDialog = DialogResult.OK Then
      RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.RichText)
    End If
  End Sub

  '-- save file
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim sfd As New SaveFileDialog
    sfd.Filter = "Rich Text Format (*.rtf)|*.rtf"
    If sfd.ShowDialog = DialogResult.OK Then
      RichTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText)
    End If
  End Sub

With •. trujade**.** • : . Process . Start ( " mns " ) : End With


Wednesday, February 3, 2010 2:02 AM

Instead of RTF format, How to save it in (say, .tru) format ?


Monday, February 8, 2010 2:21 PM

What would I have to do if I want the above .tru file to open in Microsoft Word as well.

as it was opening previously in Rich Text Format


Monday, February 8, 2010 9:18 PM | 1 vote

open msword, select "all files ( *.* )" or "all documents ( *.* )" from the OpenFileDlg, and if your file was saved in any format that msword can access, it should not be a problem. one sec. lol.
yep. seems to work by loading a tru file in wordpad, so i doubt there would be any problems.
With •. trujade**.** • : . Process . Start ( " mns " ) : End With