SortedList
Hi
i have a problam with functions from System.Collections (SortedList) and every time i try to make somthing of it i have a problem with syntax :(
Ok the task is simple : i have 1 textBox and 10 labels, when i write somthing in the textbox it should automaticli(or by pressing som buton) show in those 10 labels. and i have to use functions from System.Collections (SortedList) i have couple of books for the programers but they are old and the syntax is sometimes different :(
plis help
PS. Im using Microsoft Visual Studio 2005
[556 byte] By [
loginnet] at [2007-11-11 11:58:29]

# 1 Re: SortedList
To display the contents of a textbox in a label, put the following code in a button_click event or in the textbox_changed event:
Label1.Text = TextBox1.Text
I don't understand why you would need to use a SortedList to do this. Can you provide more information about your assignment?
# 2 Re: SortedList
i know that i can do it like this ;), but i have to put the same text from textBox to all labels in the same time. I think i need to make an empty array with 10 elements , and when i write some word in textBox it will be saved as an element of the array(copy 10 times this element and show in every label)
# 3 Re: SortedList
Why not simply say:
Label1.Text = TextBox1.Text
Label2.Text = TextBox1.Text
Label3.Text = TextBox1.Text
...etc? If you copy TextBox1.Text to an array, you'll still have to do:
Label1.Text = Array(0)
Label2.Text = Array(1)
Label3.Text = Array(2)
so using an array doesn't save you any coding; it actually increases it.
# 4 Re: SortedList
i understand that it will increase the cod and it's longer but i need to learn the structure (maybe i wiil need it one day to more complcated things).
if You know how to use it please help
# 5 Re: SortedList
Have you looked at the examples in the documentation?
http://msdn2.microsoft.com/en-us/library/system.collections.sortedlist(vs.80).aspx
# 6 Re: SortedList
Yes i have based my project on the code from the example but i still dosen't work :(
# 7 Re: SortedList
Why don't you post the code you have and describe how it doesn't work. Do you get an error message? If so, what does it say and on which line does it occur? If not, how does the code behave differently than you expect it to?
# 8 Re: SortedList
Plase just don't laugh I'm still learning VB.net :o
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class Form1
Dim a As String
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
a = TextBox1.Text
End Sub
Private Sub btnCreateSL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateSL.Click
Dim mySl As New Collections.SortedList
mySl.Item(a) = 0
mySl.Item(a) = 1
mySl.Item(a) = 2
mySl.Item(a) = 3
mySl.Item(a) = 4
mySl.Item(a) = 5
mySl.Item(a) = 6
mySl.Item(a) = 7
mySl.Item(a) = 8
mySl.Item(a) = 9
lblName.Text = ("SortedList only -VALUE- ")
Dim i As Integer
For i = 0 To mySl.Count - 1
Label1.Text = (mySl.GetByIndex(0))
Label2.Text = (mySl.GetByIndex(1))
Label3.Text = (mySl.GetByIndex(2))
Label4.Text = (mySl.GetByIndex(3))
Label5.Text = (mySl.GetByIndex(4))
Label6.Text = (mySl.GetByIndex(5))
Label7.Text = (mySl.GetByIndex(6))
Label8.Text = (mySl.GetByIndex(7))
Label9.Text = (mySl.GetByIndex(8))
Label10.Text = (mySl.GetByIndex(9))
Next i
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
End
End Sub
End Class
First i want to enter a text to textBox (remembered as "a") then i press the button btnCreateSL and it should create a SortList and show elements of it in the Label from 1-10... but no luck so far. Maybe there is a logical error but i can figure it out :(
# 9 Re: SortedList
Ok i think i understand now what was wrong :)
This is corect:
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class Form1
Dim a As String
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
a = TextBox1.Text
End Sub
Private Sub btnCreateSL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateSL.Click
Dim mySl As New Collections.SortedList
mySl.Item(0) = a
mySl.Item(1) = a
mySl.Item(2) = a
mySl.Item(3) = a
mySl.Item(4) = a
mySl.Item(5) = a
mySl.Item(6) = a
mySl.Item(7) = a
mySl.Item(8) = a
mySl.Item(9) = a
lblName.Text = ("Sorted List only -VALUE- ")
Dim i As Integer
For i = 0 To mySl.Count - 1
Label1.Text = (mySl.Item(0))
Label2.Text = (mySl.Item(1))
Label3.Text = (mySl.Item(2))
Label4.Text = (mySl.Item(3))
Label5.Text = (mySl.Item(4))
Label6.Text = (mySl.Item(5))
Label7.Text = (mySl.Item(6))
Label8.Text = (mySl.Item(7))
Label9.Text = (mySl.Item(8))
Label10.Text = (mySl.Item(9))
Next i
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
End
End Sub
End Class
Mr. Phil Weber thank you for your interest :WAVE:
but i still have a lot to learn ;)
# 10 Re: SortedList
The program abowe creats sortedList and add's text to sortedList after clicking a button (and it is done in the same time). Now i would like to change the structure of the code. The sortedlist shuld be created at the start of the program automaticly with an empty labels as an items of it, and after clicking a buuton it should load selected item from the sortedList and add text to it from the textBox. i fought about somthing like that :
Public Class Form1
Public Sub SortedList()
Dim SL As New Collections.SortedList
SL.Item(0) = lbl1.Text
SL.Item(1) = lbl2.Text
SL.Item(2) = lbl3.Text
SL.Item(3) = lbl4.Text
SL.Item(4) = lbl5.Text
End Sub
Private Sub btnAddText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddText.Click
Dim i As Integer
For i = 0 To SL.count - 1
....
Next i
End Sub
End Class
But when i write SL.count -1 it says to me that : "Name 'SL' is not decleard" ?! i don't understand why ?! i have decleard SL as a public variable, and i dont know how the load item and add text structure to SortedList item should look like:(
any help?
# 11 Re: SortedList
i know were the mistake was and i changed it and i change the code as well that it would show the new text in the same time as writing it in the text box
Public Class Form1
Public SL As New Collections.SortedList
Public a As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SL.Item(0) = lbl1
SL.Item(1) = lbl2
SL.Item(2) = lbl3
SL.Item(3) = lbl4
SL.Item(4) = lbl5
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
End
End Sub
Private Sub txtBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
a = txtBox.Text
Dim i As Integer
Dim myLbl As Label
For i = 0 To SL.Count - 1
myLbl = CType(SL.GetByIndex(i), Label)
myLbl.Text = a
Next i
End Sub
End Class
:)
# 12 Re: SortedList
So, does this mean the issue is resolved?
Hack at 2007-11-11 20:53:45 >
