KARTHI SRV KARTHI SRV - 1 year ago 83
C# Question

Got focus Event to handle all textbox

This is my coding:

public partial class App : Application
protected override void OnStartup(StartupEventArgs e)
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(SelectivelyHandleMouseButton), true);
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent,
new RoutedEventHandler(SelectAllText), true);


private static void SelectivelyHandleMouseButton(object sender, MouseButtonEventArgs e)
var textbox = (sender as TextBox);
if (textbox != null && !textbox.IsKeyboardFocusWithin)
if( e.OriginalSource.GetType().Name == "TextBoxView" )
e.Handled = true;

I got error in:

onstartup() method- method cannot be override

Answer Source

In App.xaml you need to subscribe to event Startup:

<Application x:Class="WpfApplication1.App"
                 StartupUri="MainWindow.xaml" Startup="Application_Startup">



In App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
    // Your code here