Reputation: 91
I am trying to create a chat program using C#. In my program I have an emoticon form that has the list of emoticons. I want to display those in my richtextbox in the main form. Any idea how? I am stuck trying to solve this. I really have no idea how.
BTW, the emoticons are buttons. I have already tried displaying them in a textbox but only in the same form. I want it to display into the richtextbox of the main form whenever I click the emoticon.
Well, the only code that I've tried is displaying it on a richtextbox in the emoticon form, like this:
public void Image(Bitmap displayPic)
{
Clipboard.SetDataObject(displayPic);
DataFormats.Format imgFormat = DataFormats.GetFormat(DataFormats.Bitmap);
if (richTextBox1.CanPaste(imgFormat))
{
richTextBox1.Paste(imgFormat);
}
else
{
MessageBox.Show("The data format provided is not supported by this control.");
}
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bit = new Bitmap(Properties.Resources.shocked);
Image(bit);
}
Upvotes: 1
Views: 814
Reputation: 5629
Basically, you just need to set up a communications system. In cases like these, you usually don't access the rich text box directly, instead you ask the form nicely to "please insert this emoticon into the text".
This can be done by giving your main form a public function to insert said smiley, giving your emoticons form a constructor accepting the main form's object type, and storing that main form's object in a private variable in the emoticons form, so you can make the call on it at any time.
For thread safety, Delegates should be used to access any UI stuff in a different form, since it runs on another thread. You should google around a bit to find out more about that.
Form MainForm
{
// Button to open the smileys window. Note you give "this" (MainForm)
// to the EmoteForm so it can call this object's public functions.
private void btnOpenEmoticons_Click(object sender, EventArgs e)
{
// open the smileys window, giving this as parameter
EmoteForm emoticons = new EmoteForm(this);
emoticons.Show();
}
// Delegates are always needed to access user interface stuff from external
// sources, since they generally run on different threads.
private delegate void InsertEmoticonDelegate(RichTextBox rtb, String emotCode);
//The function you call from the emoticons form
public void InsertEmoticon(String emotCode)
{
// Invoke and Delegate are needed for thread safety:
// you're asking this nicely, not stuffing it in there.
chatTextBox.Invoke(new InsertEmoticonDelegate(AddEmoticon), chatTextBox, emotCode));
}
// needs to match the Delegate so it can be called with Invoke
private void AddEmoticon(RichTextBox rtb, String emotCode)
{
// perform code to add the image defined by the "emotCode" string
// into the rich text box "rtb".
}
}
And for the emoticons form:
Form EmoteForm
{
private MainForm m_MainWindow;
// public constructor
public EmoteForm (MainForm mainWindow)
{
this.m_MainWindow = mainWindow;
InitializeComponents();
}
// The smiley button you click
private void somesmileybutton_Click(object sender, EventArgs e)
{
this.m_MainWindow.InsertEmoticon(":D");
}
}
Communications will happen as follows:
btnOpenEmoticons
is clicked, creating a new EmoteForm
object giving the main form as parameter, and then showing that form.EmoteForm
, somesmileybutton
is clicked. Calls m_MainWindow
's public InsertEmoticon
function with string ":D"chatTextBox
is (nicely) asked to please find a spot in its busy schedule to execute a function of the InsertEmoticonDelegate
type, meaning one with a RichTextBox
and a String
as parameters.AddEmoticon
as the function to execute, and the rich text box itself and the string ":D" as its parameters.AddEmoticon
function is executed, and figures out from the given argument which smiley to put into the given rich text box.Of course, you can actually give the Image object instead of the smiley code string like I did, but I dunno... seems simpler to me to manage those with codes.
As for the actual inserting of an image into the rich text... the simplest solution seems to be be to make a backup of whatever's currently in the clipboard, then put your image into the clipboard, then paste it in the rich text box, and then restore the clipboard's original contents. Sounds ugly, but it works, apparently. Once you got your Image object, this should do the trick:
IDataObject originalClip = Clipboard.GetDataObject();
Clipboard.SetImage(img);
rtb.Paste();
Clipboard.SetDataObject(originalClip);
Upvotes: 1
Reputation: 744
You can try like this using clipboard:
Emotion Form: On click of emotion you have this code:
Form1 f1 = new Form1();
Image img = Image.FromFile(fname)
Clipboard.SetImage(img);
f1.insertImotion()
Chat Form:
public void insertImotion(Clipboard)
{
RichTextBox1.Paste();
}
Upvotes: 1
Reputation: 744
I think you have to use clipboard to perform this.
try this code:
Image img = Image.FromFile(fname)
Clipboard.SetImage(img);
RichTextBox1.Paste();
Upvotes: 0