How to set the Location of the ListBox in C#?

In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. In ListBox, you are allowed to set the location of the ListBox using Location Property of the ListBox. This property contains the coordinates of the upper-left corner of the ListBox control relative to the upper-left corner of its form. You can set this property in two different ways:

1. Design-Time: It is the easiest way to set the location of the ListBox as shown in the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the ListBox control from the ToolBox and drop it on the windows form. You are allowed to place a ListBox control anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the ListBox control to set the location of the ListBox.

    Output:

2. RunTime: It is a little bit trickier than the above method. In this method, you can set the location of the ListBox control programmatically with the help of given syntax:

public System.Drawing.Point Location { get; set; }

Here, the Point indicates the upper-left corner of the ListBox control relative to the upper-left corner of its form. The following steps show how to set the location of the ListBox dynamically:

  • Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.
    // Creating ListBox using ListBox class constructor
    ListBox mylist = new ListBox();
    
  • Step 2: After creating ListBox, set the Location property of the ListBox provided by the ListBox class.
    // Setting the location
    mylist.Location = new Point(287, 109);
    
  • Step 3: And last add this ListBox control to the form using Add() method.
    // Add this ListBox to the form
    this.Controls.Add(mylist);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp25 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the
            // properties of ListBox
            ListBox mylist = new ListBox();
            mylist.Location = new Point(287, 109);
            mylist.Size = new Size(120, 95);
            mylist.ForeColor = Color.Purple;
            mylist.Items.Add(123);
            mylist.Items.Add(456);
            mylist.Items.Add(789);
      
            // Adding ListBox control
            // to the form
            this.Controls.Add(mylist);
        }
    }
    }

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.