Unformatted text preview:

Forms and ControlsC# ProgrammingJanuary 17Office HoursRavi:Wednesdays, 11-12amAmanda:Tuesdays, 11-12amThursdays, 3-4pmAll office hours will be held in Moore 100BCustom ControlsAny questions?Part IForms and ControlsGood Design•It is easy to quickly put together a GUI application using theForm Designer•But it is important to make sure the overall design of the GUIis attractive and easy to use•Today we will look at a few things to help “polish” your appsWindow Resizing•Some applications – like Calculator – are designed to be afixed size•If this is the case, you should actually make the window afixed size!•To prevent the form’s border from being resized, set itsFormBorderStyle property to FixedSingle or Fixed3D•If your window’s border is fixed, you probably don’t want toallow it to be maximized either•Set the MaximizeBox property to FalseWindow Resizing•The majority of applications should be resizable•When a window is resized, we want the controls to stretchand move appropriately to maintain the same organizationand usefulness•The Anchor property of a control defines how it isrepositioned when the window’s border changes•By default, a control’s Anchor is set toAnchorStyles.Top | AnchorStyles.Left•Note the use of the bitwise-OR operator (often used whensetting multiple flags)•This can be read as “the control is anchored to the top and tothe left” – meaning the distance from the control to the leftand top borders will remain fixedWindow Resizing•From the Designer, you can choose which sides to anchor toWindow Resizing•Depending on the type of control, it can also be resized whenthe window’s size changes•For example, if you set the Anchor ofabuttontoallfoursides, these distances will remain constant by changing thesize of the buttonWindow Resizing•Another possibility is to have a control fill the entire side of awindow•Setting a control’s Dock property to Top, for example, willbind the control the top so that it extends the entire distancefrom the left to right borders•Setting a control’s Dock property to Fill will bind it tooccupy the entire area of its containerPanels•A panel control is a container – it holds other controls•A panel has properties like BackColor and Font thatcorrespond to the area of the panel•You can use panels to group similar GUI widgets•Controls can be added to a panel instead of the form directly•For controls added to a panel, the values of propertiesLocation, Anchor, Dock,etc.arein relation to the panel,not the formGroup Boxes•A group box is similar to a panel•It draws a visible border around the controls within it•It also has a captionSplitter•You can divide your window into resizable panels•Play with SplitContainer and Splitter if you’re interestedFonts•If you want to change the font of all controls on a form, thehard way is to change the Font property of each controlindividually•The easy way is to set the Font of the container – either theform itself or a panel or group box that contains controls•This should set the Font property of all controls within thecontainerTransparency•To allow a control’s BackColor to take the color of itscontainer, set it to Color.TransparentIcons•Adefaultwindowiconisusedifnoneisspecified•To change this, set the form’s Icon property to an icon file(.ico)•Note that this also changes the icon used in the Task SwitcherIcons•There is freeware that converts images to icon files•Visual Studio also has an Icon Designer•File→New→File→Icon FileIcons•Youcanalsochangetheiconassociatedwiththecompiled.exe•By default, the icon is:•To change this, right-click the project in the Solution Explorerand select Properties•IntheApplicationtab,youcanselectaniconPart IISettings FilesSettings1. When an application is run, it is useful to allow certainproperties – like size, location, color – to be configurable2. When an application terminates, it is often useful to saveinformation to be used next time it is run•Both of these can be accomplished using settings filesSettings•A settings file is usually created with a new project, calledSettings.settings•Youcanaddsettingsfilestoanyproject•Through the Designer, you can add variables to be stored inthe settings file:Settings•Each variable can be assigned one of two scopes•Application Scope: These get stored in a commonapp.config file and can only be read at run-time•User Scope: These get stored in user.config that is specificto the user running the application can be read and written atrun-time•The location of app.config is usually in the same directoryas the project•The location of user.config is something like \Documentsand Settings\<user>\Local Settings\ApplicationData\<appname>\<appname+id>\<appversion>\ExampleAnexamplethatsavesthelocationofaformbeforeexitingandpositions the next instance of the form in the same location...private void Form1_FormClosing(object sender, FormClosingEventArgs e) {Properties.Settings settings =Properties.Settings.Default;settings.LocX = this.Location.X;settings.LocY = this.Location.Y;settings.Save();}private void Form1_Load(object sender, EventArgs e) {Properties.Settings settings =Properties.Settings.Default;this.Location =new Point(settings.LocX, settings.LocY);this.Text = "" + settings.LocX+""+settings.LocY;}Resources•You can also add resource files to your project to save strings,images, and other files•Check the Resources webpage for links to learn more aboutsettings and resource


View Full Document

Penn CIS 399 - Forms and Controls

Download Forms and Controls
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Forms and Controls and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Forms and Controls 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?