-1

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);
                }
MD TAHMID HOSSAIN
  • 1,581
  • 7
  • 29
  • 54
  • Stop writing such bad code that try to hack the system and is way more complicate than necessary and don't even properly dispose objects... – Phil1970 Aug 27 '16 at 14:53
  • Generally, it is a bad design to put a form in front of other applications. You should avoid writing applications that do not respect the system and users. – Phil1970 Aug 27 '16 at 14:55
  • chill bro, it is client's demand. – MD TAHMID HOSSAIN Aug 27 '16 at 18:30
  • Tell your client that Microsoft Windows does not work that way. Would you ruin your company reputation by doing such bad software? You should **NEVER** accept a contract from a client if that client make such stupid requirement. – Phil1970 Aug 28 '16 at 21:43

1 Answers1

2

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,

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37