I am trying to find a way to get the selected text of a richtextbox once a word is double clicked. The richtextbox is setup like :
<RichTextBox x:Name="rtbStoryLine" Grid.Column="1" Grid.Row="3" Grid.RowSpan="4" Margin="0,15,16,17" FontFamily="Arial" FontSize="25" IsReadOnly="True" MaxWidth="700" MaxHeight="130" ToolTipService.ToolTip="This box displays the selected line below." SelectionChanged="rtbStoryLine_SelectionChanged" />
I add text to the code behind like:
public void AddTextt(string text2Add)
{
Paragraph p = new Paragraph();
Run r = new Run();
r.Text = text2Add;
p.Inlines.Add(r);
rtbStoryLine.Blocks.Clear();
rtbStoryLine.Blocks.Add(p);
}
In the selection change I want to check if there was a double click and then do something with the highlighted word.
I would recommend to use mouse DoubleClick event and then compare the selection with the previous one stored in some variable. It seems more reasonable than trying to detect DoubleClick event from SelectionChanged. Rgds, AB
Related
I would like to know a way to make some text appear when you press a button.
I've already created a button and a text box:
<Button Height="25" Width="200" Click="Button_Click" Content="Press this button"/>
<TextBlock Height="50" Width="300" Margin="243,147,249,222" TextAlignment="Center" FontSize="30"/>
Also, I've created a boolean for when you press the button, I don´t know if this is necessary or not.
private void Button_Click(object sender, RoutedEventArgs e)
{
Boolean button = true;
}
Basically, like MaxB said, every control in WPF has a "Visibility" property, that you can change between Visible, Collapsed or Hidden.
Since you already have a Handle for the Button_Click event, all you need to do now is give a name to your TextBlock with the x:Name property like-so :
<TextBlock x:Name="MyTextBlock"/>
Then, in the code of your handler, you can choose which Visibility to apply to the TextBlock according to the state of your boolean.
You can access the TextBlock properties by the name you gave it in the XAML file, like-so :
this.MyTextBlock.Visibility = Visibility.Hidden, for example.
You didn't create a textbox you created a textblock. Firstly create a textbox and give it a name. Then on your Button_click method you can write NameOfTextBox.Text = "Your text";
I have 8 small textboxes with a maxlength of 1, and I want to make it so when somebody types a character in one of the boxes it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp
Is there an easy way to do this?
it automatically moves the cursor to the next box without the user having to press tab or manually click the next box in win 10 UWp Is there an easy way to do this?
The Focus method would be that you want. Pass FocusState.Keyboard as the parameter if you’re setting focus as a result of a keyboard interaction, like a tab sequence or key press.
I have made a simple code sample for your reference:
<StackPanel Orientation="Horizontal" Height="50">
<TextBox x:Name="txb1" MaxLength="1" Width="20" TextChanged="txb_TextChanged"></TextBox>
<TextBox x:Name="txb2" MaxLength="1" Width="20" TextChanged="txb_TextChanged" Margin="10 0 0 0"></TextBox>
</StackPanel>
private void txb_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = sender as TextBox;
if (!string.IsNullOrEmpty(textbox.Text.Trim()))
{
txb2.Focus(FocusState.Keyboard);
}
}
I have a textbox on a WPF app that I want to be able to drag text into from an outside application (notepad++). The PreviewDrop handler works great but when I go to retrieve the text from the text box after the handler is called (myTxtBox.Text) all I get is an empty string. As soon as i drop the text into the textbox, i want the handler to fire and call another method that will do something with the text input that is being dropped. but its firing too fast. any suggestions?
my handler method
private void myTxtBox_Drop(object sender, DragEventArgs e)
{
string itemID = myTxtBox.Text.Trim();
}
xaml of the text box
<TextBox x:Name="myTxtBox" Height="23" Margin="5,61,5,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="1" LostFocus="btnLookup_Click" Drop="myTxtBox_Drop" KeyDown="myTxtBox_KeyDown" AllowDrop="True" PreviewDrop="myTxtBox_Drop" />
Try this to get your dropped text:
string itemID = e.Data.GetData(DataFormats.Text).ToString().Trim();
I had an textbox in which user can enter text and there was an list box below the text box which shows the collections of objects of a class. When user selects any one of the list box item i am displaying the list box item in the textbox using the text property in the selected event of the list box. Now my concern here is i want to make the selected list box item as hyperlink in textbox which is clickable same as like in message composer in windows phone. And user can continue typing the text in the textbox after item was selected to select the next list box item. Can any one help me to find the solution.
yea its clear now. Can you checkthis link 'http://www.jayway.com/2011/10/05/wp7-link-in-text-with-richtextbox-on-mango/'
Hope this will help you. You can try this way.
<TextBlock x:Name="tbref" Visibility="Collapsed"/>
<RichTextBox >
<RichTextBox.Template>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<HyperlinkButton x:Name="txtnam" Content="{Binding Text,ElementName=tbref}" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox Width="200"/>
</StackPanel>
</ControlTemplate>
</RichTextBox.Template>
</RichTextBox>
selection event handler
private void lst_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
var selectedString = lst.SelectedItem;
tbref.Text = selectedString.ToString();
}
I have a textbox that after I write text in that textbox I want to show a message ("you have typed text"). However I want to show this WITHOUT button_click event. I thought a textchanged event would work (using WPF). However I cannot write text before message shows.
I feel like I need a on_click event or something like this? However how would I show the message in just that textbox AFTER the text is written ? So only time I see that message is after I type text in that textbox.
Here's how your textbox definition should look like
<TextBox Grid.Column="1" Grid.Row="3" Height="25" HorizontalAlignment="Left" Margin="250,1,0,0" x:Name="T1"
VerticalAlignment="Top" Width="115" LostFocus="T1_LostFocus" />
Show the messagebox in the T1_lostFocus
go to the text box property and under the properties there will be a lightning bolt(events) click on it and click on the TextChanged property, so you will have:
private void textbox_TextChanged (blah, blah)
{
timer1.enabled = true;
timer1.interval = 1000
}
private void timer1_tick (blah, blah)
{
MessageBox.Show("Text");
}
when the user changes text, it will trigger a timer, and after not typing for 1 second it will trigger a message