Fork me on GitHub

TextBox

 

TextBox与传统的TextBox没有多大差别,不过有了自己的新特性。下面通过三个例子来说明一下。

XAML代码:

<Grid x:Name="LayoutRoot">

                        <Grid.ColumnDefinitions>

                                    <ColumnDefinition Width="0.753*"/>

                                    <ColumnDefinition Width="0.247*"/>

                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>

                                    <RowDefinition Height="0.35*"/>

                                    <RowDefinition Height="0.291*"/>

                                    <RowDefinition Height="0.359*"/>

                        </Grid.RowDefinitions>

<TextBox

 x:Name="tbMultiLine"

 TextWrapping="Wrap"

 AcceptsReturn="True"

 VerticalScrollBarVisibility="Visible"

Margin="69,0,13,17.026" Text="This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed, or when typed text reaches the edge of the text box, a new line is automatically inserted."

IsEnabled="True" Visibility="Visible"/>

                        <Button Click="OnClick1" Margin="26,0,8,17.026" Content="Position" VerticalAlignment="Bottom" Height="27" Grid.Column="1"/>

                        <TextBox Name="gsw" Margin="69,0,13,23.077" Text="" TextWrapping="Wrap" RenderTransformOrigin="0.328,1.49" VerticalAlignment="Bottom" Height="75.016" Grid.RowSpan="2"/>

<TextBox

 Name="cxmTextBox"

 AcceptsReturn="True"

 AcceptsTab="True"

 VerticalScrollBarVisibility="Visible"

 TextWrapping="Wrap"

 Margin="69,97.066,13,9.924"

   Text="This TextBox uses a simple custom context menu. The context menu can be disabled by checking the CheckBox above, which simply sets the TextBox.ContextMenu property to null."

Grid.Row="1" Grid.RowSpan="2">

 <TextBox.ContextMenu>

    <ContextMenu

      Name="cxm"

      Opened="CxmOpened"    >

      <MenuItem

        Header="Cut"

        Name="cxmItemCut"

        Click="ClickCut"       />

      <MenuItem

        Header="Copy"

        Name="cxmItemCopy"

        Click="ClickCopy"       />

      <MenuItem

        Header="Paste"

        Name="cxmItemPaste"

        Click="ClickPaste"       />

      <Separator/>

      <MenuItem

        Header="Select All"

        Name="cxmItemSelectAll"

        Click="ClickSelectAll"       />

      <MenuItem

        Header="Select Current Line"

        Name="cxmItemSelectLine"

        Click="ClickSelectLine"       />

      <Separator/>

      <MenuItem

        Header="Undo Last Action"

        Name="cxmItemUndo"

        Click="ClickUndo"       />

      <MenuItem

        Header="Redo Last Action"

        Name="cxmItemRedo"

        Click="ClickRedo"       />

      <Separator/>

      <MenuItem

        Header="Clear All Text"

        Name="cxmItemClear"

        Click="ClickClear"       />

    </ContextMenu>

 </TextBox.ContextMenu>

</TextBox>

                        <Button Click="OnClick" Margin="35,47.99,8,23.077" Content="SelectedText" RenderTransformOrigin="0.489,0.447" Grid.Column="1" Grid.Row="1"/>

 

                        <TextBox HorizontalAlignment="Right" Margin="0,0,8,0" x:Name="gg" VerticalAlignment="Top" Width="53.181" Height="24" Grid.Column="1" Text="0" TextWrapping="Wrap"/>

                        <Label RenderTransformOrigin="0.529,-0.417" HorizontalAlignment="Left" VerticalAlignment="Top" Width="51" Height="24" Grid.Column="1" Foreground="#FFCD2D2D" Content="Postiton"/>

                        <TextBox x:Name="ww" Text="0" TextWrapping="Wrap" HorizontalAlignment="Right" Margin="0,28,8,0" VerticalAlignment="Top" Width="53.181" Height="24" Grid.Column="1"/>

                        <Label RenderTransformOrigin="0.529,-0.417" Width="68" Height="24" Foreground="#FFBD2D2D" Content="SelectNum" HorizontalAlignment="Right" Margin="0,28,65.181,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Background="#00120C2C"/>

            </Grid>

CS代码:

其中OnClick1是来定位光标,并从标处选 中的字符,其中Select 的第一个参数是光标开始的位置,第二个参数是要选 中的字符个数。Focus是焦点的设置。

void OnClick1(Object sender, RoutedEventArgs args)

        {

            tbMultiLine.Select(Convert.ToInt32(gg.Text), Convert.ToInt32(ww.Text));

            tbMultiLine.Focus();

        }

OnClick是把选中的字符赋值给gsw

        void OnClick(object sender, RoutedEventArgs e)

        {

            gsw.Text = tbMultiLine.SelectedText;

        }

 

下面是实对文本框的粘贴,拷贝,剪切,全选,清除,选中当前行,取消,重复,以及一些约定。更详细的内容请看MSDN上关于TextBox的例子。

        void ClickPaste(Object sender, RoutedEventArgs args)

        { cxmTextBox.Paste(); }

        void ClickCopy(Object sender, RoutedEventArgs args)

        { cxmTextBox.Copy(); }

        void ClickCut(Object sender, RoutedEventArgs args)

        { cxmTextBox.Cut(); }

        void ClickSelectAll(Object sender, RoutedEventArgs args)

        { cxmTextBox.SelectAll(); }

        void ClickClear(Object sender, RoutedEventArgs args)

        { cxmTextBox.Clear(); }

        void ClickUndo(Object sender, RoutedEventArgs args)

        { cxmTextBox.Undo(); }

        void ClickRedo(Object sender, RoutedEventArgs args)

        { cxmTextBox.Redo(); }

 

        void ClickSelectLine(Object sender, RoutedEventArgs args)

        {

            int lineIndex = cxmTextBox.GetLineIndexFromCharacterIndex(cxmTextBox.CaretIndex);

            int lineStartingCharIndex = cxmTextBox.GetCharacterIndexFromLineIndex(lineIndex);

            int lineLength = cxmTextBox.GetLineLength(lineIndex);

            cxmTextBox.Select(lineStartingCharIndex, lineLength);

        }

 

        void CxmOpened(Object sender, RoutedEventArgs args)

        {

            if (cxmTextBox.SelectedText == "")

                cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = false;

            else

                cxmItemCopy.IsEnabled = cxmItemCut.IsEnabled = true;

 

            if (Clipboard.ContainsText())

                cxmItemPaste.IsEnabled = true;

            else

                cxmItemPaste.IsEnabled = false;

        }

posted @ 2007-05-07 13:50  桂素伟  阅读(653)  评论(0编辑  收藏  举报