I want to be able to set up a home page in Webbrowser.

Asked 2 years ago, Updated 2 years ago, 39 views

I'd like to be able to specify a home page on the Webbrowser.
If you set the URL to be the home page in TextBox and press Enter, the home page will be displayed
Will the URL I specified come out?Please tell me how to do something like this

c#

2022-09-29 22:19

1 Answers

Both Forms and WPF should be using the WebBrowser.Navigate(string).The following is an example of the Forms code:

public partial class Form 1:Form
{
    public Form 1()
    {
        // Place control.Typically InitializeComponent(); is acceptable
        var button1 = new Button();
        button1.Dock=DockStyle.Top;
        button1.Text="Go";
        Controls.Add (button1);

        vartextBox1 = new TextBox();
        textBox1.Dock=DockStyle.Top;
        textBox1.Text="http://ja.stackoverflow.com/";
        Controls.Add (textBox1);


        var webBrowser=new WebBrowser();
        webBrowser.Dock=DockStyle.Fill;
        Controls.Add (webBrowser);

        // Event handler registration (if the confirm button is used)
        AcceptButton=button1;
        button1.Click+=(s,e)=>
        {
            // URL move
            webBrowser.Navigate(textBox1.Text);
        };

        // Event handler registration (direct return key handle)
        textBox1.KeyDown+=(s,e)=> 
        {
            if(!e.Handled&e.KeyCode==Keys.Return)
            {
                // URL move
                webBrowser.Navigate(textBox1.Text);
                e.Handled = true;
            }
        };
    }
}

There are two parts of the code that use lambda expression (~+=(s,e)=>~), but this is a normal event handler.Locate and register the event in the Properties window.


2022-09-29 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.