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;
}
}
}
|