KeyDown preventing text appearing in TextBox [UWP] - c#

I have the current UWP app targeting 10240:
<Page x:Class="App8.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ContentControl KeyDown="ContentControl_KeyDown">
<TextBox TextChanged="TextBox_TextChanged"/>
</ContentControl>
</Grid>
</Page>
And:
namespace App8
{
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
private void ContentControl_KeyDown(object sender, KeyRoutedEventArgs e) => e.Handled = true;
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) => Debug.WriteLine("NEVER RUNNING CODE");
}
}
When I write in the textbox I want to avoid any key events going to the main screen. In order to do that I have the KeyDown in the parent element of the textbox, and I handle the event. But If I do that the textbox doesn't write anything.
I'd like to end any key events going in the ContentControl going to the Page, but allowing the textbox to work normally. Any ideas?

I'd like to end any key events going in the ContentControl going to the Page, but allowing the textbox to work normally. Any ideas?
For your requirement, you could make bool flag to tell main screen fire some events or not when the TextBox is focused or not.
private bool IsFocus;
private void MyTextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
IsFocus = true;
}
private void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
IsFocus = false;
}
Usage
public MainPage()
{
this.InitializeComponent();
Window.Current.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
}
private void Dispatcher_AcceleratorKeyActivated(Windows.UI.Core.CoreDispatcher sender, Windows.UI.Core.AcceleratorKeyEventArgs args)
{
if (IsFocus)
{
System.Diagnostics.Debug.WriteLine("Do Not Fire Your Event ");
return;
}
else
{
System.Diagnostics.Debug.WriteLine(" Fire Your Event ");
}
}

Related

Winui3 OnClosin in the single page

My app has multiple pages.
When I press the Close key in the toolbar, how can I detect the OnClosing event in the single page to avoid closing the app instead of going back to the MainWindow with "this.Frame.GoBack();" ?
I can only catch OnClosing on MainWindow
You could store a reference to the window in a property or field in your App.xaml.cs class as suggested here and then handle the Closing event of the window in the Page class something like this:
public sealed partial class MainPage : Page
{
private Window _parentWindow;
public MainPage()
{
this.InitializeComponent();
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
_parentWindow = (Application.Current as App)?.m_window;
if (_parentWindow != null)
_parentWindow.Closed += OnWindowClosed;
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_parentWindow != null)
_parentWindow.Closed -= OnWindowClosed;
}
private void OnWindowClosed(object sender, WindowEventArgs args)
{
// Prevent the window from being closed based on some logic of yours...
args.Handled = true;
}
}
You can use the Unloaded event on Pages.
*.xaml
<local:TestPage Unloaded="TestPage_Unloaded" />
*.xaml.cs
private void TestPage_Unloaded(object sender, RoutedEventArgs e)
{
// Do your page closing work here...
}

Make TextBox text disappear after mouse click

I'm new to C# so this is my first task essentially, I'm hoping to make a simple login page.
I would like the text to disappear from the textbox once it has been clicked, here's what I have tried so far;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool hasBeenClicked = false;
private void textBox1_Focus(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
box.Text = string.Empty;
hasBeenClicked = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
This is from a similar post on here, It doesn't seem to work for me.
Here is something else I have tried;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
I understand it may be a silly mistake, I'm learning.
Any helps is massively appreciated :)
I'm using Winforms
Try using the event MouseClick on the TextBox:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
this.textBox1.Text = "";
}
In your screenshot, you are missing a reference. Click into the textbox events and add it like so:
Both of which should probably work but you've got to link the event to the function. You can either do this in the Designer or in code. For the Mouse click variant to work and link the event in code you can add the following in the constructor:
public Form1()
{
InitializeComponent();
this.MouseClick += textbox1_MouseClick;
}
Add Event Handler
You have not linked the given methods with the event, as you can see 0 refereces for both functions. You can add a mouse Click event as:
textBox1.Click += new System.EventHandler(textbox1_Mouseclick);
Similarly, you have to add event if you want to do with the focus event.

Changing Text Binding on User Control fires text changed event but doesnt detect the new text

I have a custom WPF TextBox User Control. Im writing some content, but it always detects the same data that was preset on the XAML, new written text is not shown by doing textbox.Text get call.
public partial class SearchBox : UserControl
{
public SearchBox()
{
InitializeComponent();
this.DataContext = this;
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox && textBox.Tag.ToString() == textBox.Text)
textBox.Clear();
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (sender is TextBox textBox && string.IsNullOrEmpty(textBox.Text))
textBox.Text = textBox.Tag.ToString();
}
public string Text { get; set; }
public event TextChangedEventHandler TextChanged;
private void TextBox_TextChanged(object sender, TextChangedEventArgs args)
{
TextChangedEventHandler h = TextChanged;
if (h != null)
{
h(this, args);
}
}
}
How it needs to work:
<v:SearchBox x:Name="searchBox" TextChanged="searchBox_TextChanged" Text="Input a keyword to filter..." Tag="Input a keyword to filter..." HorizontalAlignment="Left"/>
User writes on textbox "test" --> textbox.Text returns "Input a keyword to filter..."
Thanks for the help!
Your class inherits from UserControl and not TextBox.
Though you haven't shared your XAML for SearchBox, I assume you have a TextBox in it, which you are hoping to get the text from. Problem is, the property SearchBox.Text is different from SearchBox.TextBox.Text. As a result, even if you change the text in the TextBox, SearchBox.Text remains the same.
Solution: use SearchBox.TextBox.Text for your result.

Avoid SizeChanged event on startup

I have a MainWindow Class which as a few Events - all of them should call a method in another class.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getdata.MainWindow = this;
}
private void button_Click(object sender, RoutedEventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void comboBox2_DropDownClosed(object sender, EventArgs e)
{
getdata go = new getdata();
go.clear();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) {
getdata go = new getdata();
go.clear(); //<-this causes exception on Startup
}
}
The Problem is that the MainWindow_SizeChanged Event is also triggered on Startup of the program but the clear method uses also some objects that are not yet created at Startup, which causes an error. How can I avoid this and only have this Event triggered when the size is actually changed while running the program?
You have the IsLoaded property of Window.
You can check it before executing code.
You can check if an item is null when re-sizing so you can avoid this being an issue, this can be done using the null coalescence and null conditional operators. If for example you are using the value of a textBox on startup but it has not been instantiated you can use this
string someText = tB1?.Text?
if(someText == null) return;
SizeChanged Event gets triggered before the window is loaded. You can try subscribing to the SizeChanged event as part of Loaded event and unsubscribe it in the Unloaded as shown below.
//XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded"
Unloaded="MainWindow_OnUnloaded">
//Code Behind
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
Debug.WriteLine("Size Changed Triggered");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SizeChanged += Window_SizeChanged;
}
private void MainWindow_OnUnloaded(object sender, RoutedEventArgs e)
{
SizeChanged -= Window_SizeChanged;
}

Raising an event from child control to parent control

I have a class (which extends Framework Element) which contains within it a number of other Elements.
// Click event coverage area
private Rectangle connectorRectangle;
These shapes all have their event handlers, and when the user clicks on them its working well. Now what I want is to be able to 'handle' a right-click on my class from outside the scope of the class.
So I figured the best way to do it is to handle the event internally, and somehow bubble it to the top
private void connectorRectangle_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
MouseButtonEventArgs args = new MouseButtonEventArgs();
//???
e.Handled = true;
}
The problem is that I have no idea how to raise the event. this.OnMouseRightButtonUp doesn't exist, and all the tutorials I'm finding are for raising custom events.
I'm pretty new to silverlight, so bear with me if I missed something obvious.
Try it :
public Rectangle
{
this.Click += new System.EventHandler(Function);
}
private void Function(object sender, System.EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Right)
{
//Your code
}
}
Your "exteded Framework Element class" shouldn't handel the mouse event (or if they handel them, set e.Handled to false). Then the event should bubble up automatically (without reraise the event).
EDIT
public class ExtendedFrameworkElement : Grid
{
public ExtendedFrameworkElement()
{
Border b1 = new Border();
b1.Padding = new Thickness(20);
b1.Background = Brushes.Red;
b1.MouseRightButtonUp += b1_MouseRightButtonUp;
Border b2 = new Border();
b2.Padding = new Thickness(20);
b2.Background = Brushes.Green;
b2.MouseRightButtonUp += b2_MouseRightButtonUp;
b1.Child = b2;
this.Children.Add(b1);
}
private void b1_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
private void b2_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//DoSomeThing
e.Handled = false;
}
}
Xaml:
<Window x:Class="WpfApplicationTest.MainWindow">
<wpfApplicationTest:ExtendedFrameworkElement MouseRightButtonUp="UIElement_OnMouseRightButtonUp"/>
</Window>
Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UIElement_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
//DoSomeThing
}
}

Categories

Resources