How to Create a Web Browser in Visual Studio
Want to create a cool web browser?
If you don't have Visual Studio download it now to get started. Go to www.visualstudio.com and download a free version called Visual Studio 2015 Community.
Things you need to do:
- Download Visual Studio Community 2015. It's FREE!!!!
- To get started click on new project and make sure you are in C# on the left.
- Click on Windows Forms Application.
- Go ahead and name your application WebBrowserApp. Click OK.
- In the toolbox to the left, drag and drop a menu strip onto the design form located in the toolbox and place it on the very top of the design form.
- In the menu strip, fill in some text in the boxes, such as File, About, Exit
- Double click on Exit, automatically it creates an instance in the Form1.cs. Type in this.close();
- Next drag and drop a text box on the design form under the menu strip and resize it so it fits the page. Change it's text to http:\\ in the property menu.
- Drag and drop a button and place it to the right of the text box. In the Property menu change the text to search.
- Drag and drop a web browser object onto the designer form and resize to fit the entire page.
- OK lets get codding. Double click on the button. Type these codes in.
- The green texts are comments.
-
//On click of this button the web control will display//the page requested in the text box (url).private void button1_Click(object sender, EventArgs e){NavigateToPage();}15. We need to anchor our text box. So click once on the textbox to change the anchor properties and click on the three arrows, the right, top and left arrow.16. Anchor the button by clicking on it once and in the property menu. Change anchor property by clicking on the top and left arrows. Now when you run the program it will be automatically changed. It will hold the controls to the side.17. We want to type in the text box to go to a web browser, for example www.bing.com, but we don't want to click on the button every time. We want to press enter when we type something in the text box.18. Go to the textbox and in the property menu to the right. Click on the event button (aka. lightning bolt) and find the key press event. Double click the key press event.19. The event will pop up. Type this code in the form.cs://This function will fire everytime the enter key is pressed.private void textBox1_KeyPress(object sender, KeyPressEventArgs e){// //if the keystroke is pressed enter then do somethingif (e.KeyChar == (char)ConsoleKey.Enter)// {NavigateToPage();}20. Click on web browser and type this code in the form.cs:
//This is the core navigation to all core processingprivate void NavigateToPage(){button1.Enabled = false;textBox1.Enabled = false;webBrowser1.Navigate(textBox1.Text);}21. Test out and run the program by pressing the green start button on the top. It should run a browser when you type: www.bing.com or your preferred browser.Cool you have a web browser!!!!!!!
Here's the complete code:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace SimpleWebBrowser{public partial class Form1 : Form{public Form1(){InitializeComponent();}//This function is called when the exit menu is selected.private void exitToolStripMenuItem_Click(object sender, EventArgs e){this.Close();}private void aboutToolStripMenuItem_Click(object sender, EventArgs e){MessageBox.Show("This program was made by Roxanne Reyes.");}//On click of this button the web control will display//the page requested in the text box (url).private void button1_Click(object sender, EventArgs e){NavigateToPage();}//This is the core navigation to all core processingprivate void NavigateToPage(){button1.Enabled = false;textBox1.Enabled = false;webBrowser1.Navigate(textBox1.Text);}//This function will fire everytime the enter key is pressed.private void textBox1_KeyPress(object sender, KeyPressEventArgs e){// //if the keystroke is pressed enter then do somethingif (e.KeyChar == (char)ConsoleKey.Enter)// {NavigateToPage();}private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){button1.Enabled = true;textBox1.Enabled = true;}}}
No comments:
Post a Comment