2017-02-10 11 views
1

私はC#でチャットアプリケーションを作成していますが、メッセージの到着時刻をメッセージのすぐ後ろから表示することもできます。
右から始まるtextBoxまたはrichTextBoxに書き込むにはどうすればよいですか?C#のテキストボックスの終わりからテキストを書き込む方法は?

これは私のコードは今のところどのように見えるかです:

 textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular); 
     textBox1.AppendText(text + "\n"); 
     textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic); 
     textBox1.AppendText("sent at " + DateTime.Now.ToString("h:mm") + "\n"); 
+0

テキストの半分を右揃えにする方法をお尋ねしますか? – Andrew

+0

これはWindowsフォームまたはWPFを使用していますか? –

答えて

1

使用TextBox.TextAlignment Property

textbox1.TextAlignment = TextAlignment.Right; 

そうでなければ、それは固定サイズであり、何の改行がない場合、あなたのような何かを行うことができますこの

string time = "12:34PM"; 
string text = "Hello".PadRight(100) + time; 
textBox1.AppendText(text + "\n"); 

またはこのコードを使用してください。多分このようなものでしょうか?

textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular); 
textBox1.AppendText(text + "\n"); 
textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic); 
textBox1.AppendText(("sent at " + DateTime.Now.ToString("h:mm")).PadLeft(100) + "\n"); 
+0

ありがとう:)それは、整列プロパティで動作しました: –

0

感謝:) それは、配向性で働いていた:代わりのappendTextの

 textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular); 
     textBox1.AppendText(text + "\n"); 
     textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic); 
     textBox1.SelectionAlignment = HorizontalAlignment.Right; 
     textBox1.AppendText("sent at " + DateTime.Now.ToString("h:mm") + "\n"); 
     textBox1.SelectionAlignment = HorizontalAlignment.Left; 

    } 
0

は、私はあなたにも使用した後の文字列を追加することができString.Formatの

string text = "This is the message \n"; 
string dt = "sent at " + DateTime.Now.ToString("h:mm") + "\n" 
textBox1.Text = string.Format("{0} {1}", text, dt); 

を示唆しています本文の長さ関数、および後に日付を追加します。

関連する問題