code_junkie

Is there an easy way to create two columns in a popup text window?

Nov 10th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. TextBox textBox = new TextBox();
  2. textBox.Font = new Font("Calibri", 11);
  3. textBox.Dock = DockStyle.Fill;
  4. textBox.Multiline = true;
  5. textBox.WordWrap = false;
  6. textBox.ScrollBars = ScrollBars.Vertical;
  7.  
  8. Form form = new Form();
  9. form.Text = "Recipe";
  10. form.Size = new Size(400, 600);
  11. form.FormBorderStyle = FormBorderStyle.Sizable;
  12. form.StartPosition = FormStartPosition.CenterScreen;
  13. form.Controls.Add(textBox);
  14.  
  15. Graphics g = form.CreateGraphics();
  16.  
  17. float targetWidth = 230;
  18.  
  19. foreach (PropertyInfo property in properties)
  20. {
  21. string text = String.Format("{0}:t", Description);
  22.  
  23. while (g.MeasureString(text,textBox.Font).Width < targetWidth)
  24. text += "t";
  25.  
  26. textBox.AppendText(text + value.ToString() + "n");
  27. }
  28.  
  29. g.Dispose();
  30. form.ShowDialog();
  31.  
  32. Private Declare Function SendMessage _
  33. Lib "user32" Alias "SendMessageA" _
  34. (ByVal handle As IntPtr, ByVal wMsg As Integer, _
  35. ByVal wParam As Integer, ByRef lParam As Integer) As Integer
  36.  
  37.  
  38. Private Sub SetTabStops(ByVal ctlTextBox As TextBox)
  39.  
  40. Const EM_SETTABSTOPS As Integer = &HCBS
  41.  
  42. Dim tabs() As Integer = {20, 40, 80}
  43.  
  44. SendMessage(ctlTextBox.Handle, EM_SETTABSTOPS, _
  45. tabs.Length, tabs(0))
  46.  
  47. End Sub
  48.  
  49. using System.Runtime.InteropServices;
  50.  
  51. private const int EM_SETTABSTOPS = 0x00CB;
  52. [DllImport("User32.dll", CharSet = CharSet.Auto)]
  53. public static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);
  54.  
  55. private void SetTabStops(TextBox ctlTextBox)
  56. {
  57. const int EM_SETTABSTOPS = 203;
  58. int[] tabs = { 100, 40, 80 };
  59. SendMessage(textBox1.Handle, EM_SETTABSTOPS, tabs.Length, tabs);
  60. }
  61.  
  62. private void Form1_Load(object sender, EventArgs e)
  63. {
  64. SetTabStops(textBox1);
  65.  
  66. textBox1.Text = "HitWorld";
  67. }
  68.  
  69. // This is a better way to pass in what tab stops I want...
  70. SetTabStops(textBox, new int[] { 12,120 });
  71.  
  72. // And the code for the SetTabsStops method itself...
  73. private const uint EM_SETTABSTOPS = 0x00CB;
  74.  
  75. [DllImport("User32.dll")]
  76. private static extern uint SendMessage(IntPtr hWnd, uint wMsg, int wParam, int[] lParam);
  77.  
  78. public static void SetTabStops(TextBox textBox, int[] tabs)
  79. {
  80. SendMessage(textBox.Handle, EM_SETTABSTOPS, tabs.Length, tabs);
  81. }
Add Comment
Please, Sign In to add comment