How to avoid line wrap/centering in C# listbox control

97 Views Asked by At

sorry if this has been asked, I was unable to find a solution after I searched google for about 3 days.

I'm a n00b, sorry.

With the little background on programming I have I managed to add a custom control from this project: FilesListBox from codeproject, the small program I'm trying to make is to manage sessions of files so that they can be opened in bulk, a general file session manager.

Anyway, my question is how can I customize the way the listbox's items strings are viewed, currently they are like this: wrong line presentation

The final goal would be to open a txt file on the left, see the filenames listed on the listbox on the right and open them all at one with the default app

As of the code, I believe the important part is somewhere in here:

protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
        Rectangle bounds = e.Bounds;
        if (e.Index > -1 && e.Index < Items.Count)
        {
            Size imageSize;
            string fileNameOnly = Items[e.Index].ToString();
            string fullFileName = GetFullName(fileNameOnly);
            Icon fileIcon = null;
            Rectangle imageRectangle = new Rectangle(bounds.Left + 1, bounds.Top + 1, ItemHeight - 2, ItemHeight - 2);
            if (fileNameOnly.Equals(".."))
            {
                // When .. is the string - draws directory icon                    
                fileIcon = IconExtractor.GetFileIcon(Application.StartupPath, _fileIconSize);
                e.Graphics.DrawIcon(fileIcon, imageRectangle);
            }
            else
            {
                fileIcon =
                    IconExtractor.GetFileIcon(fullFileName, _fileIconSize);
                // Icon.ExtractAssociatedIcon(item);
                e.Graphics.DrawIcon(fileIcon, imageRectangle);
            }
            imageSize = imageRectangle.Size;
            fileIcon.Dispose();


            Rectangle fileNameRec = new Rectangle(bounds.Left + imageSize.Width + 3, bounds.Top, bounds.Width - imageSize.Width - 3, bounds.Height);
            StringFormat format = new StringFormat();
            format.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(fileNameOnly, e.Font, new SolidBrush(e.ForeColor),
                fileNameRec, format);
        }

        base.OnDrawItem(e);
    }

The full code of the class can be obtained on the codeproject page, but I paste the full code here: full FilesListBox.cs anyway, maybe a kind soul can take a look.

0

There are 0 best solutions below