none
Issue when passing webBrowser Uri on form close event RRS feed

  • Question

  • Hi all,

    I'm rather new to the world of coding and am working on a personal application that will go to Imgur via their RestAPI to collect my collections and images.  I've figured out a way to get the authorization code from the redirect,  but when I close the form thAT contains the webbrowser control my default browser opens to the website; which I don't want.  I'm sure it's something stupid, but I can't figure out why this action is occurring.  I may not be expressing the issue well, sorry, but have included sample code below in hopes that it will help.

    using Imgur.API.Authentication.Impl;
    using Imgur.API.Endpoints.Impl;
    using Imgur.API.Enums;
    using Imgur.API.Models.Impl;
    using System;
    using System.Windows.Forms;
    
    namespace ImgGfy_Saver
    {
    
        public partial class Form1 : Form
        {
            public Form2 authForm;
            string _ClientId = <ClientId>;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnImgurAuth_Click(object sender, EventArgs e)
            {
                var client = new ImgurClient(_ClientId);
                var endpoint = new OAuth2Endpoint(client);
                var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Token);
    
                authForm = new Form2(authorizationUrl);
                authForm.FormClosing += new FormClosingEventHandler(authFormClosing);
                authForm.Show();
            }
    
            private void authFormClosing(object sender, FormClosingEventArgs e)
            {
                _imgurToken = createTokenFromUrl(authForm.TokenUrl);
    ;
            }


    using System;
    using System.Windows.Forms;
    
    namespace ImgGfy_Saver
    {
        public partial class Form2 : Form
        {
            private Uri _tokenUrl;
            public Uri TokenUrl
            {
                get { return _tokenUrl; }
            }        
    
            public Form2(string imgUrl)
            {
                InitializeComponent();
                wbImgurAuth.Url = new Uri(imgUrl);
            }
    
            private void wbImgurAuth_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                if (wbImgurAuth.Url.ToString().StartsWith(<RedirctUrl>))
                {
                    _tokenUrl = wbImgurAuth.Url;
                    this.Close();
                }
            }

    Any help is much appreciated.

    Thanks

    Rob



    • Edited by RNiswonger Wednesday, January 2, 2019 6:54 PM
    Wednesday, January 2, 2019 6:51 PM

Answers

  • Maybe you should move this.Close() to DocumentCompleted event of WebBrowser. You can also hide the form (this.Hide()) or control in Navigated, if it is not relevant, with some adjustments.

    • Edited by Viorel_MVP Wednesday, January 2, 2019 9:10 PM
    • Marked as answer by RNiswonger Wednesday, January 2, 2019 9:45 PM
    Wednesday, January 2, 2019 9:08 PM

All replies

  • Hello,

    I see nothing to cause this behavior in the code shown. I would suggest setting a breakpoint in the code that executes before closing this form and setup thru until the address opens an external browser.

    Note generally speaking to open an web page is Process.Start while for a service that should be dependent on the behavior of the service.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 2, 2019 7:30 PM
    Moderator
  • Thanks Karen,

    I'll try that again and see if there is anything I missed.  When I tried it before, I didn't see anything.

    It seems like there is still a process or something running when Form2 is closed, which disposes of the webBrowser, and the system just opens the default browser to complete that orphaned item.


    [UPDATE]

    Ran it again, and this time paid attention to the Call Stack.  Not sure if this is relevant to my issue, but it looks like the Form2 dispose method execute before the Navigation is completed. So now when it gets back to that point in the stack there is nowhere for it to go except to open a new browser?

    • Edited by RNiswonger Wednesday, January 2, 2019 7:52 PM Update
    Wednesday, January 2, 2019 7:38 PM
  • Maybe you should move this.Close() to DocumentCompleted event of WebBrowser. You can also hide the form (this.Hide()) or control in Navigated, if it is not relevant, with some adjustments.

    • Edited by Viorel_MVP Wednesday, January 2, 2019 9:10 PM
    • Marked as answer by RNiswonger Wednesday, January 2, 2019 9:45 PM
    Wednesday, January 2, 2019 9:08 PM
  • Have you tried Viorel's suggestion?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 2, 2019 9:39 PM
    Moderator
  • Thanks Viorel_... That was a good solution that I didn't think of.
    • Edited by RNiswonger Wednesday, January 2, 2019 9:47 PM
    Wednesday, January 2, 2019 9:46 PM