I am trying to copy data from an excel sheet into a a rich text box in a Winform .NET project.
Right now there are grid lines are coming up in the rich text box. How do I eliminate the grid lines from the rich text box.
Because I do not want to show the grid lines in the rich text box.
Please help me
Thanks Sandeep
you can do like this in the keydown event of you richtextbox (if you are using the normal paste method)
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.V)
{
e.Handled = true;
string st = Clipboard.GetText();
richTextBox1.Text = st;
}
}
hope this helps
Banged my head against wall with this one. I was testing the different methods available on the Clipboard class with nUnit and every method returned null. With nUnit, you must add the [RequiresSTA] attribute to the class.
Final result would look like [TestFixture, RequiresSTA].
Source: https://stackoverflow.com/a/5293312/1444511
Related
My problem is that when I paste text into a rich text box I would like to get rid of all the formatting. Now this basically works:
private void RichTextBox1_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (hasImage(Clipboard.GetDataObject()))
{
e.CancelCommand();
}
RichTextBox rtb = sender as RichTextBox;
if (Clipboard.ContainsText(TextDataFormat.Rtf) || Clipboard.ContainsText())
{
// get rid of formatting
string append = Clipboard.GetText(TextDataFormat.UnicodeText);
Clipboard.SetText(append, TextDataFormat.UnicodeText);
}
}
The problem is: If I previously had formatted text in my rich text box (e.g., bold text) and removed this text, the caret would still be bold (or italic if italic was previously selected - this is easy to spot since the caret would be oblique!). In this case, the text would also be inserted with this formatting. How can I get rid of that?
If you want to clear the entire document of previous formatting, you could do something like:
TextRange wholeDocument = new TextRange(Document.ContentStart, Document.ContentEnd);
wholeDocument.ClearAllProperties();
If you want to keep the previous formatting (i.e. you are appending), it looks like this may be useful: https://msdn.microsoft.com/en-us/library/ms597038(v=vs.110).aspx
Although this only solves part of the problem, it is satisfactory for our purposes:
rtb.Selection.ClearAllProperties();
With this line, the formatting of the selected text is removed. So if you mark some text that is formatted and press CTRL+V, the text from the clipboard is inserted without formattings.
What remains unsolved is how to clear the formattings of the caret if you just remove a couple of characters using backspace.
I have a RichTextBox control in my view. I'm using code-behind (UI logic only) to format the RTF within my RichTextBox which is working from a 'Format' button click event which instantiates a TextRange:
private void _btnFormat_Click(object sender, RoutedEventArgs e)
{
TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
rangeOfText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
rangeOfText.ApplyPropertyValue(TextElement.FontSizeProperty, "12");
rangeOfText.ApplyPropertyValue(TextElement.FontFamilyProperty, "Arial");
rangeOfText.ApplyPropertyValue(TextElement.FontStyleProperty, "Normal");
rangeOfText.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
}
I want to also remove any tables within the RTF. Can I use the same approach maybe from the Table class to remove tables from my RichTextBox? Thanks
You'll have to climb down the Blocks and get the descendants
of the FlowDocument and get all the Tables and then remove them from the its Parent.
Ok, if anyone is trying to achieve this I don't think it's possible. Maybe you can iterate over a simple table in an Rtf string and remove the tags but if you can't determine user input the Rtf is by far too complex. Therefore here's my solution (of sorts...)
private void _btnFormat_Click(object sender, RoutedEventArgs e)
{
TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
rangeOfText.ApplyPropertyValue(Table.BorderThicknessProperty, "3");
rangeOfText.ApplyPropertyValue(Table.BorderBrushProperty, Brushes.Red);
}
In the 'Format' button click event I've set table borders to Red. On my save back to the database method I've used this simple if statement:
private void SaveToDbCommandAction()
{
if(PastedText.Contains("trowd"))
{
Xceed.Wpf.Toolkit.MessageBox.Show("Cannot save Article. Please remove pasted tables");
}
else
{
SaveToDb(RTBText);
}
}
Therefore when the user pastes in a table they are warned via the red cell borders. This is particularly useful if they paste a table with invisible borders and can't actually see the table. The If statement then determines whether the Rtf string contains a 'trowd' tag therefore preventing the save.
I'm using a 'Paste' button command in my view Model to copy RTF from the clipboard. PastedText is my string property that a RichTextBox is bound to in my view:
private void FormatPastedTextCommandAction()
{
PastedText += Clipboard.GetText(TextDataFormat.Rtf);
}
This works and the text is pasted on pressing the Paste button. However, I want to lock down the formatting on the paste function and remove all formatting from the pasted RTF string (colour, italics, set to black Arial 12).
I would just use PastedText += Clipboard.GetText();
to get the plain text but it pastes in at a different font size and I need it in RTF format. I've looked at iterating over the RTF string and doing a find/replace on font size, colour etc. but the RTF is very complex even for a couple of words.
Is there any way around this? Thanks
In the end I used code behind in the view to strip formatting from the RichTextBox itself using a 'Format' button:
private void _btnFormat_Click(object sender, RoutedEventArgs e)
{
TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
rangeOfText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
rangeOfText.ApplyPropertyValue(TextElement.FontSizeProperty, "12");
rangeOfText.ApplyPropertyValue(TextElement.FontFamilyProperty, "Arial");
rangeOfText.ApplyPropertyValue(TextElement.FontStyleProperty, "Normal");
rangeOfText.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
rangeOfText.ApplyPropertyValue(Paragraph.MarginProperty, new Thickness(0));
}
This does a good job and doesn't really break the MVVM pattern as the code is UI logic only.
If I copy some text with different format and paste it to my richtextbox it is not plain I mean its format will be copied as well.
Is there anyway I can copy-paste as a plain text?
By the way my program is on WinForm
thanks for any answer
you must use WinForm RichTextBox (not in UI, just in code), even if you are on WPF, in order to convert RTF to plain text. Use this method in your Copy event.
C# code :
private String ConvertRtfToText()
{
System.Windows.Forms.RichTextBox rtfBox = new System.Windows.Forms.RichTextBox();
rtfBox.Rtf = this.rtfData;
return rtfBox.Text;
}
VB.Net Code :
Private Function ConvertRtfToText() As String
Dim rtfBox As RichTextBox = New RichTextBox()
rtfBox.Rtf = Me.rtfData
Return rtfBox.Text
End Function
source : http://msdn.microsoft.com/en-US/en-en/library/vstudio/cc488002.aspx
I recently had the same issue. I did want to retain some of the formatting, i.e. paragraphs and line feeds, but I required all the addition text format to be removed.
I'm working in WPF but the RichTextBox interface is the same. I have created a button that will allow users to select some text and remove the formatting. It is very simple, you just need to use the ClearAllProperties() method on the TextSelection object.
C# Code (WPF):
private void ClearFormat_Click(object sender, RoutedEventArgs e)
{
rtbText.Selection.ClearAllProperties();
}
This is a super easy solution but perhaps not super elegant...
1) Add a plain textbox to your form and make it hidden
2) Create a button to remove the formatting (or you can do this
automatically when the text is pasted)
3) In the OnClick (or OnPaste) code just copy the text from the rich
textbox control to the plain textbox control then copy the text
from the plain textbox back to the rich textbox control (see example
below)
private void btnRemoveFormatting_Click(object sender, EventArgs e)
{
txtPlainText.Text = txtRTF.Text;
txtRTF.Text = ""; // Required - this makes sure all formatting is gone
txtRTF.Text = txtPlainText.Text;
}
I am creating a small app for personal use that allows me to clean my lists of objects. I am using a variety of filters to get a finalized list in a multiline text box. When i am finished, I use the following code to copy to the textbox to the clipboard.
#region COPY BUTTON
private void button3_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox_ListDestination.Text);
}
#endregion
What i would like to do now is add another button that allows me to save this same text to a .txt file using the SaveFileDialog. Can anyone help me with this? I am assuming I would use Streaming of some type, but I am out of my element here. Any help would be appreciated.
try
File.WriteAllText (TargetFilePath, textBox_ListDestination.Text);
For more information including sample code see MSDN.
If you want to obtain TargetFilePath via a SaveFileDialog see MSDN.
UPDATE
Sample code using SaveFileDialog:
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText (saveFileDialog1.FileName, textBox_ListDestination.Text);
}