这似乎是一件容易的事情要做。我只是想出了一个文本窗口并显示两个列的数据说明在左侧和一个相应的价值显示在右侧。我还没有工作形式很多,所以我只是抓住了第一次控制,似乎是适当的,一个文本框。我认为,使用标签将是一个容易的方式创建的第二柱,但是我发现的东西只是不工作。

似乎有两个问题的方式,我试图做到这一点(见下文)。首先,我读了许多网站,MeasureString功能不是非常精确,由于复杂的字体,紧缩的问题和所有。第二个是,我不知道是什么的文本控制是使用作为其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();
有帮助吗?

解决方案

如果你想,你可以翻译这个VB.Net 代码。该理论在这里是你的尺寸变化的一项控制。

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 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);

称这种方法,当你想到设置tabstops:

    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";
    }

其他提示

谢谢,马特,你的解决方案的工作非常适合我。这是我的版本你的代码...

// 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);
}

如果你想要的东西真正表格先生哈伦的答案是一个很好的一个。的条件会给你一个非常Excel电子表格式的外观。

如果你只想要一个双柱布局(HTML类似的表格),然后试试TableLayoutPanel.它会给你的布局,你的愿望有能力使用标准的控制内的各表格单元。

我相信,唯一的办法就是做类似的东西你在做什么,但是使用固定字体,做你自己的填补空间,使你不必担心卡膨胀。

不该文本框允许HTML使用情况?如果是这种情况下,只是使用HTML格式的文本表。否则,尝试加入该文本的数据表格,然后添加到这的形式。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top