Lately I became interested in C++ so much .. I started reading from where we stopped at college and also started learning some related technologies like C++/CLI & MFC which are used to develop windows applications using C++.

Anyway, this one of the simplest applications that you would do if you’re a C++/CLI beginner ( may be with some C# windows programming background)

Open your Visual Studio -> New Project -> Visual C++ -> Windows Forms Application

This looks pretty much like C#, the same controls on the toolbox & the same initial form .. even if you pressed Right Click -> View Code, you’ll find the same stuff that you find in C# forms except that the designer code & the code you’re going to write are on the same file.

Now go to the toolbox & and grab a Menu Strip then add 3 buttons & a TextBox inside it The user should enter the Url in the TextBox .. and then press Go To .. We’ll also have Forward & Back buttons.

Then add a web browser control under the Menu Strip .. It should look something like this:

Now let’s add some functionality for this .. Double Click on the Go To button and inside the click event handler write:

webBrowser1->Navigate(txtUrl->Text);

The Navigate function takes the text written in the textbox & ‘navigates’ to it.

Pause: Selection Operator (->):

>Now you might want to ask about the difference between the selection operator (->) & the dot (.) which is also used to access public members/functions of a class.

Well, it’s simple .. assume that we have class Student & that we created these instances:

Student stud

Student *studPtr = &stud;

Student &studRef = stud;

So we have stud (an object of Student), studPtr (a pointer to a Student object)

& studRef (a reference to a Student object)..

Assume further that Student has a Name which is a public member..

So if you want to access Name in the three cases this would be:

stud.Name = “name1″;

studPtr->Name = “name2″;

studRef.Name = “name3″;

That is, when you’re accessing public class members/functions of pointers to objects you use -> while a dot (.) is used if you’re dealing with an instance or a reference to an object of a class .. easy!

Back to our browser:

Do the same double click with the Forward/Backward buttons to create their event handlers ..

We’ll just call the GoBack() & GoForwad() functions found inside the webBrowser1

Browse!

Now let’s run and start some browsing ..

Advertisement