Edit RichTextBox programmatically without losing formatting - c#

I have formatted text in the rtf file and I load it to my richTextBox. It works fine, but then I want to remove some text parts programmatically. If I do like that:
richTextBox.LoadFile("TextFile.rtf");
richTextBox.Text = richTextBox1.Text.Substring(fromPosition, length);
text formatting disappears. I've tried to work with richTextBox.Lines, but there is no delete or edit function. All I need is to delete parts of loaded text without losing its formatting. Is it possible?
Thanks in advance.

To remove text you first select it, programmatically by setting SelectionStart and -Length. Then you use the Cut Method:
richTextBox1.SelectionStart = 20;
richTextBox1.SelectionLength = 120;
richTextBox1.Cut();
If you want to avoid putting the removed text to the clipboard you can set it to "" instead of 'cutting' it:
richTextBox1.SelectedText = ""

Related

Excel how to fit text in cell, based on cell witdh

I want to set the witdh of a cell in my ExcelSheet.
Set the witdh:
worksheet.Columns["K"].ColumnWidth = 114.80;
When the text is larger then the ColumnWith the text is not visible.
I want to split the text to a new row in the same cell based on the ColumnWith.
I tried to add \r\n to the string in the Excel but no result.
EDIT after answers
This works perfectly:
worksheet.Columns["K"].ColumnWidth = 114;
Excel.Range rangeK = worksheet.get_Range("K1");
rangeK.EntireColumn.WrapText = true;
What you are looking for is this:
worksheet.Range("K1:K100").WrapText = True;
That code, for example, will set the cells from K1 to K100 to wrap the contents inside their cells.
You have a couple options. The first option (which is what I would suggest) is to autoresize the columns.
worksheet.Columns.AutoFit();
The next option is to word wrap all text, which I have not done but this link might be of use to you. I hope this helps.

WPF Textbox - How to save text without the wrapping-paragraph

I load some texts from a database with different lengths into a textbox. (From 1 character to 1000 words...)
I use TextWrapping="Wrap", so if the text is to big for the width of the textbox, it will create a new line....
By saving the text, the added paragraph from TextWrapping="Wrap" will also be saved. But I don't want this. The original text shouldn't be changed in any way...
How can I display the text on a new line in the textbox without adding a "real" paragraph?
Is there a way I can determine if the paragraph was added from TextWrapping="Wrap" or if it belong to the original text?
Thanks
As #StevenRands mentioned, the source text won't be changed in any way by using TextWrapping.
It only affects the display of the text inside the TextBox.
Normally if you used text Wrapping,it wont affect the original source text.But anyways Before saving back to database you can remove the new line constants from your text box text which will give you the original text loaded from db.

How to read selected text on richTextBox?

There is a richTextBox that has string. I want to select a few characters on the richTexBox by mouse and save them in a variable. I use this method:
richTextBox1.SelectedRtf;
It shows me selected characters but is has some additional string and i don't want them. How can I remove them?
This is additional string:
{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1065\uc1 }
try
richTextBox1.SelectedText
Here is the msdn link for more information
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectedtext(v=vs.110).aspx
Selection.Text property will return the selected text of Rich Edit Text box.
richTextBox1.Selection.Text
RichTextBox.SelectedText? may be?
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectedtext(v=vs.110).aspx
use this
richTextBox1.SelectedText (property)

Appending Date to the end of line in multiple textbox

how could i add a date to the very end of each line with another font and size ?!
for example i want to add 1:15 AM to the end of "hello World" line.
If it doesn't have to be a textbox, you can apply some nice formatting using some manipulation of datagridview.
Using 2 columns with the formatting and colors changed to match the rest of the controls.
It's just as easy to add lines to.
dataTextView.Rows.Add(txtAddText.Text, DateTime.Now.ToShortTimeString());
Example source: http://mcspazzy.com/code/TextDisplay.zip
AppendText exists for WinForms TextBoxes
textBox1.AppendText(DateTime.Now.ToString("h:mm tt"));
However, changing font and size for a just a part of a TextBox is not possible,
You need a RichTextBox for that
StringBuilder sb=new StringBuilder();
foreach(string s in richTextBox1.Lines)
{
sb.AppendLine(s + " "+DateTime.Now.ToString("h:mm"));
}
richTextBox1.Text=sb.ToString();

Include Font while pasting to Clipboard

I'm trying to include the font "KaiTi" while setting text on the clipboard, so that wherever I paste the data, the "KaiTi" Font will be the font that the text is in, inevitably keeping all of the data that I am pasting to the clipboard formatted the way that I want it too.
This is what I tried, I know it's off, but I don't know what to do..
TextBox texter = new TextBox();
texter.Text = strToClips;
texter.Font = new Font("KaiTi", 10);
Clipboard.SetText(texter.Text);
Use a RichTextBox. It puts rich text on the clipboard that includes formatting. Use its Copy() method. Pasting back now also works automatically without any code.
The richtextbox displayed the formatting OK, but I couldn't get Copy() to bring the font across. In the end I selected the text then sent a Ctrl-C character to copy it, as follows:-
myrichtextbox.SelectAll();
myrichtextbox.Focus();
SendKeys.Send("^C");

Categories

Resources