Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I know I can format collums of data in a list box if I use a Courier font, but how do I go about it using another font?

 

Is having several different list boxes arranged side by side the only way to do it?

 

Regards,

 

Mick Dugan

  • *Experts*
Posted

The font shouldn't have any impact. However, keep in mind your column width is a pixel value when you set it.

Also, the function of a multicolumn list box is to place items into as many columns as are needed to make vertical scrolling unnecessary.

 

Are you trying to have multiple columns of related data?

like:

index1 data1

index2 data2

 

Check this link

Posted

Why not use the ListView. Initially somewhat more complex and harder to learn but when you're used to it it's simply wonderful

 

/Kejpa

Posted

One more question...

 

Thanks for the responses. I was able to figure out list view with the aid of one of my reference manuals, but there's just one glitch...

 

When I go to refresh the list view with "listView1.clear" all the formating I set up in the properties window gets erased. Is there a way to refresh the data without disturbing the columns? Or do I have to recreate it via code?

 

Also, I'd like to be able to have the user click on the a column heading and have the order toggle from asending to decending. Any tips?

 

Regards,

 

Michael Dugan

Posted

some suggestion from newbie

 

If you want a new set of column everytime user reload the page. Use the clear function followed by the re-create column scripts.

 

About the 2nd question do you mean sorting? How about put a button and the heading to call a function sort the column and then re-create and display?

  • *Experts*
Posted (edited)

To clear a listview without disturbing the column formating use ListView1.Items.Clear()

 

For sorting columns I use the following sub in reference to a class

 

   Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick

       Dim bAltSort As Boolean = False
       Dim MySortClass As New SortClass()
       Try
           MySortClass.column = e.Column

           'Find out of we are sorting a numeric column
           If IsNumeric(ListView1.Items(0).SubItems(e.Column).Text.ToUpper.Trim) Then
               MySortClass.mNumeric = True
           Else : MySortClass.mNumeric = False
           End If

           'Find out if the user is clicking on the same column.
           If mIntSelectedCol = e.Column Then
               MySortClass.AltSort = True
               mIntSelectedCol = -1
           Else
               mIntSelectedCol = e.Column
           End If

          ListView1.ListViewItemSorter = MySortClass

       Catch
           'MsgBox(Err.Description, MsgBoxStyle.Critical)
       End Try
   End Sub

then the class

   Private mIntSelectedCol As Integer

   Public Class SortClass
       Implements IComparer
       Private mColumn As Integer
       Public mNumeric As Boolean
       Private m_bAltSort As Boolean
       Public Property column() As Integer
           Get
               Return mColumn
           End Get
           Set(ByVal Value As Integer)
               mColumn = Value
           End Set
       End Property
       Public Property Numeric() As Boolean
           Get
               Return mNumeric
           End Get
           Set(ByVal Value As Boolean)
               mNumeric = Value
           End Set
       End Property
       Public Property AltSort() As Boolean
           Get
               Return m_bAltSort
           End Get
           Set(ByVal Value As Boolean)
               m_bAltSort = Value
           End Set
       End Property
       Public Function Compare(ByVal x As Object, ByVal y As Object) _
     As Integer Implements System.Collections.IComparer.Compare
           Dim lv1 As ListViewItem = x
           Dim lv2 As ListViewItem = y
           Try
               If mNumeric Then
                   If m_bAltSort = True Then
                       Return CLng(lv1.SubItems(mColumn).Text) + CDbl(lv2.SubItems(mColumn).Text)
                   Else
                       Return CDbl(lv2.SubItems(mColumn).Text) - CDbl(lv1.SubItems(mColumn).Text)
                   End If
               Else
                   If m_bAltSort = True Then
                       Return String.Compare(lv2.SubItems(mColumn).Text, lv1.SubItems(mColumn).Text)
                   Else
                       Return String.Compare(lv1.SubItems(mColumn).Text, lv2.SubItems(mColumn).Text)
                   End If
               End If
           Catch
           End Try
       End Function
   End Class

Edited by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

Hey DiverDan,

does this really, really work?!?!?

If mNumeric Then
   If m_bAltSort = True Then
       Return CLng(lv1.SubItems(mColumn).Text) + CDbl(lv2.SubItems(mColumn).Text)
   Else
       Return CDbl(lv2.SubItems(mColumn).Text) - CDbl(lv1.SubItems(mColumn).Text)
   End If

Firstly, You're not comparing, you're adding/subtracting according to my universe.

Secondly, If m_bAltSirt=true you're adding a Long with a double

 

I'd have it this way


If mNumeric Then
If m_bAltSort = True Then
Return CDbl(lv1.SubItems(mColumn).Text) > CDbl(lv2.SubItems(mColumn).Text)
Else
Return CDbl(lv2.SubItems(mColumn).Text) > CDbl(lv1.SubItems(mColumn).Text)
End If
[/codE]

 

Another thing that might screw up is if you have text and numeric values in the same column, and the first is numeric. Then you will try to sort the listview as if the whole column was numeric and that won't work. Isn't it better to mark the column as numeric when you fill it?

 

IMHO

Kejpa

  • *Experts*
Posted

You know, I haven't really taken to time to look at the code closely BUT....

Yes, It Really, Really Works....very well also.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

Sorry if I offended you.

I was just surprised over the matters I pointed out.

I will use (the modified) code and I'm thankful for what you've done.

 

/Kejpa

  • *Experts*
Posted

(I hate open questions):

If you really wanted to use a Listbox with "tabs", you'd have to use the Win API SendMessage. Something like this in C#:

public class Form1 : Form
{
   // Declare the DLL function at the top of a class
   [DllImport("user32", EntryPoint="SendMessage")]
   private static extern int SendMessageTabStops(int handle, int msg, int tabCount, int[] tabStops);
   private const int LB_SETTABSTOPS = 0x192;

   private void SetTabStops()
   {
       listBox1.Items.Add("dan\tbob\tMe");
       listBox1.Items.Add("bob\tbob\tHim");
       listBox1.Items.Add("hagatha\tbob\tHer");

       int[] tabStops = new int[] {0, 50, 100};
       SendMessageTabStops(listBox1.Handle.ToInt32(), LB_SETTABSTOPS, tabStops.Length, tabStops);
   }
}

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...