[英]Adding text to a RichTextBox in the constructor
我通過繼承RichTextBox來創建一種特殊的文本框。 我希望能夠在框中設置初始文本,我認為我可以在構造函數中進行如下操作:
class SpecialTextBox : RichTextBox
{
public SpecialTextBox()
{
BackColor = System.Drawing.Color.ForestGreen;
AppendText("Initial Text...");
Text += "and some more initial text.";
}
}
上面的代碼中的構造函數設置背景色,但不顯示初始文本。 我的問題是,為什么文本不出現?如何實現呢? 我可能希望初始文本是可配置的(也許傳遞給構造函數)。
我可以稍后通過調用來成功添加文本
specialTextBox1.AppendText("This text will appear.")
為什么沒有顯示構造函數文本?
Windows在為其加載XAML時將覆蓋FlowDocument
的文本。 在調用RichTextBox
的構造函數之后,會發生這種情況。
稍后再嘗試添加文本,例如在Loaded
事件中:
public class SpecialTextBox : RichTextBox
{
public SpecialTextBox()
{
Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(
System.Drawing.Color.ForestGreen.A,
System.Drawing.Color.ForestGreen.R,
System.Drawing.Color.ForestGreen.G,
System.Drawing.Color.ForestGreen.B));
this.Loaded += new RoutedEventHandler(SpecialTextBox_Loaded);
}
void SpecialTextBox_Loaded(object sender, RoutedEventArgs e)
{
AppendText("Initial Text...");
}
}
更新:您可能只想在加載的XAML沒有初始文本的情況下執行此操作:
void SpecialTextBox_Loaded(object sender, RoutedEventArgs e)
{
var range = new TextRange(Document.ContentStart, Document.ContentEnd);
if (range.IsEmpty)
{
AppendText("Initial Text...");
}
}
更新資料
好的,WinForms。 WinForms在設計器生成的c#代碼中設置初始文本。 您應該會看到以下內容:
this.richTextBox1 = new WinformsRichTextBox.SpecialTextBox();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(12, 2);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(1233, 507);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
最后一行是擊敗您的構造函數的原因。 您可以通過重寫Text
方法並拒絕初始設置來解決此問題:
public class SpecialTextBox : RichTextBox
{
bool suppressInitialSetText = true;
public SpecialTextBox()
{
BackColor = System.Drawing.Color.ForestGreen;
AppendText("Initial Text...");
this.VisibleChanged += new EventHandler(SpecialTextBox_VisibleChanged);
}
void SpecialTextBox_VisibleChanged(object sender, EventArgs e)
{
// Just in case, once the control becomes visible disable the kludge.
if (Visible)
suppressInitialSetText = false;
}
public override string Text
{
get
{
return base.Text;
}
set
{
if (!suppressInitialSetText || !string.IsNullOrEmpty(value) || Parent != null)
base.Text = value;
suppressInitialSetText = false;
}
}
}
在此方案中,僅當表單設計器中的“文本”行為空時,才會顯示“初始文本...”。 否則,表單設計器中的文本將覆蓋構造函數中的文本。
雖然看起來有些脆弱和笨拙。
因此,不會出現“初始文本...”,而是“還有更多初始文本”。 有嗎 乍一看,似乎是通過分配第二個字符串來破壞第一個字符串...除了使用+ =運算符而不是=之外。
我從未使用過AppendText方法,但是我可以提供一些猜測:
嘗試注釋掉Text + = ...行,看看它是否顯示第一行。 還要在AppendText前后放置一個斷點,以查看Text發生了什么。
在構造函數中,您可以將初始文本設置為
base.Text =“根據需要輸入文字”;
我將您的類添加到Windows Forms項目中,並將其添加到Load
事件中的表單中。 它似乎按預期工作。 如何將SpecialTextBox
添加到窗體?
這就是我的Load事件的樣子。
private void Form1_Load(object sender, EventArgs e)
{
SpecialTextBox stb = new SpecialTextBox();
this.Controls.Add(stb);
stb.Visible = true;
}
這是該類的樣子。
class SpecialTextBox : System.Windows.Forms.RichTextBox
{
public SpecialTextBox()
{
BackColor = System.Drawing.Color.ForestGreen;
AppendText("Initial Text...");
Text += "and some more initial text.";
}
}
這是由包含SpecialTextBox
的表單的.designer.cs中的設計器生成的代碼引起的。
要自定義該代碼,可以定義一個自定義ControlDesigner
並重寫InitializeNewComponent
,如下所示:
internal class SpecialTextBoxDesigner
: System.Windows.Forms.Design.ControlDesigner
{
public override void InitializeNewComponent(
System.Collections.IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
this.Control.Text = "Initial Text...";
}
}
然后通過DesignerAttribute
將其應用於您的SpecialTextBox
:
[System.ComponentModel.Designer(typeof(SpecialTextBoxDesigner))]
public partial class SpecialTextBox : RichTextBox
您需要事先在項目中添加對System.Designer的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.