Set VerticalScrollBarVisibility for TextBox : TextBox Style « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.ComponentModel;        // for CancelEventArgs
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

    class MainClass : Window
    {
        static string strFileName = "c:\\a.txt";
        TextBox txtbox = new TextBox();
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new MainClass());
        }
        public MainClass()
        {
            txtbox.AcceptsReturn = true;
            txtbox.TextWrapping = TextWrapping.Wrap;
            txtbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            txtbox.KeyDown += TextBoxOnKeyDown;
            Content = txtbox;

            txtbox.Text = File.ReadAllText(strFileName);
            txtbox.CaretIndex = txtbox.Text.Length;
            txtbox.Focus();
        }
        protected override void OnClosing(CancelEventArgs args)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(strFileName));
                File.WriteAllText(strFileName, txtbox.Text);
            }
            catch (Exception exc)
            {
                MessageBoxResult result = 
                    MessageBox.Show("File could not be saved: " + exc.Message + 
                                    "\nClose program anyway?", Title, 
                                    MessageBoxButton.YesNo, 
                                    MessageBoxImage.Exclamation);

                args.Cancel = (result == MessageBoxResult.No);
            }
        }
        void TextBoxOnKeyDown(object sender, KeyEventArgs args)
        {
            if (args.Key == Key.F5)
            {
                txtbox.SelectedText = DateTime.Now.ToString();
                txtbox.CaretIndex = txtbox.SelectionStart + txtbox.SelectionLength;
            }
        }
    }








24.15.TextBox Style
24.15.1.Reuse Style for TextBoxReuse Style for TextBox
24.15.2.Change TextBox background and font size
24.15.3.Set VerticalScrollBarVisibility for TextBox
24.15.4.Change TextBox Margin
24.15.5.Add TextBox to default docking space