winforms 如何在Windows窗体中的TextBox中心显示文本

polhcujo  于 2023-08-07  发布在  Windows
关注(0)|答案(5)|浏览(158)

我在Windows窗体应用程序中有多行TextBox控件,我想在TextBox区域的中心显示一些文本(水平和垂直)。我如何才能实现这样的行为?
先谢了。

oogrdqng

oogrdqng1#

对于水平对齐,可以使用

textBox1.TextAlign = HorizontalAlignment.Center;

字符串
出于各种无聊的原因,窗口中的文本框旨在根据所使用的字体自动调整其高度。要控制高度和文本的垂直居中,可以快速创建一个自定义UserControl,用它替换所有文本框。
请参考此答案
https://stackoverflow.com/a/2653360/2967572

ccrfmcuu

ccrfmcuu2#

可以做的几乎和在TextBox中垂直居中文本一样好:
只需将TextBox放入只有一个单元格的TableLayoutPanel中。然后禁用TextBox的边框,启用TableLayoutPanel的边框,并将其背景色设置为Window(即与现在的无边框文本框相同,因此您无法将TextBox与周围的TableLayoutPanel区分开来)。最后,为了使TextBox在表格单元格内垂直居中,将TextBox的锚点设置为“左+右”,这样它将填充表格单元格到边框。
注意:我忘记了为我的截图设置水平居中(以满足您的问题),但这当然是微不足道的。

textBox1.Anchor = ((AnchorStyles)((AnchorStyles.Left | AnchorStyles.Right)));
textBox1.BorderStyle = 0;

tableLayoutPanel1.BackColor = SystemColors.Window;
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.ColumnCount = 1;
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
tableLayoutPanel1.Controls.Add(textBox1, 0, 0);

字符串


的数据

lqfhib0f

lqfhib0f3#

如果您只想垂直对齐文本框中的文本,可以使用VerticalContentAlignment=“Center”
例:

<TextBox x:Name="txt_box" Text="tralala" VerticalContentAlignment="Center" TextAlignment="Left" Height="25"/>

字符串

lrl1mhuk

lrl1mhuk4#

感谢Boeryepes share https://stackoverflow.com/a/44862429/8458887的想法,使用像TextBox这样的Datagridview,我创建了这个。添加Datagridview,设置位置和大小,然后:

private void SetupTextBoxDataGridView(DataGridView dataGridView)
{
    DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
    dataGridView.Columns.AddRange(column);
    //dataGridView.Columns[0].Name = "C" + dataGridView.Name;
    dataGridView.AllowUserToAddRows = false;
    dataGridView.AllowUserToDeleteRows = false;
    dataGridView.AllowUserToResizeColumns = false;
    dataGridView.AllowUserToResizeRows = false;
    dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

    dataGridView.BackgroundColor = SystemColors.Window;
    dataGridView.BorderStyle = BorderStyle.Fixed3D;   //FixedSingle
    dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
    dataGridView.ColumnHeadersVisible = false;
    dataGridView.RowHeadersVisible = false;
    dataGridView.DefaultCellStyle.BackColor = SystemColors.Window;   //Window Cell color when in focus or selected
    dataGridView.DefaultCellStyle.ForeColor = SystemColors.ControlText;
    dataGridView.DefaultCellStyle.SelectionBackColor = SystemColors.Window;   //Window Cell color when not in focus or selected
    dataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.ControlText;
    dataGridView.MultiSelect = false;
    dataGridView.ScrollBars = ScrollBars.None;

    dataGridView.Rows.Add("");

    dataGridView.CellBeginEdit += DataGridView_CellBeginEdit;
    dataGridView.CellEndEdit += DataGridView_CellEndEdit;
    //dataGridView.MouseEnter += DataGridView_MouseEnter;
}
private void DataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    DataGridView control = sender as DataGridView;
    control.GridColor = SystemColors.Highlight;   //Border Color - SystemColors.ControlDark
    if (!string.IsNullOrEmpty((string)control.Rows[e.RowIndex].Cells[e.ColumnIndex].Value))
    {
        SendKeys.Send("{Right}");
    }
}
private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    DataGridView control = sender as DataGridView;
    control.GridColor = SystemColors.ControlDark;   //Border Color - SystemColors.ControlDark
}
//Begin Edit Mode automatically when mouse enter the cell
private void DataGridView_MouseEnter(object sender, EventArgs e)
{
    DataGridView control = sender as DataGridView;
    DataGridViewCell cell = control[control.CurrentCell.ColumnIndex, control.CurrentCell.RowIndex];
    if (cell != null && !cell.IsInEditMode && !cell.ReadOnly)
    {
        control.BeginEdit(true);
    }
}

字符串
使用方法:

public Form1()
{
    InitializeComponent();

    SetupTextBoxDataGridView(dataGridView2);
}


使用方法:

//Get text
    txtNew.Text = (string)dataGridView2.CurrentCell.Value;
    //Add text
    dataGridView2.CurrentCell.Value = "Hello";
    dataGridView2.Rows[0].Cells[0].Value = "Hello";

e3bfsja2

e3bfsja25#

在属性编辑器中将textAlign属性设置为居中。它将从水平方向居中对齐文本。

相关问题