あなたの説明: は、次の2つの部分から構成され、いくつかのデータを持っています。 1つの方法、つまりあなたが望む方法は、完全な説明を表示し、強調する必要があるテキストは斜体で表示する必要があります。
しかし、強調された単語が太字で表示されているか、別のフォントで表示されているか、または赤色で表示されているか、逆さまに表示されている表示方法について考えることもできます。あなたは、データの表示方法からあなたのデータを分離することにより
、戻ってあなたの質問に、データ自体今
を変更することなく、このビューを変更することが可能です。だから、いくつかのテキストと、強調する必要があるいくつかのサブテキストがあります。どのテキストを強調する必要があるかを知る方法は、あなたの質問の範囲外です。この情報をどのようにこのクラスに入れるかは、この質問の範囲外です。必要なのは、翻訳されたテキストの通訳者が完全なテキストを知ることができ、どのフレーズを強調しなければならないかのフォーマットにテキストを翻訳する翻訳機能です。
強調を開始する場所と停止する場所は、実際には重要ではありません。明確に定義された方法を使用してください。例えば
、あなたは強調のためにHTMLの方法を使用することができます。
Some normal text <em>is emphasized</em> and normal again
したい場合は、あなたが他の方法を使用することができ、ダブルフォワードを追加するように強調またはオフ、または何か他のものに切り替えるためにスラッシュ。それはよく定義されている限り重要ではありません。
だからあなたのテキストとあなたの強調を保持しているクラスは翻訳されたテキスト
public string ExtractTranslatedText()
{
// TODO: take your original text and add <em> and </em>
// to mark begin and end of emphasis
}
を抽出するための手順を必要とするあなたが、ほとんどこの機能を作成する成功したようです。
今度は、この強調を保持する文字列を取得したら、それを表示する必要があります。これはどのように表示クラスに依存しますか。
リッチテキストボックスにイタリック体で表示するとします。 RichTextBoxからクラスを派生させるか、RichTextBoxの拡張関数を作成することができます。あなたがしたい場合
public static void AppendText(this RichTextBox box, string text, FontStyle fontStyle)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
Font savedSelectionFont = box.SelectionFont;
box.SelectionFont = new Font(box.SelectionFont, fontStyle);
box.AppendText(text);
box.SelectionFont = savedSelectionFont;
}
:
は、私たちは与えられたSystem.Drawing.FontStyleでテキストボックス内の既存のテキストにテキストを追加する関数を必要とするExtension Methods Demystified
public static class RichTestBoxExtensions
{
// TODO: add the required functions
}
を参照してください。テキストを別の色で表示するメソッドを追加することもできます。 コードは非常に似ています。
今、あなたは、あなたの答えは、私はそれがはるかに手軽になり2番目のリソースの使用を避けることができますがあれば、非常に貴重だった
pubic static ShowEmphasis(this RichTextBox, string text, FontStyle emphasisStyle)
{
const string emphasisOn = "<em>";
const string emphasisOff = "</em>";
while(!String.IsNullOrEmpty(text))
{ // still some text to print
// get the substring until first emphasisOn
int indexStartEmphasis = text.Index(emphasisOn);
if (indexStartEmphasis == -1)
{ // no emphasisOn anymore: write all in emphasisStyle
richTextBox.AppendText(text, emphasisStyle);
text = String.Empty; // no text left
}
else
{ // write until emphasisOn:
string normalText = text.SubString(0, indexStartEmphasis);
richTextBox.AppendText(normalText, FontStyle.Normal);
// remove the normalText + <em> from text:
text = text.Substring(indexStartEmphasis + emphasisOn.Length);
// do the same until emphasisOff
int indexStopEmphasis = text.Index(emphasisOff);
if (indexStopEmphasis == -1)
{ // no emphasisOff anymore: write all in emphasisStyle
richTextBox.AppendText(text, FontStyle.Normal);
text = String.Empty; // no text left
}
else
{ // write until emphasisOff in emphasisStyle:
string emphasizedText = text.SubString(0, indexStopEmphasis);
richTextBox.AppendText(emphasizedText, emphasisStyle);
// remove the emphasizedlText + </em> from text:
text = text.Substring(indexStopEmphasis + emphasisOff.Length);
}
}
}
}
else
{
}
string normalText = text.Substring(0, indexStartEmphasis
}
}
よく考えていただきありがとうございます。私の現在のデザインには多くの問題があるので、今からMVC形式を使用しようとしています。私はそれも非常に便利だとわかった拡張メソッドへのポインタをありがとう。 – benbants