Home > Mobile >  how can you add scrolls to the listbox so that they are not only visible but also active, maybe they
how can you add scrolls to the listbox so that they are not only visible but also active, maybe they

Time:12-06

this is what filling listbox.item looks like

 listBox1.Items.Add(string.Format("Місце {0} | В роботі з {1} | {2} |({3} хв) | {4}", temp[7].Substring(6, 4), temp[8].Substring(11, 5),temp[2], rezult,rezvalSub));

this is what the event handler looks like

private void listBox1_DrawItem_1(object sender, DrawItemEventArgs e)
        {
            if ((((string)listBox1.Items[e.Index]).Substring(((string)listBox1.Items[e.Index]).Length - 1)).Equals(" "))
            {

                e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), e.Bounds);

            }
            else if ((((string)listBox1.Items[e.Index]).Substring(((string)listBox1.Items[e.Index]).Length - 1)).Equals("-"))
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
            }

            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }

            if (e.Index < 0) return;
            TextRenderer.DrawText(e.Graphics, (string)listBox1.Items[e.Index], listBox1.Font, e.Bounds, listBox1.ForeColor, TextFormatFlags.Left) ;
         
        }
`

this is what it looks like properties if details are required

enter image description here

all I have achieved is made the scrolls visible, but they are definitely not active horizontally, the text just goes beyond the box sheet and there is no scroll to drag

CodePudding user response:

Not sure if these are the solutions you are looking for but it is worth giving a look: How to make a listbox scroll horizontally (credits to Sriram Sakthivel)

From official site: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.horizontalscrollbar?view=windowsdesktop-7.0

CodePudding user response:

here are my events now

 private void listBox1_DrawItem_1(object sender, DrawItemEventArgs e)
        {

            listBox1.HorizontalScrollbar = true;
            listBox1.HorizontalExtent = 0;
            if ((((string)listBox1.Items[e.Index]).Substring(((string)listBox1.Items[e.Index]).Length - 1)).Equals(" "))
            {

                e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), e.Bounds);


            }
            else if ((((string)listBox1.Items[e.Index]).Substring(((string)listBox1.Items[e.Index]).Length - 1)).Equals("-"))
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }

            if (e.Index < 0) return;
            TextRenderer.DrawText(e.Graphics, (string)listBox1.Items[e.Index], listBox1.Font, e.Bounds, listBox1.ForeColor, TextFormatFlags.Left ) ;
         
        }
        private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {


            // Cast the sender object back to ListBox type.
            ListBox theListBox = (ListBox)sender;

            // Get the string contained in each item.
            string itemString = (string)theListBox.Items[e.Index];

            // Split the string at the " | "  character.
            string[] resultStrings = itemString.Split('|');

            // If the string contains more than one period, increase the 
            // height by ten pixels; otherwise, increase the height by 
            // five pixels.
            if (resultStrings.Length > 2)
            {
                e.ItemHeight  = 10;
            }
            else
            {
                e.ItemHeight  = 5;
            }

        }
        private void InitializeOwnerDrawnListBox()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();

            // Set the location and size.
            listBox1.Location = new Point(20, 20);
            listBox1.Size = new Size(240, 240);

            // Populate the ListBox.ObjectCollection property 
            // with several strings, using the AddRange method.
            this.listBox1.Items.AddRange(new object[]{"System.Windows.Forms",
        "System.Drawing", "System.Xml", "System.Net", "System.Runtime.Remoting",
        "System.Web"});

            // Turn off the scrollbar.
            listBox1.ScrollAlwaysVisible = true;
            listBox1.HorizontalScrollbar = true;

            // Set the border style to a single, flat border.
            listBox1.BorderStyle = BorderStyle.FixedSingle;

            // Set the DrawMode property to the OwnerDrawVariable value. 
            // This means the MeasureItem and DrawItem events must be 
            // handled.
            listBox1.DrawMode = DrawMode.OwnerDrawVariable;
            listBox1.MeasureItem  =
                new MeasureItemEventHandler(listBox1_MeasureItem);
            listBox1.DrawItem  = new DrawItemEventHandler(listBox1_DrawItem_1);
            this.Controls.Add(this.listBox1);
        }

and view a listbox enter image description here

  • Related