On my form, I have a webbrowser, that navigates to nothing on start. But on webBrowser navigated I have this code, but the msbBox keeps showing on start up.

if (webBrowser1.DocumentText.Contains("Hello"))
            {
                this.Text = "iUltimate - Cheating Zone - " + txtUser.Text;
            }
            else
            {
                MessageBox.Show("Error, please try again. This might be caused by misspelling your username or pass. Only V.I.P. Members can sign in.");
            }

Dani AI

Generated

The WebBrowser control can raise load/complete events during initialization, so code that inspects page content may run at startup unless explicitly gated. A reliable pattern is to mark when the user actually started the action, then only examine the page when the browser finishes loading that specific request.

// class-level
bool awaitingResponse = false;
string expectedMarker = "LOGIN_SUCCESS_MARKER";

// call this from the UI action that starts the navigation
awaitingResponse = true;
webBrowser1.Navigate(loginUrl);

// DocumentCompleted handler
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // only handle the top-level document and only if we initiated the request
    if (!awaitingResponse || e.Url != webBrowser1.Document?.Url)
        return;

    awaitingResponse = false;

    var body = webBrowser1.Document?.Body?.InnerText ?? string.Empty;
    bool ok = body.IndexOf(expectedMarker, StringComparison.OrdinalIgnoreCase) >= 0;

    if (ok)
        this.Text = "Signed in: " + txtUser.Text;
    else
        MessageBox.Show("Sign-in failed. Verify credentials or server response.");
}

Notes and troubleshooting: DocumentCompleted can fire multiple times for frames; checking e.Url == webBrowser1.Document.Url ensures you handle the top-level page only. The initial blank page (about:blank) or other initialization loads will then be ignored because awaitingResponse is false. Make sure the Document/Body objects are not null before reading text. If the web page you are parsing is under your control, prefer returning a clear success token (or a JSON response via an API) instead of scraping HTML.

This builds on the gating idea mentioned earlier by and the event-waiting idea from , and it avoids startup noise noted by . For event details see WebBrowser.DocumentCompleted and the browser Document API WebBrowser.Document.

Recommended Answers

All 6 Replies

Code should be,

if(SearchButtonIsClicked){
            if (webBrowser1.DocumentText.Contains("Hello"))
            {
                this.Text = "iUltimate - Cheating Zone - " + txtUser.Text;
            }
            else
            {
                MessageBox.Show("Error, please try again. This might be caused by misspelling your username or pass. Only V.I.P. Members can sign in.");
            }
 }

The SearchButtonIsClicked... doesnt seem to work even with my button name.

Help???

Even though you don't navigate anywhere, for some reason the webbrowser control fires the navigate even to no-where.

What adatapost is trying to say is that you should check to see if your button has been clicked and only execute your code if it has.

Try putting it in the DocumentLoaded event.

Thank you guys for all your effort... I used adatapost method and checked if a button was enabled.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.