Download TextBoxTabStops.zip, last updated 25/05/2019 (56.09 KB)

Download
  • md5: c19ff63a08cba2440f23c149be49025d
  • sha1: e41fa65000e766cd66ed012744340b6fe53df4b4
  • sha256: d3b220e4c5e4753d7b761ee0eb978600bdc14f6a9bbee50117e66a1a052d642c
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using FileInfo = Cyotek.Demo.ExifOrientation.FileInfo;

// Setting tab stops in a Windows Forms TextBox control
// https://www.cyotek.com/blog/setting-tab-stops-in-a-windows-forms-textbox-control

// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.

// Found this example useful?
// https://www.paypal.me/cyotek

namespace Cyotek.Demo.TextBoxTabStops
{
  public partial class MainForm : Form
  {
    #region Private Fields

    private readonly char[] _separators = { ',', ' ' };

    #endregion Private Fields

    #region Public Constructors

    public MainForm()
    {
      this.InitializeComponent();
    }

    #endregion Public Constructors

    #region Protected Methods

    protected override void OnShown(EventArgs e)
    {
      base.OnShown(e);

      string path;
      string[] files;

      path = Path.Combine(Application.StartupPath, "data");
      files = Directory.GetFiles(path);

      filesListBox.BeginUpdate();

      for (int i = 0; i < files.Length; i++)
      {
        filesListBox.Items.Add(new FileInfo(files[i]));
      }

      filesListBox.EndUpdate();

      if (filesListBox.Items.Count > 0)
      {
        filesListBox.SelectedIndex = 0;
      }
    }

    #endregion Protected Methods

    #region Private Methods

    private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      AboutDialog.ShowAboutDialog();
    }

    private void AutoDetectButton_Click(object sender, EventArgs e)
    {
      int[] tabStops;

      tabStops = TextBoxExtensions.AutoDetectTabStops(textBox.Text);

      textBox.SetTabStops(tabStops);

      // doesn't seem to be possible to get the tabstops from an 
      // existing edit control, so manually update the ruler with
      // the same information so it can build a little preview
      ruler.SetTabStops(tabStops);
    }

    private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void FilesListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (filesListBox.SelectedIndex != -1)
      {
        this.OpenFile(((FileInfo)filesListBox.SelectedItem).FullPath);
      }
    }

    private void OpenFile(string fileName)
    {
      try
      {
        textBox.Text = File.ReadAllText(fileName);
      }
      catch (Exception ex)
      {
        textBox.Text = ex.GetBaseException().Message;
      }
    }

    private void ResetButton_Click(object sender, EventArgs e)
    {
      textBox.ResetTabStops();

      // doesn't seem to be possible to get the tabstops from an 
      // existing edit control, so manually update the ruler with
      // the same information so it can build a little preview
      ruler.ResetTabStops();
    }

    private void SetButton_Click(object sender, EventArgs e)
    {
      int[] tabStops;

      tabStops = tabStopsTextBox.Text.Split(_separators, StringSplitOptions.RemoveEmptyEntries).Select(value => int.Parse(value)).ToArray();

      if (tabStops.Length > 0)
      {
        textBox.SetTabStops(tabStops);

        // doesn't seem to be possible to get the tabstops from an 
        // existing edit control, so manually update the ruler with
        // the same information so it can build a little preview
        ruler.SetTabStops(tabStops);
      }
    }

    private void SetFixedButton_Click(object sender, EventArgs e)
    {
      if (int.TryParse(fixedTextBox.Text, out int tabStop))
      {
        textBox.SetTabStops(tabStop);

        // doesn't seem to be possible to get the tabstops from an 
        // existing edit control, so manually update the ruler with
        // the same information so it can build a little preview
        ruler.SetTabStops(tabStop);
      }
    }

    private void WebLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      try
      {
        Process.Start("https://www.cyotek.com");
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.GetBaseException().Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    #endregion Private Methods
  }
}

Donate

Donate