C#.Net富文本框与变量交互问题:读取文本及显示变量报错解决
Hey there, let's tackle your two C# .NET questions one by one!
Depending on whether you need plain text or formatted rich text, you can use the appropriate property of the RichTextBox control (I'll cover both WinForms and WPF scenarios since you didn't specify):
WinForms Scenario
WinForms' RichTextBox has two straightforward properties:
- Use
Textto get plain, unformatted text - Use
Rtfto get the full rich text format (preserves fonts, colors, etc.)
Example code:
// Store plain text in a predefined string variable string plainText = myRichTextBox.Text; // Store formatted RTF content in a variable string formattedRtfContent = myRichTextBox.Rtf;
WPF Scenario
WPF's RichTextBox works a bit differently—you'll need to use TextRange to extract plain text:
using System.Windows.Documents; using System.IO; using System.Text; // Get plain text from WPF RichTextBox TextRange textRange = new TextRange( myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd ); string plainText = textRange.Text; // To get RTF in WPF, use the Save method with a MemoryStream MemoryStream rtfStream = new MemoryStream(); textRange.Save(rtfStream, DataFormats.Rtf); string rtfContent = Encoding.UTF8.GetString(rtfStream.ToArray());
This error is all about variable scope—you declared numStr14 inside a local code block (like a button click event or a method), but you're trying to access it from a different block where it's not visible. Here's how to fix it:
1. Adjust the variable's scope to class level
Move the declaration of numStr14 to the top of your class (outside any methods) so it's accessible across all methods in the class:
// Declare the variable at the class level (field) private string numStr14; // Also, reuse a single Random instance to avoid duplicate numbers private Random randomGenerator = new Random(); // Example: Generate the string in one method (e.g., a button click) private void GenerateNumberButton_Click(object sender, EventArgs e) { var code14 = "0xxxx0xxx000x0"; // Assign to the class-level variable (no 'var' here) numStr14 = Regex.Replace(code14, @"([^0-9.])", match => randomGenerator.Next(1, 9).ToString() ); } // Then display it in another method (e.g., another button click) private void ShowResultButton_Click(object sender, EventArgs e) { richResult.Text = numStr14; }
2. Keep everything in the same scope (if you don't need cross-method access)
If you only need to generate and display the string in one place, make sure both the variable declaration and the richResult.Text assignment are in the same code block:
private void GenerateAndShowButton_Click(object sender, EventArgs e) { var code14 = "0xxxx0xxx000x0"; var randomGenerator = new Random(); var numStr14 = Regex.Replace(code14, @"([^0-9.])", match => randomGenerator.Next(1, 9).ToString() ); // Same scope—no error here! richResult.Text = numStr14; }
Quick tip about Random
Your original code creates a new Random instance every time it replaces a character. Since Random uses the system clock for seed, this can lead to identical numbers if replacements happen quickly. Reusing a single Random instance (like I did above) fixes this.
内容的提问来源于stack exchange,提问作者Bizzie

