String to URL
-
I'm currently working on a simple web browser, but i can't get the simplest thing: how do i convert the text in my text box to type system.url? the web browser works and when i click a link the text box is updated, but i cannot get what's in the text box to go back to the web browser (which should be operated by a button). Thanks in advance. P.S. I am thinking the next thing i will try to add will be the source HTML. What property do i have to check to get this? Or what method should i use?
-
I'm currently working on a simple web browser, but i can't get the simplest thing: how do i convert the text in my text box to type system.url? the web browser works and when i click a link the text box is updated, but i cannot get what's in the text box to go back to the web browser (which should be operated by a button). Thanks in advance. P.S. I am thinking the next thing i will try to add will be the source HTML. What property do i have to check to get this? Or what method should i use?
I assume you mean System.Uri, not System.Url.
Uri uri = new Uri(textBox.Text);
Alternately, you can use the
TryCreate
static method of the Uri class to attempt to create a URI. Unlike the above method, which throws an exception if the text of the text box is not a valid URI (for instance, if you forgot to type the "http://" part of www.google.com),TryCreate
will tell you if it's a valid URI without throwing any errors:Uri validUri;
bool isValidUri = Uri.TryCreate(textBox.Text, UriKind.Absolute, out validUri);
if(isValidUri)
{
// Now you can use validUri object.
}p.s. You don't need to convert the text to System.Uri in order to call webBrowser.Navigate. Look at the System.Navigate method overloads[^], notice that one of them takes a string. With that, you can just call
browser.Navigate(textBox.Text);
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Guess who's having a birthday? (It's not Jesus) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I assume you mean System.Uri, not System.Url.
Uri uri = new Uri(textBox.Text);
Alternately, you can use the
TryCreate
static method of the Uri class to attempt to create a URI. Unlike the above method, which throws an exception if the text of the text box is not a valid URI (for instance, if you forgot to type the "http://" part of www.google.com),TryCreate
will tell you if it's a valid URI without throwing any errors:Uri validUri;
bool isValidUri = Uri.TryCreate(textBox.Text, UriKind.Absolute, out validUri);
if(isValidUri)
{
// Now you can use validUri object.
}p.s. You don't need to convert the text to System.Uri in order to call webBrowser.Navigate. Look at the System.Navigate method overloads[^], notice that one of them takes a string. With that, you can just call
browser.Navigate(textBox.Text);
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Guess who's having a birthday? (It's not Jesus) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Thanks. It works!:) and I did mean URI, thanks for telling me. So do you know how I could access the HTML of the webBrowser? Thanks.