Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dear fellows,

I have experienced a little issue, that I can't find the reason for.
The case is, I have an ordinary ListBox in a WinForm maked in Visual C# 2008.
I'm getting a object list of an DTO class that is created, which make details for which information should be shown.

the list is getting some Enum lists from the underlaying Database, and setting them into the ListBox.
The problem ain't getting the objects shown in listbox properbly, but somehow (and its still confusing me why), SOME of the messages ain't showing the last line as it should, and if you target it in the listbox, then the last line is hidden behind the next text object, which makes it un-readable.

Source code:
C#
private void updateLogList()
        {
            if (customer != null)
            {
                logListe1.Items.Clear();
                List<LogDTO> liste = new List<LogDTO>();

                if (radioButton1.Checked)
                {  
                    liste.AddRange(Controller.Get_B_Logs(customer));                   
                    liste.AddRange(Controller.Get_A_Logs(customer));
                }
                else
                {
                    if (radioButton2.Checked)
                        liste.AddRange(Controller.Get_B_Logs(customer));
                    else
                        liste.AddRange(Controller.Get_A_Logs(customer));
                }
                var list = from p in liste
                           orderby p.Date ascending
                           select p;
                logListe1.Items.AddRange(list.ToArray<LogDTO>());
            }
        }



Image showing the issue (as you can see in the red-lined box, there is some text overlaying the date stamp for the next log, as the object above (from there where the text continue) is selected. Otherwise it just skipped the last line):
Image link

I can't see any pattern in when it screw the lines, cuz in the beginning it looked like it was after 5 lines of a log, but then I saw some logs where the text was more than 5 lines, and wasn't screwed in the view of the text...

any idea out there? Think I have check all now, but still no solution.... How come that text of an object is overlaying another object when selected, instead of just move the underlaying object in the listbox 2pixels down???

Regards
Posted
Updated 15-Mar-10 9:12am
v2

Your ListBox.DrawMode is either OwnerDrawFixed or OwnerDrawVariable, resulting in DrawItem and maybe MeasureItem events to be thrown.

A few things could be wrong:
- your MeasureItem event handler returns an incorrect height;
- your DrawItem event handler does not take into account the bounds information it is getting.

You should specify the DrawMode you are using, and show the code of the relevant handlers if you want more help.

:)
 
Share this answer
 
Is it ld.Tekst1 that is overrunning?

You are creating the bounding rectangle infoPlads with:
C#
top = e.Bounds.Y + stor.Height;
height = e.Bounds.Height;


This looks to be extending below your item rectangle.

Nick
 
Share this answer
 
Your MeasureString call is using Width, which includes the vertical scroll bar. Have you tried using ClientSize.Width?

If that doesn't work, you could try using the TextRenderer class to measure and draw your text - it's more accurate.

You still need to fix your layout rectangle infoPlads.

Nick
 
Share this answer
 
#Answer1

The DrawMode are: DrawMode.OwnerDrawVariable;

protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (Items.Count == 0 || e.Index == -1)
                return;

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(bgs, e.Bounds);
            }
            else
            {
                if (e.Index % 2 == 1)
                    e.Graphics.FillRectangle(bg1, e.Bounds);
                else
                    e.Graphics.FillRectangle(bg2, e.Bounds);
            }
            LogDTO ld = (LogDTO)Items[e.Index];

            String dato = ld.Dato.ToString();

            SizeF stor = e.Graphics.MeasureString(dato, titlenFont);

            e.Graphics.DrawString(dato, titlenFont, titelFarve, e.Bounds, StrengFormatDato);

            String customer = ld.Customer1;

            e.Graphics.DrawString(customer, titlenFont, titelFarve, new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 20, e.Bounds.Height), StrengFormatMedarbejder);

            if (ld.Lejlighed1 == null)
                e.Graphics.DrawImage(Properties.Resources.logFLille, new Rectangle(e.Bounds.Width - 15, e.Bounds.Y, 12, 12));
            else
                e.Graphics.DrawImage(Properties.Resources.logLLille, new Rectangle(e.Bounds.Width - 15, e.Bounds.Y, 12, 12));

            Rectangle infoPlads = new Rectangle(infoIndryk, (int)(e.Bounds.Y + stor.Height), e.Bounds.Width - infoIndryk, e.Bounds.Height);
            e.Graphics.DrawString(ld.Tekst1, infoFont, infoFarve, infoPlads, StrengFormatDato);

        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            if (Items.Count == 0)
                return;

            LogDTO ld = (LogDTO)Items[e.Index];

            int datoHojde = (int)e.Graphics.MeasureString("Some text to calculate the height", titlenFont).Height;
            int infoTekstHojde = (int)e.Graphics.MeasureString(ld.Tekst1, infoFont, new SizeF(Width - infoIndryk, 1000), StrengFormatDato).Height;

            e.ItemHeight = datoHojde + infoTekstHojde;
        }



Sorry for some non-english words... Some of the references are written in danish.
the code shows the MeasureItem event handler, and the OnDrawItem event handler... Hope you can understand the code, regardless some words are danish :)
 
Share this answer
 
#Answer3

Yes, ld.tekst1 is the text field that (as in thepicture), is overlaying the next text object when selecting.
It might be that each time its going to be coloured field, that the text is not fully shown or overlaying when selected, havn't figured that possibility yet.

const int infoIndryk = 10; for the infoIndryk if some1 should be confused.

infoPlads is the field below date, where the info text should be.
 
Share this answer
 
I have located the issue to following code snippet:
int infoTekstHojde = (int)e.Graphics.MeasureString(ld.Tekst1, infoFont, new SizeF(Width - infoIndryk, 1000), StrengFormatDato).Height;


I try'd to + some extra to the height as following:
int infoTekstHojde = (int)e.Graphics.MeasureString(ld.Tekst1, infoFont, new SizeF(Width - infoIndryk, 1000), StrengFormatDato).Height + e.ItemHeight;


But ofcause this ain't the optimal solution, since EVERY text field now get an extra space (even tho its only one line text).
Now the code line is targeted, maybe some of you can locate an nice solution, so all text fields looks same, regardless of size of text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900