Tabibito
Tabibito

Reputation: 121

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?

Upvotes: 0

Views: 1706

Answers (2)

박건도
박건도

Reputation: 36

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

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

Upvotes: 2

Tabibito
Tabibito

Reputation: 121

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.

P.S. This link was helpful: Set Focus on Excel Application

Upvotes: 1

Related Questions