daveydave400 Posted December 1, 2005 Posted December 1, 2005 Ok, This is for a school assignment. All you need to know is that the user of the program enters values into an input box and then those values go into a listbox with its sorted property set to true. When you enter a 2 digit number it sorts by the first digit and not the second. Is there a way to sort the numbers by their value instead of just the first digit. For Example the listbox would show: 1 12 2 3 35 5 Quote
Administrators PlausiblyDamp Posted December 1, 2005 Administrators Posted December 1, 2005 How are you populating the listbox? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
daveydave400 Posted December 2, 2005 Author Posted December 2, 2005 Said before When you say populating I am guessing you mean how am I getting the values that are in the listbox into it. I stated in my first post that I am using an input box and each time the user enters a number and clicks ok on the inputbox it is added to the listbox. If you enter one digit numbers they are put in order because the sort property is set to true. ex: 1 2 3 4 when using 2 digit numbers: 1 15 2 3 34 5 If you meant some other meaning of populate please specify. Thanks. Quote
Cags Posted December 2, 2005 Posted December 2, 2005 That is probably what he meant by populating. If you are adding the items one at a time you could work out its position manuallly before adding it (obviously you would have to set the sort property to false to false) with the InsertAt property as opposed to the Add method. Quote Anybody looking for a graduate programmer (Midlands, England)?
Administrators PlausiblyDamp Posted December 2, 2005 Administrators Posted December 2, 2005 I was wondering if you felt like sharing the code with us - makes it a lot easier to understand the problem. At a guess I'd say you are adding the items in as strings - hence the listbox is sorting them as strings. Try converting them to a numeric data type and then adding them to the listbox. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rOSSybOY Posted December 4, 2005 Posted December 4, 2005 hey, this is rOSSybOY, im in the same class as daveydave, and i could let u see my code too, to see if there is something similar we are doing. i will have it posted on monday Quote
rOSSybOY Posted December 5, 2005 Posted December 5, 2005 Public Class Form1 Inherits System.Windows.Forms.Form Dim Index As Object #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents lstMedian As System.Windows.Forms.ListBox Friend WithEvents btnAdd As System.Windows.Forms.Button Friend WithEvents btnDisplay As System.Windows.Forms.Button Friend WithEvents btnClear As System.Windows.Forms.Button Friend WithEvents btnExit As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstMedian = New System.Windows.Forms.ListBox() Me.btnAdd = New System.Windows.Forms.Button() Me.btnDisplay = New System.Windows.Forms.Button() Me.btnClear = New System.Windows.Forms.Button() Me.btnExit = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'lstMedian ' Me.lstMedian.Location = New System.Drawing.Point(8, 8) Me.lstMedian.Name = "lstMedian" Me.lstMedian.Size = New System.Drawing.Size(224, 199) Me.lstMedian.Sorted = True Me.lstMedian.TabIndex = 0 ' 'btnAdd ' Me.btnAdd.Location = New System.Drawing.Point(8, 216) Me.btnAdd.Name = "btnAdd" Me.btnAdd.Size = New System.Drawing.Size(88, 48) Me.btnAdd.TabIndex = 1 Me.btnAdd.Text = "Add Value" ' 'btnDisplay ' Me.btnDisplay.Location = New System.Drawing.Point(144, 216) Me.btnDisplay.Name = "btnDisplay" Me.btnDisplay.Size = New System.Drawing.Size(88, 48) Me.btnDisplay.TabIndex = 2 Me.btnDisplay.Text = "Display Median" ' 'btnClear ' Me.btnClear.Location = New System.Drawing.Point(240, 8) Me.btnClear.Name = "btnClear" Me.btnClear.Size = New System.Drawing.Size(48, 80) Me.btnClear.TabIndex = 3 Me.btnClear.Text = "Clear" ' 'btnExit ' Me.btnExit.Location = New System.Drawing.Point(240, 128) Me.btnExit.Name = "btnExit" Me.btnExit.Size = New System.Drawing.Size(48, 80) Me.btnExit.TabIndex = 4 Me.btnExit.Text = "Exit" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 271) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnExit, Me.btnClear, Me.btnDisplay, Me.btnAdd, Me.lstMedian}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click lstMedian.Items.Clear() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click End End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim add As Object add = CInt(InputBox("Enter a value:", "Enter")) lstMedian.Items.Add(add) End Sub Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim median, first, last, find As Object first = 1 last = lstMedian.Items.Count - 1 find = (first + last) / 2 MessageBox.Show(find) End Sub End Class this is the code, it is what the final product may look like, but im not completely finished, due to my delay with the list box Quote
Cags Posted December 5, 2005 Posted December 5, 2005 As I mentioned previously one solution would be to turn off the Sorted property and place the items in the array manually. To do this you need to cycle through the values currently in the array checking if they are smaller than the one you wish to add, if you find one that isn't you simply add your new item before that. I've never actually really used VB.Net but I think this is the right syntax... Dim i As Int16 Dim bNotAdded As Boolean = True While bNotAdded If i < lstMedian.Items.Count Then If CInt(lstMedian.Items.Item(i)) < add Then i += 1 Else lstMedian.Items.Insert(i, add) bNotAdded = False End If Else lstMedian.Items.Add(add) bNotAdded = False End If End While Perhaps not the most elegant solution but it would work. Alternatively you could write a method (sub) to sort the array yourself (using a bubblesort algorithm or something similar) and call this every time you add an item. Quote Anybody looking for a graduate programmer (Midlands, England)?
rOSSybOY Posted December 6, 2005 Posted December 6, 2005 hey man, that worked great, thanx for the help Quote
Administrators PlausiblyDamp Posted December 8, 2005 Administrators Posted December 8, 2005 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim add As Integer add = CInt(InputBox("Enter a value:", "Enter")) lstMedian.Items.Add(add) End Sub the above code worked fine when I used it to items to the listbox. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Cags Posted December 8, 2005 Posted December 8, 2005 It works, but it doesn't add the items in the correct numerical order. For example if you add 1 then 2 then 10 it will order them 1 10 2 rather than 1 2 10 Quote Anybody looking for a graduate programmer (Midlands, England)?
penfold69 Posted December 9, 2005 Posted December 9, 2005 It works, but it doesn't add the items in the correct numerical order. For example if you add 1 then 2 then 10 it will order them 1 10 2 rather than 1 2 10 Make sure your variable is eclared as type Integer, not type Object Quote
Cags Posted December 9, 2005 Posted December 9, 2005 I don't know if you just didn't try it or I'm being stupid, but PlausiblyDamp's code clearly defines the add variable as an Integer and using his exact code does not order the list correctly on my machine. Perhaps people are using either different version of the .Net Framework or Visual Studio .Net. Quote Anybody looking for a graduate programmer (Midlands, England)?
Administrators PlausiblyDamp Posted December 9, 2005 Administrators Posted December 9, 2005 (edited) You could also store the numbers in a SortedList and use that as the datasource, the following code can replace the relevant section of your form. Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click lstMedian.Items.Clear() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Dim sl As New SortedList Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Dim add As Integer = CInt(InputBox("Enter a value:", "Enter")) sl.Add(add, add) 'needed to get the display refreshed... lstMedian.DataSource = Nothing lstMedian.DataSource = sl.GetValueList() End Sub Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim median, first, last, find As Integer first = 1 last = lstMedian.Items.Count - 1 find = Convert.ToInt32((first + last) / 2) MessageBox.Show(find.ToString) End Sub Also you might want to decalre variables as the correct type rather than Object whenever possible as it allows the compiler to do a lot more error checking for you - I would always recommend adding Option Strict On to the top of every source file. Just another quick question - are you using the listbox as part of the UI or was it only intended as a way to sort the data itself? Edited December 9, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.