Search code examples
c#winformsadd-oncoreldraw

C# WinForms - Can't type some keys in TextBox (CorelDraw AddOn)


I'm not getting success in solving a strange behavior with a TextBox input in C#.

Enviroment

The project was created using a Control AddOn, this kind of project create a button in CorelDraw menu bar like this Buttons in right side

Clicking on this button, I can open a new Window to manipulate and control CorelDraw functionalities, automate tasks, etc.

https://marketplace.visualstudio.com/items?itemName=bonus630.CorelDrawDockerTemplate

Problem

When I click the button and open a new window containing a TextBox, using method .Show() strangely enough the TextBox not work properly, only numbers and specific letters can be inserted, I can't even delete the content pressing backspace.

But if I open the Window using the method .ShowDialog() everything works fine and I can type anything in TextBox! But I can't block user interaction (ShowDialog does it) with the window placed behind (CorelDraw window), the user need to access the CorelDraw window withou closing the AddOn when they need.

Could anyone please tell me what I missing? Or explain why this happens? I really appreciate any help.

The Codes

ControlUI.xaml

<!-- Basic structure to create a button in CorelDraw menu bar (came with the extension) -->
<UserControl x:Class="MaisUmaTentativa.ControlUI"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MaisUmaTentativa"
             mc:Ignorable="d" 
             Height="24" Width="24" Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/Colors.xaml"/>
                <ResourceDictionary Source="Styles/Styles.xaml"/>
                <ResourceDictionary Source="Resources/Images.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <!--button in menu bar goes here-->
        <Button Content="A" Name="btn_Command" >
            <Button.ToolTip>
                <ToolTip>
                    <StackPanel>
                        <Label Content="Enter Caption" FontWeight="Bold" FontSize="12"/>
                        <Label Content="Click ME!" FontSize="11"/>
                    </StackPanel>
                </ToolTip>
            </Button.ToolTip>
        </Button>
    </Grid>
</UserControl>

ControlUI.xaml.cs (Code Behind)

using ...; // removed here to reduce code
using corel = Corel.Interop.VGCore;

namespace MaisUmaTentativa
{
    public partial class ControlUI : UserControl
    {
        private corel.Application corelApp;
        private Styles.StylesController stylesController;

        public ControlUI(object app)
        {
            InitializeComponent();
            try
            {
                this.corelApp = app as corel.Application;
                stylesController = new Styles.StylesController(this.Resources, this.corelApp);
            }
            catch
            {
                global::System.Windows.MessageBox.Show("VGCore Erro");
            }

            // handle click in button placed at menu bar
            // MainWindow is a WinForm with a TextBox and a Label
            btn_Command.Click += (s, e) => {
                MainWindow mainWindow = new MainWindow();
                mainWindow.ShowDialog();
            };
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            stylesController.LoadThemeFromPreference();
        }

    }
}

Solution

  • As @Jimi said, the problem really was the message loop not started in my MainWindow that is a Form.

    I just placed the Application.Run(this); inside the constructor of MainWindow form, and the TextBox bug disappeared. Thanks for the help 🎈