有没有一种简单的方法在弹出文本窗口中创建两列? [英] Is there an easy way to create two columns in a popup text window?

查看:105
本文介绍了有没有一种简单的方法在弹出文本窗口中创建两列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一件容易的事。我只是想弹出一个文本窗口,并显示两列数据 - 在左边的说明和右侧显示相应的值。我还没有与形式的工作很多,所以我只是抓住了那似乎是适当的,一个TextBox第一个控件。我想用的标签是创建第二列一个简单的方法,但我发现事情根本不工作那么好。



似乎有两个问题这样我试图做到这一点(见下文)。首先,我由于多么复杂的字体阅读在众多网站的MeasureString功能不是很精确,其字距问题和所有。第二个问题是我不知道TextBox控件用作为下它的StringFormat。



总之,结果是,我总是在右列的项目结束通过标签关闭。我想我可以推出自己的文本窗口,并尽一切自己,但哎呀,是不是有一个简单的方法来做到这一点?



感谢您的帮助!

 文本框的textBox =新的TextBox(); 
textBox.Font =新的字体(宋体,11);
textBox.Dock = DockStyle.Fill;
textBox.Multiline = TRUE;
textBox.WordWrap = FALSE;
textBox.ScrollBars = ScrollBars.Vertical;

Form表单=新表();
form.Text =配方;
form.Size =新的大小(400,600);
form.FormBorderStyle = FormBorderStyle.Sizable;
form.StartPosition = FormStartPosition.CenterScreen;
form.Controls.Add(的textBox);

图形G = form.CreateGraphics();

浮动targetWidth = 230;

的foreach(在属性中的PropertyInfo财产)
{
字符串文本=的String.Format({0}:\t,说明);

,而(g.MeasureString(文字,textBox.Font).WIDTH< targetWidth)
文字+ =\t;

textBox.AppendText(文本+ value.ToString()+\\\
);
}

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


解决方案

如果你愿意,你可以翻译这个VB.Net代码为C#。这里的理论是,您更改控制选项卡的大小。

 私人声明函数SendMessage函数_ 
库USER32别名SendMessageA_
(BYVAL句柄作为IntPtr的,BYVAL WMSG整数,_
BYVAL的wParam为整数,为ByRef lParam的作为整数)作为整数


私人小组SetTabStops(BYVAL ctlTextBox作为文本框)

常量EM_SETTABSTOPS作为整数=安培; HCBS

尺寸标签()作为整数= {20,40,80}

SendMessage函数(ctlTextBox.Handle,EM_SETTABSTOPS,_
tabs.Length,标签(0))

端子

我转换一个版本为C#的你。 。经过测试,在VS2005工作



这using语句添加到您的窗体:

 使用System.Runtime.InteropServices; 



将这个权类声明后:

 私人const int的EM_SETTABSTOPS = 0x00CB; 
函数[DllImport(User32.dll中,字符集= CharSet.Auto)]
公共静态外部的IntPtr SendMessage函数(IntPtr的H,INT味精,诠释的wParam,INT [] lParam的);

当你要设置的制表位调用此方法:

 私人无效SetTabStops(文本框ctlTextBox)
{
const int的EM_SETTABSTOPS = 203;
INT []选项= {100,40,80};
SendMessage函数(textBox1.Handle,EM_SETTABSTOPS,tabs.Length,制表符);
}

要使用它,这里是我所做的:

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
SetTabStops(textBox1中);

textBox1.Text =Hi\tWorld;
}


This seemed like an easy thing to do. I just wanted to pop up a text window and display two columns of data -- a description on the left side and a corresponding value displayed on the right side. I haven't worked with Forms much so I just grabbed the first control that seemed appropriate, a TextBox. I thought using tabs would be an easy way to create the second column, but I discovered things just don't work that well.

There seems to be two problems with the way I tried to do this (see below). First, I read on numerous websites that the MeasureString function isn't very precise due to how complex fonts are, with kerning issues and all. The second is that I have no idea what the TextBox control is using as its StringFormat underneath.

Anyway, the result is that I invariably end up with items in the right column that are off by a tab. I suppose I could roll my own text window and do everything myself, but gee, isn't there a simple way to do this?

Thanks for any help!

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

解决方案

If you want, you can translate this VB.Net code to C#. The theory here is that you change the size of a tab in the control.

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

I converted a version to C# for you, too. Tested and working in VS2005.

Add this using statement to your form:

using System.Runtime.InteropServices;

Put this right after the class declaration:

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

Call this method when you want to set the tabstops:

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

To use it, here is all I did:

    private void Form1_Load(object sender, EventArgs e)
    {
        SetTabStops(textBox1);

        textBox1.Text = "Hi\tWorld";
    }

这篇关于有没有一种简单的方法在弹出文本窗口中创建两列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆