简体   繁体   中英

VSTO Excel AddIn: When Excel Inputbox is called from modal windows form, focus goes to Visual Studio

I have a windows form in Excel AddIn application. I use ShowDialog() to display the form as a modal window. I need to specify a range address in my application. So, I added a button, which calls Application.InputBox. The button click event contains the following code

       private void button1_Click(object sender, EventArgs e)
    {
        Excel.Range myRng;
        Excel.Application app = Globals.ThisAddIn.Application;
        myRng = app.InputBox("Prompt", "Title", Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing,   8);
        label1.Text = myRng.Address.ToString();
        this.Focus();
    }

It works fine. However, I need to hide the windows form when inputbox is active. So I slightly modified the code

       private void button1_Click(object sender, EventArgs e)
    {
        Excel.Range myRng;
        Excel.Application app = Globals.ThisAddIn.Application;
        this.Visible = false;
        myRng = app.InputBox("Prompt", "Title", Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing,   8);
        label1.Text = myRng.Address.ToString();
        this.Visible = true;
        this.Focus();
    }

Unfortunately, this raises a problem. When I click the button the focus moves to Visual Studio. What am I doing wrong? How to retain the focus on Excel application at the moment when InputBox opens?

For me I added this line in the middle as below, and it worked fine.

this.Visible = false;
this.BrintToFront();

Finally I have found the solution. I changed the code within the form class as follows

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    private void button1_Click(object sender, EventArgs e)
    {
        Excel.Range myRng;
        Excel.Application app = Globals.ThisAddIn.Application;

        string fileName;
        fileName = app.ActiveWorkbook.Name;
        Process[] processes = Process.GetProcessesByName("excel");
        foreach (Process p in processes)
        {
            if (p.MainWindowTitle.Contains(fileName.Substring(fileName.LastIndexOf("/") + 1)))
            {
                SetForegroundWindow(p.MainWindowHandle);
            }
        }

        this.Visible = false;

        try
        {
            myRng = app.InputBox("Prompt", "Title", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
        }
        catch
        {
            myRng = null;
        }

        if (myRng != null)
        {
            label1.Text = myRng.Address.ToString();
        }

        this.Visible = true;
        this.Activate();
    }

Now it works as required. However, I still wonder why the problem happened and whether there is simpler solution. Please, let me know if you have any ideas.

PS This link was helpful: Set Focus on Excel Application

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