Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (2024)

  • Article

In this series of three tutorials, you create a Windows Forms application that loads a picture and displays it.The Visual Studio Integrated Design Environment (IDE) provides the tools you need to create the app.

This program has a picture box, a checkbox, and several buttons, which you use to control the application.This tutorial shows you how to add these controls.

In this second tutorial, you learn how to:

  • Add controls to your application
  • Add buttons in a layout panel
  • Change control names and locations
  • Add dialog components

Prerequisites

  • This tutorial builds on the previous tutorial, Create a picture viewer application.Complete that one first and start with the project that you created for that tutorial.

Add controls to your application

The Picture Viewer app uses a PictureBox control to display a picture.It uses a checkbox and several buttons to manage the picture and background and to close the app.You add the PictureBox and a checkbox from the Toolbox in the Visual Studio IDE.

  1. Open Visual Studio. Your Picture Viewer project appears under Open recent.

  2. In the Windows Forms Designer, select the TableLayoutPanel you added in the previous tutorial.Check that tableLayoutPanel1 appears in the Properties window.

  3. On the left side of the Visual Studio IDE, select the Toolbox tab.If you don't see it, select View > Toolbox from the menu bar or Ctrl+Alt+X.In the toolbox, expand Common Controls.

  4. Double-click PictureBox to add a PictureBox control to your form. Visual Studio adds the PictureBox control to the first empty cell of the TableLayoutPanel.

  5. Choose the new PictureBox control to select it, and then select the black triangle on the new PictureBox control to display its task list.

    Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (1)

  6. Select Dock in Parent Container, which sets the PictureBox Dock property to Fill.You can see that value in the Properties window.

  7. In the Properties window for the PictureBox, set the ColumnSpan property to 2.The PictureBox now fills both columns.

  8. Set the BorderStyle property to Fixed3D.

  9. In the Windows Forms Designer, select TableLayoutPanel.Then, in the Toolbox, double-click the CheckBox item to add a new CheckBox control.Your PictureBox takes up the first two cells in the TableLayoutPanel, so the CheckBox control is added to the lower-left cell.

  10. Choose the Text property and enter Stretch.

    Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (2)

Add buttons in a layout panel

So far, you added controls to the TableLayoutPanel.These steps show you how to add four buttons to a new layout panel in the TableLayoutPanel.

  1. Select the TableLayoutPanel on the form.Open the Toolbox, and select Containers.Double-click FlowLayoutPanel to add a new control to the last cell of the TableLayoutPanel.

  2. Set the FlowLayoutPanel's Dock property to Fill.You can set this property by selecting the black triangle, then selecting Dock in parent container.

    A FlowLayoutPanel is a container that arranges other controls in a row, one after another.

  3. Select the new FlowLayoutPanel, and then open the Toolbox and select Common Controls.Double-click the Button item to add a button control called button1.

  4. Double-click Button again to add another button. The IDE calls the next one button2.

  5. Add two more buttons this way.Another option is to select button2. Then select Edit > Copy or Ctrl+C.Next, choose Edit > Paste from the menu bar or press Ctrl+V to paste a copy of your button.Now paste it again.The IDE adds button3 and button4 to the FlowLayoutPanel.

  6. Select the first button and set its Text property to Show a picture.

  7. Set the Text properties of the next three buttons to Clear the picture, Set the background color, and Close.

  8. To size the buttons and arrange them, select the FlowLayoutPanel. Set the FlowDirection property to RightToLeft.

    The buttons should align themselves to the right side of the cell, and reverse their order so that the Show a picture button is on the right of the cell.You can drag the buttons around the FlowLayoutPanel to arrange them in any order.

  9. Choose the Close button to select it. Then, to select the rest of the buttons at the same time, press and hold the Ctrl key and choose them, too.

  10. In the Properties window, set the AutoSize property to True.The buttons resize to fit their text.

    Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (3)

You can run your program to see how the controls look. Select the F5 key, select Debug > Start Debugging, or select the Start button.The buttons that you added don't do anything, yet.

Change control names

There are four buttons on your form, named button1, button2, button3, and button4 in C#.In Visual Basic, the default first letter of any control name is capitalized, so the buttons are named Button1, Button2, Button3, and Button4.Use these steps to give them more informative names.

  1. On the form, choose the Close button.If you still have all the buttons selected, choose Esc to cancel the selection.

  2. In the Properties window, look for (Name).Change the name to closeButton.

    Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (4)

    The IDE doesn't accept names that contain spaces.

  3. Rename the other three buttons to backgroundButton, clearButton, and showButton.You can verify the names by choosing the control selector dropdown list in the Properties window.The new button names appear.

You can change the name of any control, such as the TableLayoutPanel or CheckBox.

Add dialog components

Your app can open picture files and choose a background color by using components.A component is like a control.You use the Toolbox to add a component to your form.You set its properties using the Properties window.

Unlike a control, adding a component to your form doesn't add a visible item.Instead, it provides certain behaviors that you can trigger with code.For instance, it's a component that opens an Open File dialog box.

In this section, you add an OpenFileDialog component and a ColorDialog component to your form.

  1. Select the Windows Forms Designer (Form1.cs [Design]). Then open the Toolbox and select the Dialogs group.

  2. Double-click OpenFileDialog to add a component called openFileDialog1 to your form.

  3. Double-click ColorDialog to add a component called colorDialog1.The components appear at the bottom of Windows Forms Designer as icons.

    Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (5)

  4. Choose the openFileDialog1 icon and set two properties:

    • Set the Filter property to the following value:

      JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.*
    • Set the Title property to the following value: Select a picture file.

    The Filter property settings specify the types that the Select a picture dialog box displays.

Next step

Advance to the next tutorial to learn how to add code to your application.

Tutorial part 3: Add code to your Picture Viewer

Tutorial: Add controls to a picture viewer app - Visual Studio (Windows) (2024)
Top Articles
Table de billard : comment bien choisir ? | Billards Bréton
Advanced Mining Laser
123Movies Encanto
Palm Coast Permits Online
Libiyi Sawsharpener
Walgreens Pharmqcy
Euro (EUR), aktuální kurzy měn
Coffman Memorial Union | U of M Bookstores
Es.cvs.com/Otchs/Devoted
Practical Magic 123Movies
What Auto Parts Stores Are Open
Arrests reported by Yuba County Sheriff
Teamexpress Login
Fnv Turbo
Best Cav Commanders Rok
Hardly Antonyms
Insidekp.kp.org Hrconnect
The Witcher 3 Wild Hunt: Map of important locations M19
Moparts Com Forum
Slope Tyrones Unblocked Games
Www Craigslist Milwaukee Wi
Labby Memorial Funeral Homes Leesville Obituaries
Zoe Mintz Adam Duritz
X-Chromosom: Aufbau und Funktion
Www Craigslist Com Bakersfield
Hewn New Bedford
Deshuesadero El Pulpo
Cal State Fullerton Titan Online
Paradise Point Animal Hospital With Veterinarians On-The-Go
Page 2383 – Christianity Today
Deepwoken: Best Attunement Tier List - Item Level Gaming
Robert A McDougal: XPP Tutorial
Kacey King Ranch
Storelink Afs
The Hoplite Revolution and the Rise of the Polis
NIST Special Publication (SP) 800-37 Rev. 2 (Withdrawn), Risk Management Framework for Information Systems and Organizations: A System Life Cycle Approach for Security and Privacy
Sedano's Supermarkets Expands to Orlando - Sedano's Supermarkets
Tributes flow for Soundgarden singer Chris Cornell as cause of death revealed
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Dadeclerk
Thanksgiving Point Luminaria Promo Code
Daly City Building Division
Noaa Marine Weather Forecast By Zone
This 85-year-old mom co-signed her daughter's student loan years ago. Now she fears the lender may take her house
Lcwc 911 Live Incident List Live Status
Isabella Duan Ahn Stanford
Is Ameriprise A Pyramid Scheme
John Wick: Kapitel 4 (2023)
Mawal Gameroom Download
Rise Meadville Reviews
Epower Raley's
Palmyra Authentic Mediterranean Cuisine مطعم أبو سمرة
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 6466

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.