Vous êtes sur la page 1sur 5

In this tutorial we'll make a little application that can search the web with Google.

It will use of course a web service provided by Google. More information about it can be found at http://www.google.com/apis/. Before we start you need to have an account at Google because using that account you can get the license key don't worry! it's free" that will allow you to do #$$$ %ueries per day. &o register at https://www.google.com/accounts/'ew(ccount and after you confirm using the link in the email they sent you go to http://api.google.com/createkey and you'll get another email from Google containing the key you need.

)here's no need to download the pro*ect but if you want! go ahead: + Google,&.-ip

'ow create a new Windows Application pro*ect named Google,& and add four TextBoxes with the followin names:

txtKey where you'll enter you license key! you can set the Text property to your license key now so you don't have to enter it every time you run the application" txtNum where the number of results will be displayed" txtSearch where the string you want to search will be entered" txtResult set the Multiline property of this one to )rue and ScrollBars property to Both because this is where will display the result"

.urther add a ComboBox named cmb/age with the property DropDownStyle set to DropDown ist and a Button named btn&earch. .inaly add two Chec!Boxes named chk.ilter and chk&afe&earch.

,ith some labels added this is how the form looks like:

'ow let's add a WebRe"erence to Google's WebSer#ice. Go ahead! right click the pro*ect in &olution 01plorer and select (dd ,eb 2eference. (nd this is the 324 you need to enter! that points to Google's ,eb&ervice: http$%%api&'oo'le&com%(oo'leSearch&wsdl )he connection is added to &olution 01plorer! but let's rename it to 'oo'le:

.irst let's see the code:

public void search(int start) { // Set the mouse pointer to Busy this.Cursor = Cursors.WaitCursor;

// Clear

or any earlier results

t!t"esult.Clear(); // Create ne# $oo%leSearchService %oo%le.$oo%leSearchService $SS = ne# %oo%le.$oo%leSearchService(); // Create ne# $oo%leSearch"esult %oo%le.$oo%leSearch"esult $S" = $SS.do$oo%leSearch(t!t&ey.'e!t( t!tSearch.'e!t( start( )*( ch+,ilter.Chec+ed( --( ch+Sa eSearch.Chec+ed( --( --( --); // Store the number o total results

int res.um = $S".estimated'otal"esultsCount; // res.um/ #ill be used int res.um/ = *; // Sho# the number o results or loopin%

t!t.um.'e!t = res.um.'oStrin%(); // 0oop throu%h all the results #hile(res.um 1 res.um/) { // 2dd them to the combo bo! cmb3a%e.4tems.2dd(res.um/.'oStrin%() 5 - 6 - 5 (res.um/5)*)); // 4ncrement #ith )* because #e display )* items / pa%e res.um/ = res.um/ 5 )*; 7 // Select the corespondin% item cmb3a%e.Selected4nde! = (start / )*); // Word su%%estion strin% su%%ested = $SS.doSpellin%Su%%estion(t!t&ey.'e!t( t!tSearch.'e!t); // 4 there is a su%%estion

i (su%%ested 8= null) { // Sho# it t!t"esult.2ppend'e!t(-9id you mean: - 5 su%%ested 5 -;r;n;r;n-);

7 // 0oop throu%h the elements { // Sho# the title t!t"esult.2ppend'e!t(result.title 5 -;r;n-); // 'he ="0 t!t"esult.2ppend'e!t(result.="0 5 -;r;n-); // 2nd the summary t!t"esult.2ppend'e!t(result.summary 5 -;r;n-); 7 // Set the selection start to *... t!t"esult.SelectionStart = *; // so #e can scroll to the top o t!t"esult.Scroll'oCaret(); // Set the cursor bac+ this.Cursor = Cursors.9e ault; 7 private void btnSearch>Clic+(ob?ect sender( System.<vent2r%s e) { // 9o the search startin% search(*); 7 rom item * the te!tBo! oreach(%oo%le."esult<lement result in $S".result<lements)

private void cmb3a%e>Selected4nde!Chan%ed(ob?ect sender( System.<vent2r%s e) { // 4 the inde! is chan%ed start rom the item

// e@ual #ith the selected item in the combo bo! search((cmb3a%e.Selected4nde! A )*)); 7

)he most important method that we used is the following:

doGoogle&earch string key! string %! int start! int ma12esults! bool filter! string restrict! bool safe&earch! string lr! string ie! string oe"

5ompile it and give it a try. 6on't enter a very common keyword like pro'rammin' or Microso"t because it will take longer than you think. )he name 'ee!pedia isn't so well known ! here's the result:

.urther you should download the Google ,eb (/Is 6eveloper 7it and check Google ,eb (/Is 2eference to add more functionality to the program.

Vous aimerez peut-être aussi