ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Common Controls
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    6 min read
    Topic 13 of 55
    Topic Study Note Illustration
    13

    Common Controls

    In both Windows Forms and WPF , common controls are essential components used to interact with users, collect input, display output, and create responsive user interfaces. These...

    Common Controls in C# (Windows Forms and WPF)

    In both Windows Forms and WPF, common controls are essential components used to interact with users, collect input, display output, and create responsive user interfaces. These controls include buttons, textboxes, labels, combo boxes, and more.

    Below is a detailed guide to common controls in C# for both Windows Forms and WPF.


    1. Windows Forms Common Controls

    Windows Forms (WinForms) provides a range of basic controls that can be used to build desktop applications. The following are some of the most frequently used controls in Windows Forms:

    Button

    • A Button control is used to trigger an action when clicked. It is commonly used in forms for submitting data or performing operations.

      Example:

      Button button = new Button();
      button.Text = "Click Me";
      button.Click += (sender, e) => MessageBox.Show("Button clicked!");
      this.Controls.Add(button);
      

    TextBox

    • A TextBox is used to allow the user to input text. It is an editable field.

      Example:

      TextBox textBox = new TextBox();
      textBox.Text = "Enter your name here";
      this.Controls.Add(textBox);
      

    Label

    • A Label control is used to display non-editable text. It is often used for instructions, descriptions, or to show the result of an operation.

      Example:

      Label label = new Label();
      label.Text = "This is a label.";
      label.Location = new Point(10, 10);
      this.Controls.Add(label);
      

    ComboBox

    • A ComboBox provides a dropdown list from which users can select an option. It can also allow the user to type a custom value (depending on the DropDownStyle property).

      Example:

      ComboBox comboBox = new ComboBox();
      comboBox.Items.Add("Option 1");
      comboBox.Items.Add("Option 2");
      comboBox.SelectedIndexChanged += (sender, e) =>
      {
          MessageBox.Show($"Selected: {comboBox.SelectedItem}");
      };
      this.Controls.Add(comboBox);
      

    ListBox

    • A ListBox is used to display a list of items. It supports single or multiple selections based on its SelectionMode property.

      Example:

      ListBox listBox = new ListBox();
      listBox.Items.Add("Item 1");
      listBox.Items.Add("Item 2");
      listBox.SelectedIndexChanged += (sender, e) =>
      {
          MessageBox.Show($"Selected: {listBox.SelectedItem}");
      };
      this.Controls.Add(listBox);
      

    CheckBox

    • A CheckBox is used for binary choices, where the user can either check or uncheck the box. It is often used for options such as agreeing to terms or preferences.

      Example:

      CheckBox checkBox = new CheckBox();
      checkBox.Text = "I agree to the terms";
      checkBox.CheckedChanged += (sender, e) =>
      {
          MessageBox.Show($"Checkbox state: {checkBox.Checked}");
      };
      this.Controls.Add(checkBox);
      

    RadioButton

    • A RadioButton is used in a group where only one option can be selected at a time (exclusive choice).

      Example:

      RadioButton radioButton = new RadioButton();
      radioButton.Text = "Option 1";
      radioButton.CheckedChanged += (sender, e) =>
      {
          if (radioButton.Checked)
              MessageBox.Show("Option 1 selected.");
      };
      this.Controls.Add(radioButton);
      

    ProgressBar

    • A ProgressBar is used to show the progress of a task. It can be used for tasks like downloading files, copying, etc.

      Example:

      ProgressBar progressBar = new ProgressBar();
      progressBar.Minimum = 0;
      progressBar.Maximum = 100;
      progressBar.Value = 50; // Set current progress
      this.Controls.Add(progressBar);
      

    PictureBox

    • A PictureBox is used to display images. It supports various image formats like JPG, PNG, BMP, etc.

      Example:

      PictureBox pictureBox = new PictureBox();
      pictureBox.Image = Image.FromFile("path_to_image.jpg");
      pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
      this.Controls.Add(pictureBox);
      

    DateTimePicker

    • A DateTimePicker allows the user to select a date and/or time. It provides a calendar interface.

      Example:

      DateTimePicker dateTimePicker = new DateTimePicker();
      dateTimePicker.ValueChanged += (sender, e) =>
      {
          MessageBox.Show($"Selected date: {dateTimePicker.Value.ToShortDateString()}");
      };
      this.Controls.Add(dateTimePicker);
      

    2. WPF Common Controls

    In WPF (Windows Presentation Foundation), you use a similar set of controls, but with more styling capabilities through XAML. The following are some of the most commonly used WPF controls:

    Button

    • A Button in WPF is similar to WinForms. It triggers actions when clicked and is highly customizable in terms of style and content.

      Example (XAML):

      <Button Content="Click Me" Width="100" Height="50" Click="Button_Click"/>
      

      Example (Code-Behind):

      private void Button_Click(object sender, RoutedEventArgs e)
      {
          MessageBox.Show("Button clicked!");
      }
      

    TextBox

    • A TextBox is used for user input in WPF. It allows for both single-line and multi-line text input.

      Example (XAML):

      <TextBox Name="textBox" Width="200" Height="30"/>
      

    Label

    • A Label is used to display text without allowing user input.

      Example (XAML):

      <Label Content="This is a label" HorizontalAlignment="Left" VerticalAlignment="Top"/>
      

    ComboBox

    • A ComboBox in WPF is a dropdown list, similar to Windows Forms, but with more styling options. It can also support autocompletion.

      Example (XAML):

      <ComboBox Width="200">
          <ComboBoxItem>Option 1</ComboBoxItem>
          <ComboBoxItem>Option 2</ComboBoxItem>
      </ComboBox>
      

    ListBox

    • A ListBox in WPF displays a list of items, and can allow multiple selections depending on the SelectionMode.

      Example (XAML):

      <ListBox Width="200" Height="100">
          <ListBoxItem>Item 1</ListBoxItem>
          <ListBoxItem>Item 2</ListBoxItem>
      </ListBox>
      

    CheckBox

    • A CheckBox allows the user to choose one or more options from a list. In WPF, it can be styled with custom visuals.

      Example (XAML):

      <CheckBox Content="I agree to the terms"/>
      

    RadioButton

    • A RadioButton allows the user to select one option from a group. In WPF, RadioButton controls are often used within a GroupBox.

      Example (XAML):

      <RadioButton Content="Option 1" GroupName="Options" />
      <RadioButton Content="Option 2" GroupName="Options" />
      

    ProgressBar

    • A ProgressBar in WPF displays the progress of a task. It can be customized with animations and visual effects.

      Example (XAML):

      <ProgressBar Minimum="0" Maximum="100" Value="50" Width="200" Height="20"/>
      

    Image

    • An Image is used to display images. In WPF, you can bind it to resources, file paths, or URIs.

      Example (XAML):

      <Image Source="image.jpg" Width="100" Height="100"/>
      

    Key Differences Between WinForms and WPF

    • Styling and Customization: WPF provides more powerful tools for customizing the look and feel of controls using XAML and Styles. For example, you can use templates and data binding in WPF to make controls look much more dynamic.

    • Graphics Support: WPF supports advanced graphics, like 2D and 3D transformations, gradients, and animations. This makes it ideal for modern applications where a rich graphical user interface is needed.

    • Control Layout: WPF has more flexible layout systems (e.g., Grid, StackPanel, WrapPanel) compared to Windows Forms, which uses DockPanel and FlowLayoutPanel.


    Resources for Further Learning:

    • Windows Forms Controls Documentation: [Windows Forms Controls](https://learn.microsoft.com/en
    ← Previous topic 12
    Dialogs and Windows Controls
    Next topic 14 →
    Dynamic Link Libraries (DLLs)

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming