如何设置在Windows的标签宽度窗体TextBox控件? [英] How to set the TAB width in a Windows Forms TextBox control?

查看:312
本文介绍了如何设置在Windows的标签宽度窗体TextBox控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一个WinForms TextBox控件多=真 AcceptsTab ==真,我怎么能设置宽度该选项卡显示的角色?

Given a WinForms TextBox control with MultiLine = true and AcceptsTab == true, how can I set the width of the tab character displayed?

我想用这个作为一个插件一个快速和肮脏的脚本输入框。这真的并不需要花哨可言,但如果标签没有被显示为8个字符宽这将是很好...

I want to use this as a quick and dirty script input box for a plugin. It really doesn't need to be fancy at all, but it would be nice if tabs were not displayed as 8 characters wide...

这是公认的答案:

// set tab stops to a width of 4
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);

public static void SetTabWidth(TextBox textbox, int tabWidth)
{
    Graphics graphics = textbox.CreateGraphics();
    var characterWidth = (int)graphics.MeasureString("M", textbox.Font).Width;
    SendMessage(textbox.Handle, EM_SETTABSTOPS, 1, 
                new int[] { tabWidth * characterWidth });
}

这可以称得上是你的表格的构造函数,但要注意:确保 InitializeComponents 首先运行。

This can be called in the constructor of your Form, but beware: Make sure InitializeComponents is run first.

推荐答案

我觉得送 EM_SETTABSTOPS 消息文本框将正常工作。

I think sending the EM_SETTABSTOPS message to the TextBox will work.

  • Link at MSDN
  • Here is another link

这篇关于如何设置在Windows的标签宽度窗体TextBox控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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