KARTHI SRV KARTHI SRV - 2 months ago 21
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);

base.OnStartup(e);
}

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;
textbox.Focus();
}
}
}


I got error in:


onstartup() method- method cannot be override

Answer

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

<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml" Startup="Application_Startup">

    <Application.Resources>

    </Application.Resources>
</Application>

In App.xaml.cs

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