表示するテキスト行があり、表示されるテキストの見出し部分のみに下線を引きたいと考えています。どうすればこれを達成できますか?
メッセージ: これは、クライアントの名前に対するメッセージです。
「Message:」には下線が引かれています。
代わりにRichTextBoxを使用してください。
this.myRichTextBox.SelectionStart = 0;
this.myRichTextBox.SelectionLength = this.contactsTextBox.Text.Length-1;
myRichTextBox.SelectionFont = new Font(myRichTextBox.SelectionFont, FontStyle.Underline);
this.myRichTextBox.SelectionLength = 0;
RichTextBox コントロールを使用して下線を引くことができます
int start = rtbTextBox.Text.IndexOf("Message:", StringComparison.CurrentCultureIgnoreCase);
if(start > 0)
{
rtbTextBox.SelectionStart = start;
rtbTextBox.SelectionLength = "Message:".Length-1;
rtbTextBox.SelectionFont = new Font(rtbTextBox.SelectionFont, FontStyle.Underline);
rtbTextBox.SelectionLength = 0;
}
この例では、質問で提供したテキストを直接使用しています。このコードをプライベート メソッドにカプセル化し、見出しテキストを渡すとより効果的です。
例えば:
private void UnderlineHeading(string heading)
{
int start = rtbTextBox.Text.IndexOf(heading, StringComparison.CurrentCultureIgnoreCase);
if(start > 0)
{
rtbTextBox.SelectionStart = start;
rtbTextBox.SelectionLength = heading.Length-1;
rtbTextBox.SelectionFont = new Font(rtbTextBox.SelectionFont, FontStyle.Underline);
rtbTextBox.SelectionLength = 0;
}
}
フォームから次のように呼び出します。UnderlineHeading("Message:");
リッチ テキスト ボックスを使用してテキストを表示する場合は、次のようにします。
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Underline);
richTextBox1.SelectedText = "Message:";
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Regular);
richTextBox1.SelectedText = " This is a message for Name of Client.";
または、メッセージが動的で、ヘッダーとテキストが常にコロンで区切られている場合は、次のようにすることができます。
string message = "Message: This is a message for Name of Client";
string[] parts = message.Split(':');
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Underline);
richTextBox1.SelectedText = parts[0] + ":";
richTextBox1.SelectionFont = new Font("Times New Roman", 10, FontStyle.Regular);
richTextBox1.SelectedText = parts[1];
または、テキストをラベルに動的に表示したい場合は、次のようにすることができます。
string message = "Message: This is a message for Name of Client";
string[] parts = message.Split(':');
Label heading = new Label();
heading.Text = parts[0] + ":";
heading.Font= new Font("Times New Roman", 10, FontStyle.Underline);
heading.AutoSize = true;
flowLayoutPanel1.Controls.Add(heading);
Label message = new Label();
message.Text = parts[1];
message.Font = new Font("Times New Roman", 10, FontStyle.Regular);
message.AutoSize = true;
flowLayoutPanel1.Controls.Add(message);
考えただけで、マスクされたテキストボックスを使用するか、リッチテキストボックスに下線を付けてカスタムコントロールを作成し、クライアントアプリケーションで使用できます。GDI+ API を使用して下線付きのテキスト ボックスを作成できる可能性があると聞きましたが、よくわかりません。
ありがとうマヘシュ・コテカール