Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2007

Tip: Looking for answers? Try searching our database.

Drawing tabs in ListBox.DrawItem event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Quimbly - 27 Sep 2007 18:11 GMT
I have a ListBox, and have changed the DrawMode to DrawMode.OwnerDrawFixed.  
I'm using the ListBox.DrawItem event to do some custom coloring in my ListBox
object.

My item strings contain tabs, but after drawing the items myself, the tabs
are not intepreted.  Currently I'm just using the sample code given in the
MSDN docs for the event handler:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

private void listBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
  // Set the DrawMode property to draw fixed sized items.
  listBox1.DrawMode = DrawMode.OwnerDrawFixed;
  // Draw the background of the ListBox control for each item.
  e.DrawBackground();
  // Define the default color of the brush as black.
  Brush myBrush = Brushes.Black;

  int mod = e.Index % 3;

  // Determine the color of the brush to draw each item based on the index
of the item to draw.
  switch (mod)
  {
     case 0:
        myBrush = Brushes.Red;
        break;
     case 1:
        myBrush = Brushes.Orange;
        break;
     case 2:
        myBrush = Brushes.Purple;
        break;
  }

  // Draw the current item text based on the current Font and the custom
brush settings.
  e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font,
myBrush,e.Bounds,StringFormat.GenericDefault);
  // If the ListBox has focus, draw a focus rectangle around the selected
item.
  e.DrawFocusRectangle();
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

As I mentioned, my items contain tabs, and when I intercept this event, my
strings look like this:

"Name\taddress\tetc..."

Also, in case it matters, I'm setting my own tab stops in the list box:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

   private void SetDeviceListTabStops()
   {
     // A tab-stop location of 4 equates to the width of one average
character for the specified font
     int[] listBoxTabs = new int[] { 20, 75, 110, 160 };

     // Send an LB_SETTABSTOPS message to the ListBox control.
     int result = SendMessage(this.listBox1.Handle.ToInt32(),
LB_SETTABSTOPS, listBoxTabs.Length, ref listBoxTabs[0]);

     // Refresh the List Box control.
     this.listBox1.Refresh();
   }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Anyone know what I need to set in order for the tabs to be drawn/interpretted?
Quimbly - 27 Sep 2007 18:30 GMT
Answered my own question...

By default, when drawing a string using the

e.Graphics.DrawString()

method, the tab stops aren't set!  By specifying the tab stops in the format
string as a parameter to this method, I was able to get the tabs to display
properly:

     StringFormat format = StringFormat.GenericDefault;
     format.SetTabStops(0f, new float[] { 20f, 75f, 110f, 160f });

     // Draw the current item text based on the current Font and the custom
brush settings.
     e.Graphics.DrawString(lstDevices.Items[e.Index].ToString(), e.Font,
myBrush, e.Bounds, format);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.