0

我正在开发一个 Compact Framework 应用程序。这种特殊的硬件实现有一个触摸屏,但它的软输入面板的按钮太小而无法使用。需要输入输入的表单不止一种,因此我创建了一个表单,其中的按钮布局类似于键盘。使用此“键盘”表单的表单是模态对话框。当加载需要此“键盘”的对话框时,我将“键盘”表单加载为无模式:

    private void CardInputForm_Load(object sender, EventArgs e)
    {
        ...
        keypadForm = new KeypadForm();
        keypadForm.Owner = this;
        keypadForm.SetCallback(keyHandler);
        keypadForm.Show();
    }

SetCallback 方法告诉“键盘”表单将击键发送到哪里(作为委托)。我遇到的问题是无模式的“键盘”表单不接受输入。它按我预期的方式显示,但是当我按下它的任何按钮时都会发出哔哔声,并且它的标题是灰色的。似乎模态对话框正在阻止它。我读过这个论坛上的其他帖子,说模式对话框可以创建和使用无模式对话框。任何人都可以阐明这种情况吗?我的实施有问题吗?

4

1 回答 1

0

I found the answer: Set the keypad form's Parent property, not its Owner property, to the form instance wanting the keystrokes. The keypad dialog's title bar stays grayed out, but the form is active.

    private void CardInputForm_Load(object sender, EventArgs e)
    {
        // (do other work)
        keypadForm = new KeypadForm();
        keypadForm.Parent = this;
        keypadForm.Top = 190;         // set as appropriate
        keypadForm.Show();
    }

Be sure to clean up when done with the parent form. This can be in the parent's Closing or Closed events.

    private void CardInputForm_Closing(object sender, CancelEventArgs e)
    {
        // (do other work)
        keypadForm.Close();
        keypadForm.Dispose();
    }

There are two panels on the keypad form, one with numerals and one with letters and punctuation that I want. There is also an area not on a panel that is common to both, containing buttons for clear, backspace, enter/OK, and cancel. Each panel has a button to hide itself and unhide its counterpart ('ABC', '123', for example). I have all the buttons for input on the keypadForm fire a common event. All it does is send the button instance to the parent. The parent is responsible for determining what action or keystroke is desired. In my case I named the buttons "btnA", "btnB", "btn0", "btn1", "btnCancel", etc. For keystrokes, the parent form takes the last character of the name to determine what key is desired. This is a bit messy but it works. Any form wishing to use the keypad form inherits from a base class, defining a method for callback.

    public partial class TimeClockBase : Form
    {
        public TimeClockBase()
        {
            InitializeComponent();
        }

        // (other implementation-specific base class functionality)

        public virtual void KeyCallback(Button button)
        {
        }
    }

The click event on the keypad form looks like this.

    private void btnKey_Click(object sender, EventArgs e)
    {
        // play click sound if supported
        (Parent as TimeClockBase).KeyCallback(sender as Button);
    }

The method in the parent form looks like this.

    public override void KeyCallback(Button button)
    {
        switch (button.Name)
        {
            case "btnCancel":
                // setting result will cause form to close
                DialogResult = DialogResult.Cancel;
                break;
            case "btnClear":
                txtCardID.Text = string.Empty;
                break;
            // (handle other cases)
        }
    }
于 2013-06-10T23:22:14.433 回答