代码改变世界

跟着小王学wpf之六 常用控件TextBox

2011-04-04 00:27  Wang_top  阅读(437)  评论(0)    收藏  举报

Textbox

虽然textbox含有内容,但是它一般不被认为是ContentContorl,因为他的内容一定是文字,它具有一个text属性,让你设定内容的文字。因为textbox继承与Contorl所有可以设置前景画刷和背景画刷字型等,但是他所有的字的格式都是一样的。Richtextbox则可以有多重状态的显示,他俩之间的区别就相当于记事本和word之间的区别一样。

下面我们建立一个程序来使用textbox:

class MyDiolog:Window

    {

        TextBox txt;

        public MyDiolog()

        {

            Title = "Enter a URI";

            ShowInTaskbar = false;

            SizeToContent = SizeToContent.WidthAndHeight;

            WindowStyle = WindowStyle.ToolWindow;

            WindowStartupLocation = WindowStartupLocation.CenterOwner;

 

            txt = new TextBox();

            txt.Margin = new Thickness(48);

            Content = txt;

            txt.Focus();

        }

        public string Text

        {

            set

            {

                txt.Text = value;

                txt.SelectionStart = txt.Text.Length;

            }

            get

            {

                return txt.Text;

            }

        }

        protected override void OnKeyDown(KeyEventArgs e)

        {

            if (e.Key == Key.Enter)

            {

                Close();

            }

            //base.OnKeyDown(e);

        }

    }

这个程序是存储输入的对话框类,它使用了textbox类。

class MyWindow:Window

    {

        Frame frm;

        public MyWindow()

        {

            Title = "Sharp of Button";

            frm = new Frame();

            Content = frm;

            Loaded += new RoutedEventHandler(MyWindow_Loaded);

        }

 

        void MyWindow_Loaded(object sender, RoutedEventArgs e)

        {

            MyDiolog md = new MyDiolog();

            md.Owner = this;

            md.Text = "http://";

            md.ShowDialog();

            try

            {

                frm.Source = new Uri(md.Text);

            }

            catch (System.Exception ex)

            {

                MessageBox.Show(ex.Message, Title);

            }

        }

    }

这是主窗口类,它在load方法中加载这个对话框,并且根据输入的网址,在主窗口中打开这个网站,当然主窗口中用到了Frame类。

下面我们使用RichTextBox:

class MyWindow:Window

    {

        RichTextBox txt;

        [STAThread]

        static void Main(string[] args)

        {

            Application app = new Application();

            app.Run(new MyWindow());

        }

        public MyWindow()

        {

            Title = "RichText Editer";

            txt = new RichTextBox();

            txt.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;

            txt.Background = new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1));

            Content = txt;

            txt.Focus();

        }

    }

可以看到这个是一个简单的txt,他能接收键盘输入,换行等操作。我们要是想响应一些特殊的键盘消息,就需要自己重写窗口的程序,为什么呢,因为在这个程序中richtextbox会讲所有的事件都接收,如果你仅仅在它的keydown事件中添加自己的响应,是无法收到消息的,因为他自己会接受所有的keydown消息,而不会去触发keydown事件。

键盘鼠标等输入设备触发的事件会首先在窗口上发生,以preview事件的形式出现。所以要抢在Richtextbox之前,将窗口的Onpreviewkeydown覆盖掉(override)。可以在override的版本中将我们感兴趣的输入进行处理,并且将这个事件的句柄设置为true 。表明已经处理了这个事件,不需要再送给Richtextbox进行处理了。

  protected override void OnPreviewTextInput(TextCompositionEventArgs e)

        {

            if (e.ControlText.Length>0&&e.ControlText[0]=='\x0F')//“Ctrl”+“O”

            {

                OpenFileDialog dlg = new OpenFileDialog();

                dlg.CheckFileExists = true;

                dlg.Filter = strFilter;

                if ((bool)dlg.ShowDialog((this)))

                {

                    FlowDocument flow = txt.Document;

                    TextRange rangle = new TextRange(flow.ContentStart, flow.ContentEnd);

                    Stream strm = null;

                    try

                    {

                        strm = new FileStream(dlg.FileName, FileMode.Open);

                        rangle.Load(strm, DataFormats.Xaml);

                    }

                    catch (System.Exception ex)

                    {

                        MessageBox.Show(ex.Message, Title);

                    }

                    finally

                    {

                            if (strm!=null)

                            {

                                strm.Close();

                            }

                    }

                }

                e.Handled = true;

            }

            base.OnPreviewTextInput(e);

        }