ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Common Controls
    Advanced ProgrammingTopic 13 of 55

    Common Controls

    6 minread
    1,084words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time6 min
      Word count1,084
      Code examples0
      DifficultyIntermediate