Tải bản đầy đủ - 0 (trang)
3 (Optional) Using Visual Studio .NET

3 (Optional) Using Visual Studio .NET

Tải bản đầy đủ - 0trang

VisualStudiowritesmuchoftheconfigurationcode,suchassettingthe

locationsofthecontrols.Wecanaddaneventhandlerforacontrolby

double-clickingonit,whichwillregisteraneventhandlerandshowits

templateforustofillin.Forexample,double-clickingonthebuttonbrings

upthecode

privatevoidbutton1_Click(objectsender,System.EventArgse)

{



}

forustofillinthebody.

Weneedtocompletetheeventhandlingcode,declaretheColor

variablec,andaddtheOnPaintmethodthatweusedinExample9.4.

VisualStudio.NETgeneratedtheremainingcodeofExample9.7.

Example9.7:Form1.cs



usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

usingSystem.Data;



namespaceVisual

{

///

///SummarydescriptionforForm1.

///


publicclassForm1:System.Windows.Forms.Form

{

privateSystem.Windows.Forms.Buttonbutton1;

privateSystem.Windows.Forms.RadioButtonradioButton1

privateSystem.Windows.Forms.RadioButtonradioButton2

privateSystem.Windows.Forms.ComboBoxcomboBox1;

///

///Requireddesignervariable.



///


privateSystem.ComponentModel.Containercomponents

=nul

privateColorc=Color.Yellow;



publicForm1()

{

//

//RequiredforWindowsFormDesignersupport

//

InitializeComponent();



//

//TODO:Addanyconstructorcodeafter

InitializeComponentcall

//

}



///

///Cleanupanyresourcesbeingused.

///


protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}



#regionWindowsFormDesignergeneratedcode

///

///RequiredmethodforDesignersupport–

donotmodify



///thecontentsofthismethodwiththecodeeditor.

///


privatevoidInitializeComponent()

{

this.button1=newSystem.Windows.Forms.Button()

this.radioButton1=

newSystem.Windows.Forms.RadioButton(

this.radioButton2=

newSystem.Windows.Forms.RadioButton(

this.comboBox1=

newSystem.Windows.Forms.ComboBox(

this.SuspendLayout();

//

//button1

//

this.button1.Location=

newSystem.Drawing.Point(16,16

this.button1.Name="button1";

this.button1.TabIndex=0;

this.button1.Text="Draw";

this.button1.Click+=

newSystem.EventHandler(this.button1_Click

//

//radioButton1

//

this.radioButton1.Location=

newSystem.Drawing.Point(104,16

this.radioButton1.Name="radioButton1";

this.radioButton1.TabIndex=1;

this.radioButton1.Text="Square";



//

//radioButton2

//

this.radioButton2.Location=

newSystem.Drawing.Point(240,16

this.radioButton2.Name="radioButton2";



this.radioButton2.TabIndex=2;

this.radioButton2.Text="Circle";

//

//comboBox1

//

this.comboBox1.Items.AddRange(newobject[]{

"Red",

"Green",

"Blue"});

this.comboBox1.Location=

newSystem.Drawing.Point(368,16

this.comboBox1.Name="comboBox1";

this.comboBox1.Size=

newSystem.Drawing.Size(121,21)

this.comboBox1.TabIndex=3;

//

//Form1

//

this.AutoScaleBaseSize=

newSystem.Drawing.Size(5,13

this.ClientSize=

newSystem.Drawing.Size(488,266

this.Controls.AddRange

(newSystem.Windows.Forms.Control[]

this.comboBox1,

this.radioButton2,

this.radioButton1,

this.button1});

this.Name="Form1";

this.Text="SelectItem";

this.ResumeLayout(false);



}

#endregion



//Displaychosenshapeinselectedcolor

protectedoverridevoidOnPaint(PaintEventArgse)



{

Graphicsg=e.Graphics;

Brushbrush=newSolidBrush(c);

if(radioButton1.Checked)

g.FillRectangle(brush,100,100,100,100);

else

g.FillEllipse(brush,100,100,100,100);

base.OnPaint(e);

}



///

///Themainentrypointfortheapplication.

///


[STAThread]

staticvoidMain()

{

Application.Run(newForm1());

}



privatevoidbutton1_Click

(objectsender,System.EventArgse)

{

if(comboBox1.SelectedItem.ToString()=="Red")

c=Color.Red;

elseif(comboBox1.SelectedItem.ToString()=="Green"

c=Color.Green;

else

c=Color.Blue;

Invalidate();

}

}

}



WeuseVisualStudio.NETtobuildandruntheapplication.Figure9.9

showsthecompletedapplicationofExample9.9.





Figure9.9:TheFormofExample9.7

TheBIGPicture

VisualStudio.NETprovidesanintegrateddevelopmentenvironmentthat

generatescodetoconfigurecontrolsthatwedragontoaform.Wecan

buildanddebugourapplicationusingthistool.



TestYourUnderstanding

12. Ifavailable,useVisualStudio.NETtocreatetheGUIofExample

9.6.



9.4(Optional)AGUIforInsertionSorting

Withuserinterfacecomponentswecanenhanceourconsole

applications,makingthemmoreuser-friendlyandinteractive.Inthis

optionalsectionwedevelopagraphicaluserinterfacefortheinsertion

sortapplicationofSection7.5.Wecompleteonefunctionatatimebefore

movingontothenext,thuscontrollingthedevelopmentprocess.We

havesomethingtoshowforoureffortbeforewefinishtheentireproject.



DefiningtheProblem

Inthissectionwedevelopagraphicaluserinterfacetosortbyinsertion.

InExample7.8wesorteddatainputbytheuserinaconsolewindow,

displayingthesortedarrayinthesameway,withnographics.Herewe

provideauser-friendlyinterfacethatletsusseetheinsertionsort

proceedgraphically,stepbystep.



DesigningaSolution:TheExploratoryProcess

Sometimesproblemscometousfullyformulated,ourjobbeingto

developagoodsolution.Othertimeswehaveageneralgoal,buthave

notyetsettledonthespecificrequirements.Forexample,wehavethe

goalofprovidingaGUIforinsertionsort,buthavenotdecidedona

specificdesign.Agoodapproachinthissituationistodoalittle

exploratoryprogramming,tryingoutsomeideasonasmallscaleto

determinewhatmightworknicely.

Wewillwanttodisplaythedatainabarchart,soletusstartthe

exploratoryprocessbytryingtodisplaydatainachart.Wecanuseatext

boxtoenterthedata.Ourprogramwillbeeventdriven,gettingthedata

anddisplayingwhentheuserclickstheSubmitbutton.



DesigningaSolution:MakingaChart

Weneedtofigureouthowtodrawthechart.Wemighthaveverylargeor

verysmallvalues,positiveornegative.Thebestapproachistosimplify

asmuchaspossible,addingrefinementslaterwhenwemasterthe



simplercases.Fornow,letususeintegerdatabetween0and99,which

willeliminatetheproblemoffiguringouttheverticalscale;werepresenta

valueof59withabarofheight59pixels.Tofindthewidthofeachbar,

wedividethewidthoftheformbythesizeofthedata,dividingtheform

intoequalpartsforeachbar.

TousetheFillRectanglemethodtodrawourbars,weneedthe

upper-leftcornerforeachbaranditsheightandwidth.Figure9.10shows

thechartwithafewbars.





Figure9.10:Partofabarchart





Figure9.11:ThechartofExample9.8

Becausethecoordinateoriginisintheupper-leftcornerofthecanvas,

theupper-leftcornerofthebarrepresentingthearrayelementitem[i]

is(i*width,100-item[i])wherewidthisthecommonwidthof

eachbar,givenbyCHART_WIDTH/count,andcountisthenumberof

elementsenteredthusfar.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

3 (Optional) Using Visual Studio .NET

Tải bản đầy đủ ngay(0 tr)

×