Message shown after text written - c#

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

Related

Show text when you press a button

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";

Moving cursor to next textbox automatically in UWP

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);
}
}

WPF - Need to drag in text from notepad and then call a method

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();

Global event handler for TextBox getting focus by mouse click

I have a situation when I want to detect when a TextBox, anywhere in the application, has been brought into focus by the user clicking on it with the mouse, or touch. I have "solved" this by adding a global event handler like this:
Application.Current.MainWindow.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(txt_MouseLeftButtonUp), true);
...
void txt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is TextBox)
{
// Do somthing
}
}
However, if the user clicks the edges of a textbox instead of in the middle, quite often an hosting control (Grid, Border etc.) receives the mouse event and somehow passes this on to the contained TextBox, that will handle it and receive focus. This makes the approach above fruitless as the TextBox will not be the e.OriginalSource in this case and I have found no way of identifying that a TextBox was brought into focus by click.
Deriving TextBox and overriding OnMouseDown for instance, will catch this event and I guess this path could be explored to find a solution to the problem but that would require me to use a custom TextBox everywhere.
Anyone out there with a nice solution for this?
This is an example that will trigger the problem. By clicking the edges of the TextBoxes, the grid will handle the mouse event and focus will be passed on to the TextBox.
<Grid>
<Grid HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red">
<TextBox>2323</TextBox>
</Grid>
<Grid Margin="200,0,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red" Focusable="False">
<TextBox>2323</TextBox>
</Grid>
</Grid>
The GotMouseCapture event seems to work:
AddHandler(UIElement.GotMouseCaptureEvent,
new MouseEventHandler(OnGotMouseCapture), true);
...
void OnGotMouseCapture(object sender, MouseEventArgs e)
{
if (e.OriginalSource is TextBox)
{
// ...
}
}
When I click on TextBox elements with the mouse this event handler is fired, however focus changes made via keyboard do not fire the event.
Simply handle the GotFocus event for each TextBox.

Can you use richtextbox selectionchanged to look for doubleclick event?

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

Categories