【问题标题】:Is there an easy way to create two columns in a popup text window?有没有一种简单的方法可以在弹出文本窗口中创建两列?
【发布时间】:2008-09-16 13:32:29
【问题描述】:

这似乎是一件容易的事。我只想弹出一个文本窗口并显示两列数据——左侧的描述和右侧显示的相应值。我没有过多地使用 Forms,所以我只是抓住了第一个看起来合适的控件,一个 TextBox。我认为使用标签是创建第二列的一种简单方法,但我发现事情并没有那么好。

我尝试这样做的方式似乎存在两个问题(见下文)。首先,我在许多网站上读到 MeasureString 函数不是很精确,因为字体非常复杂,还有字距调整问题等等。第二个是我不知道 TextBox 控件在下面用作它的 StringFormat 是什么。

不管怎样,结果是我总是在右栏中的项目中得到一个选项卡关闭的项目。我想我可以滚动自己的文本窗口并自己做所有事情,但是天哪,没有一种简单的方法可以做到这一点吗?

    TextBox textBox    = new TextBox();
    textBox.Font       = new Font("Calibri", 11);
    textBox.Dock       = DockStyle.Fill;
    textBox.Multiline  = true;
    textBox.WordWrap   = false;
    textBox.ScrollBars = ScrollBars.Vertical;

    Form form            = new Form();
    form.Text            = "Recipe";
    form.Size            = new Size(400, 600);
    form.FormBorderStyle = FormBorderStyle.Sizable;
    form.StartPosition   = FormStartPosition.CenterScreen;
    form.Controls.Add(textBox);

    Graphics g = form.CreateGraphics();

    float targetWidth = 230;

    foreach (PropertyInfo property in properties)
    {
        string text = String.Format("{0}:\t", Description);

        while (g.MeasureString(text,textBox.Font).Width < targetWidth)
            text += "\t";

        textBox.AppendText(text + value.ToString() + "\n");
    }

    g.Dispose();
    form.ShowDialog();

【问题讨论】:

    标签: c# winforms controls formatting


    【解决方案1】:

    感谢马特,您的解决方案对我很有用。这是我的代码版本...

    // This is a better way to pass in what tab stops I want...
    SetTabStops(textBox, new int[] { 12,120 });
    
    // And the code for the SetTabsStops method itself...
    private const uint EM_SETTABSTOPS = 0x00CB;
    
    [DllImport("User32.dll")]
    private static extern uint SendMessage(IntPtr hWnd, uint wMsg, int wParam, int[] lParam);
    
    public static void SetTabStops(TextBox textBox, int[] tabs)
    {
        SendMessage(textBox.Handle, EM_SETTABSTOPS, tabs.Length, tabs);
    }
    

    【讨论】:

    • 甚至更好——你让它更通用,这是完美的。我可能有的唯一其他建议是,如果您将 tabPos1-3 作为参数组合到一个数组中,那么您可以传递任意数量或任意数量,并且您根本不需要 int[] tabs... 行.祝 AZDean 好运。
    【解决方案2】:

    如果需要,您可以将此 VB.Net 代码翻译成 C#。这里的理论是您可以更改控件中选项卡的大小。

    Private Declare Function SendMessage _
      Lib "user32" Alias "SendMessageA" _
      (ByVal handle As IntPtr, ByVal wMsg As Integer, _
      ByVal wParam As Integer, ByRef lParam As Integer) As Integer
    
    
    Private Sub SetTabStops(ByVal ctlTextBox As TextBox)
    
      Const EM_SETTABSTOPS As Integer = &HCBS
    
      Dim tabs() As Integer = {20, 40, 80}
    
      SendMessage(ctlTextBox.Handle, EM_SETTABSTOPS, _
        tabs.Length, tabs(0))
    
    End Sub
    

    我也为您将一个版本转换为 C#。在 VS2005 中测试和工作。

    将此 using 语句添加到您的表单中:

    using System.Runtime.InteropServices;
    

    把它放在类声明之后:

        private const int EM_SETTABSTOPS = 0x00CB;
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);
    

    当你想设置制表位时调用这个方法:

        private void SetTabStops(TextBox ctlTextBox)
        {
            const int EM_SETTABSTOPS = 203;
            int[] tabs = { 100, 40, 80 };
            SendMessage(textBox1.Handle, EM_SETTABSTOPS, tabs.Length, tabs);
        }
    

    要使用它,这就是我所做的一切:

        private void Form1_Load(object sender, EventArgs e)
        {
            SetTabStops(textBox1);
    
            textBox1.Text = "Hi\tWorld";
        }
    

    【讨论】:

      【解决方案3】:

      如果您想要真正的表格,Haren 先生的答案是一个很好的答案。 DataGridView 将为您提供非常 Excel 电子表格类型的外观。

      如果你只想要一个两列布局(类似于 HTML 的表格),那么试试 TableLayoutPanel。它将为您提供所需的布局,并能够在每个表格单元格中使用标准控件。

      【讨论】:

        【解决方案4】:

        我相信唯一的方法是做一些类似于你正在做的事情,但使用固定字体并用空格做你自己的填充,这样你就不必担心标签扩展。

        【讨论】:

        • 是的,固定字体也可以解决问题,但是固定字体看起来很丑,所以这对我来说不是一个好的解决方案。
        • 哇,被问题的提问者打分了,基于字体美学!!
        【解决方案5】:

        文本框不允许使用 HTML 吗?如果是这种情况,只需使用 HTML 将文本格式化为表格。否则,请尝试将文本添加到数据网格,然后将其添加到表单。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-06
          • 1970-01-01
          • 2014-01-10
          • 2023-03-06
          • 2012-10-27
          • 1970-01-01
          • 2011-02-16
          相关资源
          最近更新 更多