Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Repositioning Winform label in code, not working as expected

Repositioning Winform label in code, not working as expected

Scheduled Pinned Locked Moved C#
csswinforms
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Ben Fair
    wrote on last edited by
    #1

    I have a windows forms application with a single form. On the form I have a ListView control in Details mode that I am using as a grid to display data; the ListView's Anchor property is set to all 4 edges (Top, Bottom, Left, Right), so it will retain it's distance to all edges when resized. The data is readonly, no changes will be made to it. The ListView has 4 columns, one of which is a dollar amount and I decided to put a Label control beneath the ListView control aligned with the amount column. In code, I set the label's Text to be the total of the amounts from the ListView's amount column; the label has no anchoring or docking and AutoSize is turned off. I decided that since the ListView columns can be resized and also the ListView itself can change size if the form is maximized or restored, then I'd like to move the label around so that it stays aligned with the amount column in the ListView. The ListView is named lstInvoices and the label is named lblTotalAmount. I hooked up the ListView's ColumnWidthChanged event handler like so:

        private void lstInvoices\_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            int columnIndex = lstInvoices.Columns.IndexOfKey(NETAMOUNT\_COLUMN\_NAME);
            if (columnIndex != -1 && (columnIndex == e.ColumnIndex) || (e.ColumnIndex == columnIndex - 1))
                AlignTotalLabelWithAmountColumn();
        }
    

    and here's the AlignTotalLabelWithAmountColumn function:

        private void AlignTotalLabelWithAmountColumn()
        {
            int columnIndex = lstInvoices.Columns.IndexOfKey(NETAMOUNT\_COLUMN\_NAME);
            if (columnIndex == -1)
                return;
    
            lblTotalAmount.Width = lstInvoices.Columns\[columnIndex\].Width;
            lblTotalAmount.Left = lstInvoices.Left;
            for (int i = 0; i < columnIndex; i++)
            {
                lblTotalAmount.Left += lstInvoices.Columns\[i\].Width;
            }
            lblTotalAmount.Top = lstInvoices.Bottom + 2;
        }
    

    In the function, I set the label's Width property to the same as the column in the ListView, and I calculate the Left position of the label by taking the Left of the ListView, plus the Width of each column up to the column I wish to line it up with. Last

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups