Need a help on Checked List Boxes - [RESOLVED]

sameera

Member
Joined
Jun 16, 2005
Messages
20
Programming Experience
Beginner
I need a help regarding list boxes in vb.net my question is i have a checked list box with 3 columns, one is for checked marks and a colum for subjectCode and another for subject Name (this is for my degree project)

i'm populating it form the database. (i have wrote a dll for that ;))

what i need is a code to upload checked subject codes to another table.

can someone please tell me
1.how do i navigate the list box one by one
2.how do i find the checkd rows
3.how to i extract the first column ( or only a given column) form the list

And guys if you have a better alternative just tell me coz i wanted to get this done by anyway.

hope you all will help me,

regards
sameera
 
Last edited:
1. Iterate over the Items property.
2. CheckedItems is a collection of the actual items and CheckIndices is a collection of the indices of the items.
3. Dunno, never used multi-column list boxes.

A word of advice. If you want info about an object, put your cursor on the name in your code and press F1. The help window will display a topic listing all the members of that class. Property names are often self-explanatory (like CheckedItems) but you also get a brief description. Each member name is a link to a more detailed explanation.

Edit:
Actually thinking about number 3, if you are binding your data to the list box, an object returned by indexing the Items property will actually be a row of data. You then get the column you want from that.
 
Thanx , I tried as you said and now able to identify the checked row and can extract it. But still i'm strugalling with geting the necessary column . So if Ill show you the code so that you will get to know what i'm really wanted ,

(And i dont know whether i can tell it multi column , Its also just a list box with 2 or more columns, Sorry if i misslead you :))

say ,
'lstTest is the checked listbox name

lstTest.Items.Add("Subject Code " & vbTab & vbTab & "SubjectName")

what i need is to get only the values "Subject Code"

So please give me a help!

And thanx again for your help,

regards,
sameera




 
Firstly, it is preferred in .NET to use ControlChars.Tab rather than vbTab. Some "pure .NET" zealots will tell you that ControlChars is bad too, because it is part of the Microsoft.VisualBasic namespace. The Visual Studio help system specifically says, though, that ControlChars is the preferred .NET method, rather than using vbCrLf, et al and ControlChars.NewLine is used in some examples that I've seen in the help system.

A Tab is a character like any other (ASCII code 9 I think), so you can use this:
VB.NET:
myString.Substring(0, myString.IndexOf(ControlChars.Tab))
to return everything before the first Tab.
 
Please can you do me a favour. I tried by using several methods but still havent able to complete it. Can you give me the same code with using my above list box or tell me how to use this code segment.

I think i almost there with your help !

thankx a lot

regards,
sameera
 
I'm not sure excatly what you want to do with the subject codes once you have them, so I'll just give you an example that put's them in an array.
VB.NET:
		[color=Green]'Create an array to store the subject codes.
		'It needs to have as many elements as there are checked items in the list box.[/color]
		[color=Blue]Dim[/color] subjectCodes([color=Blue]Me[/color].lstTest.CheckedItems.Count - 1) [color=Blue]As String[/color]

		[color=Blue]Dim[/color] listItem [color=Blue]As String[/color]

		[color=Green]'Iterate over the checked items.
		'Include the step in case the count is zero.[/color]
		[color=Blue]For[/color] i [color=Blue]As Integer[/color] = 0 [color=Blue]To [/color][color=Blue]Me[/color].lstTest.CheckedItems.Count - 1 [color=Blue]Step[/color] 1
			[color=Green]'Get the indexed item and cast it to a string.
			'The cast is not necessary unless Option Strict is set to
			'On in the project properties but is good practice anyway.[/color]
			listItem = [color=Blue]CStr[/color]([color=Blue]Me[/color].lstTest.CheckedItems(i))

			[color=Green]'Extract the subject code and put it in the array.
			'The subject code goes from the first character up to the first instance of the Tab character.[/color]
		    subjectCodes(i) = listItem.Substring(0, listItem.IndexOf(ControlChars.Tab))
		[color=Blue]Next[/color]
 
Thanx Buddy, Problem Solved....But need another information

Thanx Buddy, Its perfectly working and problem solved :) . Thank you very much. Now i can do the rest.

And if you dont mind one more question !:cool: I used checked list boxes because i wasnt able to find a grid in vb.net which has checkbox property. (I mean to select...) So is there any grid in vb.net which has checkboxes to select !

And this is the first time i subscribe something in a forum and i think your site is a greate way to solve problems.

So..bye for now and I'll be back again (Coz i just started my project ;) )

Regards,

sameera
 
The DataGrid does support check boxes. I've never done it myself but you can use column styles to do all manner of things in a DataGrid. You can use a column in a DataGrid to display a field of boolean type in your table. If you want to select rows using a check box rather than display an existing field, you can simply add an extra column to your DataTable. This extra column will not affect your existing data or the underlying database in any way. Look up DataGridTableStyle and DataGridColumnStyle classes in the help or on MSDN for more information. You should probably be able to find other threads on this forum that refer to column styles. Finally, you may also be able to find a third-party grid that supports some extra features, perhaps a leading column of checkboxes, as is. GotDotNet and the Code Project are good places to start looking for free, third-party controls written by your fellows in the developer community.
 
Back
Top