For the first time ever, I'm investigating RichTextBox
controls in C# Windows forms. I know I need this control in my app as TextBox
is too simple for my needs.
I have the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _19_richtextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBoxHome_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (char)(Keys.Return))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
}
}
}
}
For the moment I just want whatever is typed in one RichTextBox
to be displayed on the other RichTextBox
on hitting Return.
The issue is that everytime I hit Return, the data is being transfered to the other control but the first control is left with a carriage return before the cursor. This happens everytime I hit Return. Both the controls accept multiline input. How do I make it stop doing this?
i would like a way to make the "Home:" part in bold.
i have found very little info on this on my searches. the following are the only actual code that i could understand.
rtb1.Rtf = @"{\rtf1\ansi {\b hello} {\i World}}" ;
richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";
im not sure how to proceed with this info. i just want the richtextbox to display "Home:" and "Away:" in bold and be able to handle URLs in text.
please advise what should i specify when searching this potic on google or any reference your could think of would be of great help.
thank you again for taking interest.
i have made progress on my issue and thought to share.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using开发者_JS百科 System.Windows.Forms;
namespace _19_richtextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBoxHome_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)(Keys.Enter))
{
e.Handled = true;
// richTextBoxChat.Rtf = @"{\rtf1 \b Home: \bO}" + @richTextBoxHome;
// do not delete next 2 lines
//string test = @"{\rtf1 \b Home: \b0";
//test = test + richTextBoxHome.Rtf + "}";
string chatBuffer = richTextBoxChat.Rtf;
string buffer = @"{\rtf1";
buffer = buffer + chatBuffer;
buffer = buffer + @"\b Home:\b0";
buffer = buffer + richTextBoxHome.Rtf + "}";
// MessageBox.Show(buffer);
richTextBoxChat.Rtf = buffer;
//do not delete the following 2 lines
//richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text);
richTextBoxHome.Clear();
}
}
}
}
just need to figure out how to get rid of the new lines/carriage returns in the text.
any tips most welcome.
thanks.
that did not workout so well. ended up using the below instead.
this.richTextBoxChat.SelectionFont = new Font(this.richTextBoxChat.Font.FontFamily, this.Font.Size, FontStyle.Bold);
richTextBoxChat.AppendText("Home: ");
this.richTextBoxChat.SelectionFont = new Font(this.richTextBoxChat.Font.FontFamily, this.Font.Size, FontStyle.Regular);
richTextBoxChat.AppendText(richTextBoxHome.Text);
richTextBoxChat.ScrollToCaret();
richTextBoxHome.Clear();
You can set the event to handled to prevent further processing of the return keydown event
Try this:
private void richTextBoxHome_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (char)(Keys.Return))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
Edit: added e.SuppressKeyPress = true; Cory Charlton pointed this out in his post.
Edit: also, as others have mentioned, use the KeyPress event handler, so if someone holds down enter/return the event is triggered over and over again. an example of the usage is as follows:
private void richTextBoxHome_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.Equals((Char) Keys.Enter))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
e.Handled = true;
}
}
Edit: Also, there is a Property you can set on the RichTextBox to not allow MultiLine strings to be entered into the textbox. This does pose a problem when pasting content with multiple lines, it will only take the first line.
For Bold words: You can change the SelectionFont of the RichTextBox to set it's font attributes:
private readonly Font BoldSelectionFont = new Font("Arial", 9.0f, FontStyle.Bold);
private readonly Font RegSelectionFont = new Font("Arial", 9.0f, FontStyle.Regular);
...
richTextBoxChat.SelectionFont = BoldSelectionFont;
richTextBoxChat.AppendText("Home: ");
richTextBoxChat.SelectionFont = RegSelectionFont;
richTextBoxChat.AppendText(richTextBoxHome.Text + "\n");
...
Use the KeyPress
instead of KeyDown
and set e.Handled=true
To suppress the KeyDown
event, write e.Handled = true
.
Also, the best way to check what key was pressed is to write if (e.KeyCode == Keys.Enter)
Handle and supress the key stroke:
private void richTextBoxHome_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (char)(Keys.Return))
{
richTextBoxChat.AppendText("Home:" + richTextBoxHome.Text + "\n");
richTextBoxHome.Clear();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
精彩评论