如何在消息框上显示焦点 [英] How to show the focus on message box

查看:69
本文介绍了如何在消息框上显示焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#创建了基于Windows窗体的应用程序。



我在主窗体上有一个取消按钮。

当用户选择取消按钮时,它会显示一个消息框(是/否)。

I have created Windows Form based application using c#.

I have a button which is 'Cancel' on Main Form.
When user selects 'Cancel' button, it displays a message box (yes/No).

<pre lang="c#">MessageBox.Show(this,"Are you sure to exit the application", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);





直到用户选择'是'或'否'消息框仍然出现在主窗体上。



现在,如果用户从任务栏中选择X关闭窗口,焦点将从消息转移框到主窗体。



如何在上面的场景中显示关注消息框。



你可以请任何人帮忙。



提前谢谢。



Until user selects either 'Yes' or 'No' message box still appear on the main form.

Now, if user selects 'X' Close window from Task bar, focus is shifting from message box to Main form.

How to show the focus on message box in the above scenario.

Could you please anyone help on this.

Thanks in advance.

推荐答案

这段代码(VB。 NET,我知道)应该像你想要的那样工作:

This code (VB.NET , I know) should work like you want :
Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
    Dim myResult As DialogResult = MessageBox.Show("FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)

    If myResult = Windows.Forms.DialogResult.No Then
        e.Cancel = True
    End If
    MyBase.OnFormClosing(e)
End Sub





更新以获取MessageBox ALLWAYS TopMost:




Update to get the MessageBox ALLWAYS TopMost :

Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
    Dim myResult As DialogResult = MessageBox.Show(Me, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)

    If myResult = Windows.Forms.DialogResult.No Then
        e.Cancel = True
    End If
    MyBase.OnFormClosing(e)
End Sub


当我尝试这个时(已修复你的代码所以它编译):

When I try this (having fixed your code so it compiles):
DialogResult result = MessageBox.Show(this, "Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);



Or

DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);

结果是一样的:你不能按X按钮关闭对话框,它显示为灰色。

并试图点击主窗体上的X按钮也不起作用 - 因为对话框是模态的,主窗体在关闭之前不会响应。



那究竟是什么你有不同的做法 - 因为我怀疑你的代码看起来不像你给我们看的......



我认为他点击了任务栏提供微小红色X以从外面关闭应用程序的图标







AH!



在这种情况下,它很简单:处理FormClosing事件并显示消息框:您可以通过FormClosingEventArgs取消关闭。取消财产





The result is the same: you can't press the "X" button to close the dialog, it's greyed out.
And trying to click the "X" button on the main form doesn't work either - because the dialog box is modal and the main form won't respond until it's closed.

So what exactly are you doing differently - because I suspect your code doesn't look like what you showed us...

"I think he is clicking on the taskbar icon that offers tiny red X to close the app "from outside""

"yes"


AH!

In which case, it's simple: handle the FormClosing event and show the message box from that: you can cancel the close via the FormClosingEventArgs.Cancel property


"

I have tried that. But, issue is still exist.
In 'Cancel' button event, i have added FormClosingEventArgs.Cancel= false;
Then In Form Closing event, added the Message box to display.

When i clicked on cancel button, message box displayed. Then again i have clicked on task bar icon close window, focus is moving from message box to Main Window.







我自己尝试修改现有的app:


"


I tried it myself by modding an existing app:

/// <summary>
/// Save the size and location (if the used doesn't
/// override it)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    DialogResult result = MessageBox.Show("Please confirm", "Are you sure to exit the application", MessageBoxButtons.YesNo);
    if (result != DialogResult.Yes)
        {
        e.Cancel = true;
        return;
        }
    if ((ModifierKeys & Keys.Shift) == 0)
        {
        this.SaveLocation();
        }
    }



我运行应用程序,将鼠标悬停在任务栏上并单击X - 出现消息框。

如果我再次将鼠标悬停在任务栏上,我会得到两个预览窗口,我可以从对话框中将焦点切换到主窗体但我无法用它做任何事情 - 如果我尝试,我得到的只是一个默认的嘟嘟声。



这是我怀疑的Windows的功能,没有真正的方法可以摆脱它。



所以...你必须绕着它工作。

在这里和我一起生活......



首先在项目中添加一个新表单 - 称之为MyMessageBox

设置标题,添加标签以提示用户关闭它,并添加两个按钮:调用一个butYes,另一个butNo并适当地设置文本。将每个按钮的DialogResult属性分别设置为是和否。

将表单属性AcceptButton设置为butYes,将CancelButton设置为butNo。

将表单属性ShowInTaskBar设置为false,将StartPosition设置为CenterParent。



现在,返回主表单,添加两个私有类变量:


I run the app, hover the mouse over the taskbar and click "X" - the message box appears.
If I then hover the mouse over the taskbar again, I get two preview windows and I can switch the focus to the "main" form from the dialog box but I can't do anything with it - All I get is a default beep if I try.

This is a "feature" of windows I suspect, and there is no real way to get rid of it.

So...You will have to work round it.
Bear with me here...

Start by adding a new form to your project - call it MyMessageBox
Set the caption, add a label to prompt the user to close it, and add two buttons: call one "butYes", the other "butNo" and set the text appropriately. Set the DialogResult property for each button to "Yes" and "No" respectively.
Set the form properties "AcceptButton" to "butYes", and "CancelButton" to "butNo".
Set the form properties "ShowInTaskBar" to "false", and "StartPosition" to "CenterParent".

Now, back to your main form, and add two private class variables:

private bool checkClosing = false;
private MyMessageBox closeConfirm = null;



更改FormClosing事件处理程序:


Change your FormClosing event handler:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    checkClosing = true;
    closeConfirm = new MyMessageBox();
    DialogResult result = closeConfirm.ShowDialog();
    checkClosing = false;
    closeConfirm = null;
    if (result != DialogResult.Yes)
        {
        e.Cancel = true;
        return;
        }
    }

(从技术上讲,你只需要closeConfirm变量,但是......)

现在添加一个新的表单事件处理程序:已激活

(Technically, you only need the closeConfirm variable, but...)
Now add a new form event handler: Activated

private void frmMain_Activated(object sender, EventArgs e)
    {
    if (checkClosing && closeConfirm != null)
        {
        closeConfirm.Activate();
        }
    }



编译并试用。


Compile and try it.


这篇关于如何在消息框上显示焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆