Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > checkedlistbox help.....


Reply
 
Thread Tools Display Modes
  #1  
Old 09-09-2005, 02:52 AM
chngbh chngbh is offline
Centurion
 
Join Date: Mar 2005
Posts: 169
Unhappy checkedlistbox help.....


Hi There,

can anybody tell me how to retrieved the item in the CheckedListBox and compare with the textbox? so there would not have any duplicate data...
Here is my coding line
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "" Then MsgBox("Please enter your string under the text box!!!", MsgBoxStyle.Exclamation, "Error") Else If ChkListBox.Items.Count = 0 Then ChkListBox.Items.Add(Trim(TextBox1.Text)) Else For n = 1 To ChkListBox.Items.Count If ChkListBox.CheckedItems.Item(n) = Trim(TextBox1.Text) Then MsgBox("Duplicate Entry : " & TextBox1.Text & _ " !!!", MsgBoxStyle.Information, "Error") Exit For End If Next If n = (ChkListBox.Items.Count - 1) Then ChkListBox.Items.Add(Trim(TextBox1.Text)) End If End If TextBox1.Clear() TextBox1.Focus() End If End Sub
Edit by italkid: Please use the [vb]][ and [/vb] tags to display your code, thanks.

Last edited by italkid; 09-09-2005 at 04:28 AM.
Reply With Quote
  #2  
Old 09-09-2005, 06:41 AM
McF's Avatar
McF McF is offline
Junior Contributor
 
Join Date: Feb 2003
Location: South Carolina
Posts: 215
Default

Where is n declared and what data type is it?
The reason is that this line:
Code:
If ChkListBox.CheckedItems.Item(n) = Trim(TextBox1.Text) Then

will normally complain about the '='
__________________
"Success is dependent on effort."~Sophocles
"Behold the turtle. He makes progress only when he stick his neck out."~James Bryant Conant

Last edited by McF; 09-09-2005 at 06:47 AM.
Reply With Quote
  #3  
Old 09-09-2005, 07:21 AM
McF's Avatar
McF McF is offline
Junior Contributor
 
Join Date: Feb 2003
Location: South Carolina
Posts: 215
Default

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n As Integer Dim b As Boolean If TextBox1.Text = "" Then MsgBox("Please enter your string under the text box!!!", MsgBoxStyle.Exclamation, "Error") Else If ChkListBox.Items.Count = 0 Then ChkListBox.Items.Add(Trim(TextBox1.Text)) Else 'For n = 0 To ChkListBox.Items.Count() b = TextBox1.Text.Equals(ChkListBox.Items(n).ToString) If (b = True) Then MsgBox("Duplicate Entry : " & TextBox1.Text & _ " !!!", MsgBoxStyle.Information, "Error") 'Exit For End If 'Next 'If n = (ChkListBox.Items.Count - 1) Then ' ChkListBox.Items.Add(Trim(TextBox1.Text)) 'End If End If TextBox1.Clear() TextBox1.Focus() End If End Sub End Class

This works only the first pass. Examine your For loop closely and you will see that it doesn't work the way it's implemented. The rest is up to you.
__________________
"Success is dependent on effort."~Sophocles
"Behold the turtle. He makes progress only when he stick his neck out."~James Bryant Conant
Reply With Quote
  #4  
Old 09-11-2005, 08:21 PM
chngbh chngbh is offline
Centurion
 
Join Date: Mar 2005
Posts: 169
Default

thanks.. i had figure out the for loop... but i faced another problem in delete selected item in the check listbox....

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For n = 0 To ChkListBox.Items.Count - 1
If ChkListBox.GetSelected(n) = True Then
ChkListBox.Items.RemoveAt(n)
End If
Next
End Sub
Reply With Quote
  #5  
Old 09-11-2005, 09:05 PM
McF's Avatar
McF McF is offline
Junior Contributor
 
Join Date: Feb 2003
Location: South Carolina
Posts: 215
Default

Quote:
Originally Posted by chngbh
thanks.. i had figure out the for loop... but i faced another problem in delete selected item in the check listbox....
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ChkListBox.Items.Remove(ChkListBox.SelectedItem) End Sub
__________________
"Success is dependent on effort."~Sophocles
"Behold the turtle. He makes progress only when he stick his neck out."~James Bryant Conant
Reply With Quote
  #6  
Old 09-11-2005, 09:18 PM
chngbh chngbh is offline
Centurion
 
Join Date: Mar 2005
Posts: 169
Default

your code is working great but if i selected more that one....its able to remove the last checked list only???
Reply With Quote
  #7  
Old 09-11-2005, 11:09 PM
McF's Avatar
McF McF is offline
Junior Contributor
 
Join Date: Feb 2003
Location: South Carolina
Posts: 215
Default

Try this:
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim x As Integer x = 0 Do While ChkListBox.CheckedItems.Count > 0 If ChkListBox.GetItemCheckState(x) = CheckState.Checked Then ChkListBox.Items.RemoveAt(x) Else x += 1 End If Loop End Sub
__________________
"Success is dependent on effort."~Sophocles
"Behold the turtle. He makes progress only when he stick his neck out."~James Bryant Conant

Last edited by McF; 09-11-2005 at 11:28 PM.
Reply With Quote
  #8  
Old 09-11-2005, 11:30 PM
chngbh chngbh is offline
Centurion
 
Join Date: Mar 2005
Posts: 169
Default

thanks
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->