简体   繁体   中英

How to bring DialogResult result on front

How to bring DialogResult on front. Below is my Form. i want to bring it to front of all other application run. But it is working in debug mode but not working in "Run without debug mode".

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace XYZ
{
    public partial class Form2 : Form
    {

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

        public Form2()
        {
            InitializeComponent();

        }
        static Form2 MsgBox; static DialogResult result = DialogResult.No;
        public static DialogResult Show( /*string Text, string Caption, string btnOK, string btnCancel */)
        {
                MsgBox = new Form2();
                MsgBox.FormBorderStyle = FormBorderStyle.None;
                result = DialogResult.No;
                MsgBox.TopMost = true;


                try
                {
                    SetForegroundWindow(MsgBox.Handle);
                    System.Media.SystemSounds.Beep.Play();
                    MsgBox.TopLevel = true;
                    MsgBox.ShowDialog(MsgBox.ParentForm);

                }
                catch (Exception ex)
                {

                    LogHelper.WriteFatalLog("Show", ex);
                }
                return result;

        }


        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void btnLoginF2_Click(object sender, EventArgs e)
        {
            Program.username = txtUserNameBtn.Text;
            Program.password = txtPassBtn.Text;


            result = DialogResult.Yes; MsgBox.Close();
        }
    }
}

i am executing like below

                try
                {
                    Util.TaskHide(0);
                    Util.KillCtrlAltDelete();
                    Util.KillTaskManager();
                    //  ShowWindow(hwnd,0);
                }
                catch (Exception ex)
                {

                //    MessageBox.Show(ex.Message);
                    LogHelper.WriteErrorLog("Hide Kill Manage", ex);
                }
                Form2.Show();
               // Program.mfLogin.Hide();
                try
                {
                    Util.TaskHide(1);
                    Util.EnableCTRLALTDEL();

                }
                catch (Exception ex2)
                {

                    MessageBox.Show(ex2.Message);
                    LogHelper.WriteErrorLog("Show Pamper Leave", ex2);
                }

Use Form's Activate() method as told here .

At the remarks it says; Activating a form brings it to the front if this is the active application.

Hope helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM