0

I dont know if this works but is there any chance to move the cursor inside a Textbox automaticaly for one position(1 space bar click). For example: I run the application and the cursor inside the Textbox moves automatically for one space bar

my textbox:

<TextBox TextChanged="Searchbox_TextChanged" x:Name="Testing" Margin="2" Grid.Row="1" Text="{Binding SearchSmthg, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" VerticalContentAlignment="Center" Controls:TextboxHelper.ClearTextButton="True" Controls:TextboxHelper.Watermark="Search ..."/>
user655762
  • 45
  • 3
  • 8
  • use `CaretIndex`, e.g. ``: http://stackoverflow.com/a/2892988/352101 – Bolu Jan 15 '13 at 11:37
  • It is a bit unclear, do you want to move the cursor in a predefined string or do you want to insert a space in the textbox? – Tinsa Jan 15 '13 at 11:38
  • Sorry, i want to insert a space, but without clicking the space bar – user655762 Jan 15 '13 at 11:47
  • Do you want to have a space in the actual text? Or just some visual padding? If you just want Padding use `Padding="3,0,0,0"` – RoelF Jan 15 '13 at 15:41

1 Answers1

0

use CaretIndex and FocusManager:

  <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid FocusManager.FocusedElement="{Binding ElementName=Focustext}">
            <TextBox x:Name="Focustext" Text=" " CaretIndex="1" MaxHeight="25"  />
        </Grid>
    </Window>

Edit: add a GotFocus event handler and set CaretIndex=1 there

XAML:

<TextBox GotFocus="Testing_GotFocus"

Code Behind:

private void Testing_GotFocus(object sender, RoutedEventArgs e)
        {
            //make sure your Textbox is not empty..
            Testing.CaretIndex = 1;
        }
Community
  • 1
  • 1
Bolu
  • 8,696
  • 4
  • 38
  • 70