Input for WinForms | ComponentOne
Input Controls / Label
In This Topic
    Label
    In This Topic

    Label control is a read only control displaying formatted data. The control is usually used to provide descriptive text to inform user about name or type of data required in another input control such as TextBox. It can also be used to give information or instruction about the form.

    Label control

    The control can have a single line or a multi-line text and can be resized automatically according to its caption. The automatic resizing is controlled by AutoSize property which is especially helpful if the caption is set to change at run-time. You can also set the Trimming property which lets you specify how to handle the text that does not fit in the available space.

    Add Label Control
    Copy Code
    label1 = new C1Label();
    Controls.Add(label1);
    label1.Location = new Point(10, 140);
    label1.Text = "This is a long label.";
    label1.Trimming = StringTrimming.EllipsisCharacter;
    

    The Label control also lets you set an Image on the control. You can set the TextAlign and ImageAlign properties to position the text and image respectively. To specify the position of image with respect to the text, you can also set the TextImageRelation property. The control also lets you set icon using the Icon property which supports bitmap, font and vector icons.

    Data Binding

    Binding a control with a data source gives you the ability to communicate and even update the underlying data through the control. The Label control can be bound to an array, enumeration or a binding source using DataSource property of the C1Label class. Additionally, it can be bound to a specific field of the data source using DataMember property of the C1Label class.

    The following code binds the Label control to a data source. Here, first, we create a data table, say, _data and add two columns, Name and Phone to it. Then, we use DataMember property to bind the Label control to the Name field of the data table.

    C#
    Copy Code
    //create a data table
    DataTable _data = new DataTable("Phones");
    //bind the label control
    c1Label1.DataSource = _data;
    c1Label1.DataMember = "Name";