非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

C#: Tab size in TextBox

      2015/12/29

I think that there is a request from many users regarding tab size change in TextBox. But in C#, there is no way of changing tab size in TextBox. Therefore, if we want to change tab size, It is necessary to use Win32API.

The below is the source code.

using System; 
using System.Drawing; 
using System.Windows.Forms; 
  
class Mainwindow{ 
   static void Main(){ 
      Application.Run(new Test()); 
   } 
  
class Test : Form{ 
  
   //Win32 API 
   [System.Runtime.InteropServices.DllImport(“User32.dll”)] 
   static extern IntPtr SendMessage( 
       IntPtr hWnd, int msg, int wParam, int [] lParam); 
  
   public Test(){ 
      this.StartPosition = FormStartPosition.CenterScreen; 
      this.Width = 600; 
      this.Height = 250; 
      this.Text = “Test”
  
      TextBox textBox = new TextBox(){ 
         Font = new Font(“Courier New”, 10), 
         ScrollBars = ScrollBars.Both, 
         WordWrap = false,      //Not wrap 
         AcceptsTab = true,      //tab is inserted in TextBox by [Tab] key 
         Dock = DockStyle.Fill, 
         BorderStyle = BorderStyle.FixedSingle, 
         Multiline = true
         Parent=this
      }; 
  
      //tab size is designated  
      const int EM_SETTABSTOPS = 0x00CB; 
      SendMessage(textBox.Handle, EM_SETTABSTOPS, 1, new int[] {16}); 
  
   } 

スポンサーリンク

 - C#応用編