Windowsフォームのラベルのワードラップ
質問
Windowsフォームでラベルのワードラップ機能を取得するにはどうすればよいですか?
パネルにラベルを配置し、ラベルにテキストを動的に追加しました。ただし、パネルの長さを超えています。どうすれば解決できますか?
解決
クイックアンサー:オフに切り替えるオートサイズ。
ここでの大きな問題は、ラベルの高さが自動的に変更されないことです(幅のみ)。これを正しく行うには、ラベルをサブクラス化し、垂直サイズ変更ロジックを含める必要があります。
基本的にOnPaintで行う必要があるのは:
- テキストの高さを測定します(Graphics.MeasureString)。
- ラベルの高さがテキストの高さと等しくない場合、高さを設定して戻ります。
- テキストを描画します。
コンストラクターのResizeRedraw スタイルフラグ。
他のヒント
実際、受け入れられた答えは不必要に複雑です。
ラベルをAutoSizeに設定すると、ラベルをテキストに合わせて自動的に拡大します。 (これには垂直成長が含まれます。)
特定の幅で折り返すようにする場合は、MaximumSizeプロパティを設定できます。
myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;
テスト済みで動作します。
私の場合(パネルのラベル)では、 label.AutoSize = false
および label.Dock = Fill
を設定します。
ラベルテキストは自動的に折り返されます。
悪いニュース:autowrapプロパティはありません。
朗報:トンネルの終わりにライトがあります!
これをプログラムで実行して動的にサイズを変更することもできますが、最も簡単な解決策は次のとおりです。
- ラベルのプロパティを選択
- AutoSize = True
-
MaximumSize =( Width 、 Height )ここで、 Width =ラベルの最大サイズと Height =ラップするピクセル数
MSDN から、 ラベルでテキストを自動的に折り返す :
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
public class GrowLabel : Label {
private bool mGrowing;
public GrowLabel() {
this.AutoSize = false;
}
private void resizeLabel() {
if (mGrowing)
return;
try {
mGrowing = true;
Size sz = new Size(this.Width, Int32.MaxValue);
sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
this.Height = sz.Height;
}
finally {
mGrowing = false;
}
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
resizeLabel();
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
resizeLabel();
}
protected override void OnSizeChanged(EventArgs e) {
base.OnSizeChanged(e);
resizeLabel();
}
}
簡単な解決策を見つけなければならなかったので、これらのプロパティを持つTextBoxを使用しました:
var myLabel = new TextBox
{
Text = "xxx xxx xxx",
WordWrap = true,
AutoSize = false,
Enabled = false,
Size = new Size(60, 30),
BorderStyle = BorderStyle.None,
Multiline = true,
BackColor = container.BackColor
};
@hypoの回答に基づいてより良いものを用意してください
public class GrowLabel : Label {
private bool mGrowing;
public GrowLabel() {
this.AutoSize = false;
}
private void resizeLabel() {
if (mGrowing)
return;
try {
mGrowing = true;
int width = this.Parent == null ? this.Width : this.Parent.Width;
Size sz = new Size(this.Width, Int32.MaxValue);
sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
this.Height = sz.Height + Padding.Bottom + Padding.Top;
} finally {
mGrowing = false;
}
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
resizeLabel();
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
resizeLabel();
}
protected override void OnSizeChanged(EventArgs e) {
base.OnSizeChanged(e);
resizeLabel();
}
}
int width = this.Parent == null? this.Width:this.Parent.Width;
これにより、親にドッキングしたときに自動成長ラベルを使用できます。パネル。
this.Height = sz.Height + Padding.Bottom + Padding.Top;
ここでは、上下のパディングを処理します。
- パネル内にラベルを貼る
-
パネルの
ClientSizeChangedイベント
を処理し、 スペースを埋めるラベル:private void Panel2_ClientSizeChanged(object sender, EventArgs e) { label1.MaximumSize = new Size((sender as Control).ClientSize.Width - label1.Left, 10000); }
-
ラベルの
Auto-Size
をtrue
に設定します
- ラベルの
Dock
をFill
に設定します
すべてのユースケースに適合するかどうかはわかりませんが、単純なトリックを使用してラッピング動作を取得することがよくあります。
Label
のサイズを処理する1x1 TableLayoutPanel
内に AutoSize = false
で Label
を配置します。
パネルがラベルの幅を制限している場合、ラベルのアンカープロパティをLeft、Rightに設定し、AutoSizeをtrueに設定できます。これは、概念的にはPanel’の SizeChanged
イベントをリッスンし、ラベルのMaximumSizeを new Size(((Control)sender).Size.Width、 0)
以前の回答で提案されているとおり。 Anchorプロパティにリストされているすべての側は、含まれているControlのそれぞれの内側に固定されています。したがって、アンカーに2つの反対側をリストすると、コントロールの寸法が効果的に設定されます。左と右に固定するとControl’のWidthプロパティが設定され、上と下に固定するとそのHeightプロパティが設定されます。
C#としてのこのソリューション:
label.Anchor = AnchorStyles.Left | AnchorStyles.Right;
label.AutoSize = true;
コンテンツとは無関係にラベル幅を設定したい場合、最も簡単な方法は次のとおりです:
- オートサイズをtrueに設定
- 最大幅を希望どおりに設定
- 最小幅を同一に設定
ラベルの幅は一定になりましたが、自動的に高さが調整されます。
ダイナミックテキストの場合は、フォントサイズを小さくします。必要に応じて、ラベルテキストが設定されているサブで次のスニペットを使用します。
If Me.Size.Height - (Label12.Location.Y + Label12.Height) < 20 Then
Dim naam As String = Label12.Font.Name
Dim size As Single = Label12.Font.SizeInPoints - 1
Label12.Font = New Font(naam, size)
End If
これは、InpitWindowというフォームで役立ちました。 Designer for Labelの場合:
AutoSize = true;
Achors = Top, Left, Right.
private void InputWindow_Shown(object sender, EventArgs e) {
lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left - btOK.Margin.Right -
lbCaption.Margin.Right - lbCaption.Margin.Left,
Screen.GetWorkingArea(this).Height / 2);
this.Height = this.Height + (lbCaption.Height - btOK.Height - btCancel.Height);
//Uncomment this line to prevent form height chage to values lower than initial height
//this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);
}
//Use this handler if you want your label change it size according to form clientsize.
private void InputWindow_ClientSizeChanged(object sender, EventArgs e) {
lbCaption.MaximumSize = new Size(this.ClientSize.Width - btOK.Width - btOK.Margin.Left * 2 - btOK.Margin.Right * 2 -
lbCaption.Margin.Right * 2 - lbCaption.Margin.Left * 2,
Screen.GetWorkingArea(this).Height / 2);
}
ボタンの寸法を変更せずに維持する必要がある場合:
myButton.Text = "word\r\nwrapped"
この問題の簡単な答えは、ラベルのDOCKプロパティを変更することです。 「なし」です。デフォルトで。
以下のHTMLのように、ラベルで style =&quot; overflow:Scroll&quot;
を使用します。これにより、パネル内のラベルにスクロールバーが追加されます。
<asp:Label
ID="txtAOI"
runat="server"
style="overflow:Scroll"
CssClass="areatext"
BackColor="White"
BorderColor="Gray"
BorderWidth="1"
Width = "900" ></asp:Label>