Rich Text box Properties problem - c#

<RichTextBox AcceptsTab="True" ForceCursor="True" IsDocumentEnabled="True" TextChanged="ContentChanged" Name="TextContent"/>
In C# file i am not able to get Text property of Rich Textbox.
I am trying to get this like;
TextContent.Text= "hello"
But it is giving compile time error.
'System.Windows.Controls.RichTextBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Windows.Controls.RichTextBox' could be found (are you missing a using directive or an assembly reference?)
Please suggest me.

Generally, you need to work with Blocks property. But, if you are using FlowDocument for representing RichTextBox content, then you can access text with Document property.
For example, writing content:
XAML:
<RichTextBox Name="rtb">
</RichTextBox>
Code:
FlowDocument contentForStoring =
new FlowDocument(new Paragraph(new Run("Hello, Stack Overflow!")));
rtb.Document = contentForStoring;
To read content you simply access Document property:
FlowDocument yourStoredContent = rtb.Document;
If you need just to take text, you have more simple way - TextRange class. Next code will retrieve all text content:
TextRange storedTextContent =
new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string yourText = storedTextContent.Text;

If you want to retrieve the text from the rich text box then use this code,
string content = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;

Related

How to copy richtextbox content with link?

I want to copy richtextbox content with keeping format same and hyperlinks. But it is been copied as a plain text without hyperlinks. I am using linklabel in richtextbox.
private void Bttn_copy_Click(object sender, EventArgs e)
{
richtxtbx_email.SelectAll();
Clipboard.Clear();
Clipboard.SetText(richtxtbx_email.SelectedRtf, TextDataFormat.Rtf);
}
and trying this:
DataObject dto = new DataObject();
dto.SetText(mesrtf, TextDataFormat.Rtf);
dto.SetText(mes, TextDataFormat.UnicodeText);
Clipboard.Clear();
Clipboard.SetDataObject(dto);
Can you help me solve this issue ?
Hyperlinks are just a way of a using hypertext links inside an editor that is capable of rendering them as such.
When copying the text from the textbox, you can only copy the plaintext itself.
Note that RichTextBox.SelectedRtf is property of type string.
RichTextBox doesn't hold a hyperlink like HTML does.
It only detects if a certain text looks like a link and automatically colores it blue, add underline and detect if the user clicks on it.
It does so if the RichTextBox.DetectUrls Property is set to true.
If you are copying data to a new RichTextBox and don't see the link that was detected in the other RichTextBox then you just need to set this property to true before you copy the text.
On the other hand, if you need real links so that the text is one thing and the link is another have a look here.

How can I access the text within an WPF text box as a string from c#?

I am building a form which has the usual form of text box next to a "browse..." button. If a file is browsed for, it is then pasted into the text box.
I want to be able to use a separate function to read in the file, but I can't find any way of getting the text outside the scope of the function that pastes it upon closing the browse dialog.
So the XAML text is this:
<TextBlock Grid.Row="3" Grid.ColumnSpan="3">Source File:</TextBlock>
<TextBox Grid.Row="4" Grid.ColumnSpan="3" Name="FileNameTextBox" Margin="2"></TextBox>
<Button Grid.Column="3" Margin="2" Name="button1" Click="button1_Click" Grid.Row="4">Browse...</Button>
And the C# code behind it is this:
public void button1_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
}
}
I'm new to all this, so please explain it in a way I can understand, if possible... But after doing this, why can I not refer to the text in another function with "FileNameTextBox.Text" again? Doesn't that exist in XAML, not in the C# code? The new function can't "see" it.
I feel like it's somehow due to having "(object sender..." in the browse function, but I can't see a way to do this same kind of thing again without making a "Read what you just browsed for" button after it, which would make no sense.
Would {binding} the text box into this other code somehow work out? I haven't found a description of that which has allowed me to understand it well enough to use that at all, so far.
Even if that did work, I still don't get which places I can, and cant, declare a "DataContext", as it isn't suggested half the time.
EDIT: So, I came back to it later on in the day and, through what I can only image is a classic case of adding or deleting a curvy bracket, C# was suddenly able to detect the object FileNameTextBox where I was typing. Wack. Anyway, thanks, everyone.

Add line break to richt text content control

I have a function which add text to a rich content control in word. I have succeeded to get the content control by searching voor the tag. Then I add text with line breaks to the content control, but it does not accept the line breaks. It simply add the text on one line.
I would run the function (as in code below) several times, and add each new text on a new line in de content control.
I've tried several options as giving here on stackoverflow, but it still does not work. Can you please help me?
//Insert Text to manual.
public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, string text)
{
SdtElement element = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>().FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag);
if (element == null)
throw new ArgumentException($"ContentControlTag \"{contentControlTag}\" doesn't exist.");
element.Descendants<Text>().First().Text = text;
return doc;
}
Unless your string starts with a line-break or paragraph-break, whatever you add to the content control will simply be appended to the same line as whatever line already contains the last character. And, although you've described it as a 'rich content control', you might reconfirm that is the case - depending on how they're configured, plain text content controls can also accept paragraph breaks.

How can you get a xaml element using C#?

I have a xaml page samplePage.xaml and its corresponding .cs file samplePage.xaml.cs. In this page there is a textBox textBox1.
Now I create an instance of the page:
PhoneApp1.samplePage s = new PhoneApp1.samplePage();
and after that, I would like to set the value in the text box by calling:
s.textBox1.Text = "whatever"
but it turns out there is an error message saying
'PhoneApp1.samplePage' does not contain a definition for 'textBox1'
and no extension method 'textBox1' accepting a first argument of type
PhoneApp1.samplePage' could be found (are you missing a using
directive or an assembly reference?)
I would like to know how to get the xaml element using C# code?
You textbox is probably private or protected. Add a wrapper in your page to expose the textbox, something like :
public TextBox TextBox1
{
get
{
return this.textBox1;
}
}
(mind the case)
Then just use the property from anywhere you want: s.TextBox1.Text = "whatever";
Good question, you'll need to use XamlReader.Load to load your page/control at runtime, then you'll be able to access the controls within it:
http://msdn.microsoft.com/en-us/library/cc189076(v=vs.95).aspx#using_xamlreaderload
http://blogs.silverlight.net/blogs/msnow/archive/2008/10/09/silverlight-tip-of-the-day-60-how-to-load-a-control-straight-from-xaml.aspx

How to bind data with the rich text box with MVVM pattern?

How to bind data dynamically with the document property of the rich textbox. I am using MVVM in Wpf with c#?
EDIT:
I tried with this example in "codeproject.com/KB/WPF/BindableWPFRichTextBox.aspx"; but i can't understand what is happening in that example. I am very new to WPF and MVVM.
It's throwing error in the line
try {
var stream = new MemoryStream(Encoding.UTF8.GetBytes(GetDocumentXaml(richTextBox)));
var doc = (FlowDocument)XamlReader.Load(stream);
// Set the document
richTextBox.Document = doc;
}
catch (Exception) { richTextBox.Document = new FlowDocument(); }
the error is like "Data at the root level is invalid. Line 1, position 1." i am giving value like "Sample Text"
I found the xaml text should be like
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="schemas.microsoft.com/winfx/2006/xaml/… generated by app back-end</Paragraph>
</FlowDocument>" But how to get this text?
I hope I interpret your question correctly:
I assume you are binding to a normal string (sample text) with the RichTextBox you got from codeproject. This will not work, 'cause the Document you have to bind is a FlowDocument and it has a specific format. If you assign a string you will get the error "data invalid" when it tries to create a FlowDocument from the string
Here's a link on how to create a FlowDocument via XAML or via CodeBehind.
http://msdn.microsoft.com/en-us/library/aa970909.aspx
Then the converter comes into play: Out of the string representation it creates a real FlowDocument.
So, if you want to display your sample text bind to a string in the VM like this:
<FlowDocument PagePadding=\"5,0,5,0\" AllowDrop=\"True\" "
+ "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ "<Paragraph>Your sample text</Paragraph></FlowDocument>"

Categories