using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.ComponentModel;
using System.IO;
namespace WpfApplication1
{
public class EditSomeText : Window {
static string strFileName = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData),
"petzold\\editsometext\\aa.txt");
// static string strFileName = Path.Combine(@"e:\11", "aa.txt");
TextBox txtbox;
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new EditSomeText());
}
public EditSomeText()
{
Title = "edit some text";
txtbox = new TextBox();
txtbox.AcceptsReturn = true;
txtbox.TextWrapping = TextWrapping.Wrap;
txtbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
txtbox.KeyDown+=new KeyEventHandler(txtbox_KeyDown);
Content = txtbox;
try
{
txtbox.Text = File.ReadAllText(strFileName);
}
catch{
}
txtbox.CaretIndex = txtbox.Text.Length;
txtbox.Focus();
}
protected override void OnClosing(CancelEventArgs e)
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(strFileName));
File.WriteAllText(strFileName,txtbox.Text);
}
catch(Exception exc) {
MessageBoxResult result = MessageBox.Show(
"file could be saved: "+exc.Message+
"\n close program anyway?",Title,
MessageBoxButton.YesNo,
MessageBoxImage.Exclamation);
e.Cancel=(result==MessageBoxResult.No);
}
}
void txtbox_KeyDown(object sender,KeyEventArgs args)
{
if(args.Key==Key.F5){
txtbox.SelectedText = DateTime.Now.ToString();
txtbox.CaretIndex = txtbox.SelectionStart +
txtbox.SelectionLength;
}
}
}
}