2016-06-30 19 views
1

WPFでは、TextBoxコントロールの垂直方向の配置を既定でサポートしています。 Windowsフォームでは、TextBoxコントロールの垂直方向の配置を設定する方法はありません。WindowsForms TextBoxの垂直配置を設定するにはどうすればよいですか?

私の場合、私は複数行のテキストボックスを使用しており、テキストはTextBoxの下部に表示する必要があり、それを入力しながら整列を維持する必要があります。

入力したテキストの行数を取得し、テキストの長さに基づいてテキストボックスの境界を計算しようとしました。単語の折り返しでテキストを編集するときに行数が正しく取得されません。

enter image description here

いずれかが編集中にテキストボックスの垂直方向の配置を維持するために、この上で私を助けることができますか?

私は、そのスレッドで与えられた提案を使用してテキストボックスの場所を変更しようとしました。テキストを編集するとき。テキストを編集しようとすると、境界が正しく更新されず、テキストの一部がテキストボックスに隠されます。私はフォントサイズとテキストの幅に基づいて境界を計算しました。

Size textSize = TextRenderer.MeasureText(TextBoxText, this.textBoxControl.Font); 
int textBoxTop = this.textBoxControl.Bounds.Top; 
int nol = (textSize.Width > this.textBoxControl.Width) ? ((textSize.Width)/this.textBoxControl.Width) + 1 : 1; 
{ 
    if (nol > n) 
    { 
     n = nol; 
     rect1 = this.textBoxControl.Bounds; 
     if (top + (height - nol * textSize.Height) > top) 
     { 
      rect1.Y = top + (height - nol * textSize.Height); 
      rect1.Height = nol * textSize.Height; 
      this.textBoxControl.Bounds = rect1; 
     } 
     else 
     { 
      this.textBoxControl.Bounds = rect1; 
     } 
    } 
    else if (nol < n) 
    { 
     n = nol; 
     rect1 = this.textBoxControl.Bounds; 
     if (rect1.Y + nol * textSize.Height < top + height) 
     { 
      rect1.Y += textSize.Height - this.textBoxControl.Margin.Top; 
      rect1.Height -= textSize.Height; 
      //this.textBoxControl.Bounds = rect1; 
     } 
     if (nol == 1) 
     { 
      rect1.Y = top + height - textSize.Height; 
      rect1.Height = textSize.Height; 
      //this.textBoxControl.Bounds = rect1; 
      } 
      this.textBoxControl.Bounds = rect1; 
    } 
} 

そのテキストを編集しながら細かい作業が、いくつかのケースではnolライン数が誤って計算されます。折り返し線を含むテキストボックスの実際の行数を取得するにはどうすればよいですか?

+0

関連:http://stackoverflow.com/questions/2653153/c-sharp-winforms-vertical-alignment-for-textbox-etc –

+0

私はそのスレッドで与えられた提案を使ってテキストボックスの位置を変更しようとしました。テキストを編集するとき。 – Adhi

+0

WinFormsのWPFからTextBoxをホストできます。 –

答えて

1

私はちょっとTextBoxのように見えるパネルの下部にドッキングTextBoxを持つコントロールを作成しました:

// Make sure you have this. 
    using System.Linq; 

public class BottomAlignTextBox : Panel 
{ 
    public BottomAlignTextBox() 
    { 
     this.BackColor = Color.White; 
     this.BorderStyle = (Application.RenderWithVisualStyles) ? BorderStyle.FixedSingle : BorderStyle.Fixed3D; 
     this.Size = new Size(200, 200); 
     this.Padding = new Padding(5, 0, 4, 2); 

     bottomAlignTextBox.Dock = DockStyle.Bottom; 
     bottomAlignTextBox.Multiline = true; 
     bottomAlignTextBox.WordWrap = true; 
     bottomAlignTextBox.AcceptsReturn = true; 
     bottomAlignTextBox.BorderStyle = BorderStyle.None; 
     bottomAlignTextBox.Height = 20; 

     bottomAlignTextBox.TextChanged += delegate 
     { 
      if (bottomAlignTextBox.Height < this.Height - 20) 
      { 
       if (TextRenderer.MeasureText(bottomAlignTextBox.Text, bottomAlignTextBox.Font).Width > bottomAlignTextBox.Width + 6) 
       { 
        string longestLine = bottomAlignTextBox.Lines.OrderByDescending(s => TextRenderer.MeasureText(s, bottomAlignTextBox.Font).Width).First(); 
        bottomAlignTextBox.Text = bottomAlignTextBox.Text.Replace(longestLine, longestLine.Substring(0, longestLine.Length - 1) + Environment.NewLine + longestLine[longestLine.Length - 1]); 
        bottomAlignTextBox.Height += 19; 
        bottomAlignTextBox.SelectionStart = bottomAlignTextBox.Text.Length + 2; 
        bottomAlignTextBox.SelectionLength = 0; 
       } 
      } 
     }; 

     this.Controls.Add(bottomAlignTextBox); 
     this.Click += delegate { bottomAlignTextBox.Focus(); }; 
    } 

    public new string Text 
    { 
     get { return bottomAlignTextBox.Text; } 
     set { bottomAlignTextBox.Text = value; } 
    } 

    private TextBox bottomAlignTextBox = new TextBox(); 
} 
+0

あなたの提案をありがとう、私はこれを試しているが、高さは、テキストを入力したときに間違って計算されます。 私は、テキストボックスに表示される実際の行を得ることができない、ある時点で遅れています。適切な行数を取得すると、 'LineCount * FontHeight'のようなコードを使ってテキストボックスの高さを簡単に計算できます。 これらの行数を取得するオプションはありますか。 – Adhi

+0

@Adhi今修正して、最も長い行の最後の文字を改行に移動します。 –

+0

こんにちは@ Shad0wk、 サンプルをありがとう、私は行の終わりとして選択の開始を変更して、継続的にテキストを入力することができます。 'bottomAlignTextBox.SelectionStart = bottomAlignTextBox.Text.Length; bottomAlignTextBox.Height + = bottomAlignTextBox.Font.Height; ' これでうまくいきましたが、もうこれ以上行う必要はありません。テキストボックスから文字を削除またはバックスペースすると、行数に応じて行の高さを減らす必要があります。どのように私はこれを達成することができます。 – Adhi

0

私はそれが完全に可能であるかどうかわからないが、私はあなたがラップすることができると思いますPanelコントロールまたは何らかのソートを使ったコントロールと、それをラップPanelコントロールの下部にドッキングしますか?サイズが動的である必要がある場合は、Anchorプロパティの周りを再生する必要があります。

+0

良いアイデア!あなたがコードを表示するとさらに良くなります。 –

+0

実際にこれを実際にコードする必要はありませんか?あなたのテキストボックスまたはRTB(使用しているもの)のDockの位置を「ボトム」に設定し、パネルの背景色をテキストボックスと同じに設定すると、実際に行う必要があります。頼んでいる。 –

関連する問題