CHANDRA
CHANDRA

Reputation: 4938

how to highlight the currently typed text of textbox in wpf

public partial class MultiTexbox_2 : Window
{
    Control texbox_full_details = null;      //get all textbox property and method in when gotfocused
    Control button_full_details;             //get all button property and method in when click event
    Button keyboard_button;     //behave like button

    public MultiTexbox_2()
    {
        this.InitializeComponent();

        // Insert code required on object creation below this point.
    }

    private void btn_a_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;
        all_in_one();

        //var caretIndex = txt_diplay_1.CaretIndex;
        //txt_diplay_1.Text = txt_diplay_1.Text.Insert(caretIndex, btn_a.Content.ToString());
        //txt_diplay_1.Focus(); 
        //txt_diplay_1.CaretIndex = caretIndex + 1;    


    }

     private void btn_b_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;

        all_in_one();
    }



    private void btn_c_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;

        all_in_one();
    }

    private void txt_diplay_1_GotFocus(object sender, RoutedEventArgs e)
    {
        texbox_full_details = (Control)sender;           

    }

    private void txt_diplay_2_GotFocus(object sender, RoutedEventArgs e)
    {
        texbox_full_details = (Control)sender;
    }


    public void all_in_one()
    {
        keyboard_button = button_full_details as Button;
        if (texbox_full_details != null)
        {
            //TextBox tb = texbox as TextBox;
            //tb.Text += btn.Content;

            TextBox txt_box = texbox_full_details as TextBox;
            var caret_index = txt_box.CaretIndex;
            txt_box.Text = txt_box.Text.Insert(caret_index, keyboard_button.Content.ToString());
            txt_box.Focus();
            txt_box.CaretIndex = caret_index + 1;               
        }           

    }
}

It's output will be like this

result1

But need output like this

Result2

When click the button,it's content will bind in textbox.on that time the currently binded textbox text's background color,font color and font size should change.what should i do to get that kind of output.Please help me.

Upvotes: 1

Views: 596

Answers (2)

GameAlchemist
GameAlchemist

Reputation: 19294

Ok so you want that, on a given condition, your TextBox text Zoom.
So First create a class with two properties : EditedText and IsZoomed for instance :

public class ZoomableText
{
    public string EditedText { get; set; }
    public Boolean IsZoomed { get; set; }
}

Then use Xaml : use a style with a DataTrigger on IsZoomed, and change the text aspect you want when IsZoomed is true. You might declare this style inside your window resources or in your application resources. Example :

    <Style TargetType="TextBox" x:Key="LargerWhenFocusedTextBox">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontWeight" Value="Normal" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsZoomed}" Value="True">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="FontWeight" Value="Bold" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

To use it, just do something like :

     <StackPanel >
        <TextBox  Text="{Binding EditedText}" 
                  Style="{StaticResource LargerWhenFocusedTextBox}"  />
        <ToggleButton IsChecked="{Binding IsZoomed}" Content="Zoomed?" />
      </StackPanel >

Where you will set the DataContext of the StackPanel to an instance of a ZoomableText Object.

You might want to make the ZoomableText Object implements INotifyPropertyChanged on its properties.

For the curret change, handle the Checked event of the ToggleButton.

Note that if you don't give a Key to the style, it will be applied automatically to all your TextBoxes.

Upvotes: 1

kenny
kenny

Reputation: 22414

Look into SelectedText, SelectionStart and SelectionLength. http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.aspx

tb.SelectionStart = tb.Length - 1;
tb.SelectionLength = 1;

Upvotes: 1

Related Questions