Results 1 to 23 of 23

Thread: [RESOLVED] CheckedListBox select all

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Resolved [RESOLVED] CheckedListBox select all

    Hello everybody!

    I'm interested in select all items in checkedlistbox, have anyone code?
    Last edited by Hack; Jan 8th, 2009 at 01:09 PM. Reason: Added RESOLVED to thread title and green resolve checkmark

  2. #2
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: CheckedListBox select all

    Code:
            For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i, True)
            Next i
    To uncheck them change the 'True' to 'False'.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: CheckedListBox select all

    Just loop through the items and check each one. Use a For loop and call SetItemChecked for each index.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    138

    Re: CheckedListBox select all

    thanks

  5. #5
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: CheckedListBox select all

    Quote Originally Posted by jmcilhinney
    Just loop through the items and check each one. Use a For loop and call SetItemChecked for each index.

    Hi.
    I i need some help.

    I am doing something like this..I got a checkedlistbox inside contain a few numbers. when i checked and select a few numbers den i press send right..the program will send a msg to the numbers selected.

    Cos currently example when i select only 2 numbers right? my program will send to every numbers in the checkedlistbox which is not wad i want..

    Hope you all can help me..
    Thanks in advance.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Hey,

    In this instance, you are going to want to loop through te CheckedItems Collection, i.e.

    Code:
            For i As Integer = 0 To CheckedListBox1.CheckedItems.Count
    
            Next i
    Gary

  7. #7
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    Quote Originally Posted by gep13
    Hey,

    In this instance, you are going to want to loop through te CheckedItems Collection, i.e.

    Code:
            For i As Integer = 0 To CheckedListBox1.CheckedItems.Count
    
            Next i
    Gary
    Hmmm. I see sth like this before.. this is to loop through the checkedlistbox right?

    but how do i send msg to the selected numbers?

    i need to use sth like if else izzt? for example if blah blah is selected den send msg else do nothing? izzt sth like this?

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Hey,

    I think you missed what I suggested.

    The code that I gave you will only loop through the checkboxes that are checked, it will not loop through all of them.

    Is this not what you want?

    Gary

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Although, one slight correction on the above:

    Code:
            For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
    
            Next i
    Gary

  10. #10
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    I see..

    So it will loop through the checkedlistbox and check for selected right?

    but if i wanna do sth for example send msg to the selected right? wad is the code i need to type?

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Yes, the above code will loop through all the checked items in the checkedlistbox and you can do something for each one.

    You are going to have to be a bit more clear about what you mean about send msg though?!? Are you talking about sending an email?

    Gary

  12. #12
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    okie thanks.

    i am not sending email..i am using a sim card which is place inside a GSM modem to send msg to other phones by selecting the phone numbers in the checkedlistbox..

    the problem i have now is no matter wad number i select..the program will send to every numbers in the checkedlistbox..

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Well assuming that the mobile number is the text of the checklistbox item, you can get that information using:

    Code:
            For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
                MessageBox.Show(CheckedListBox1.CheckedItems.Item(i).ToString())
            Next i
    And from there you could use the number to do the sending of the message.

    Gary

  14. #14
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    hmmm..so i assume
    For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1 (will loop through the checkedlistbox right?

    but as for

    MessageBox.Show(CheckedListBox1.CheckedItems.Item(i).ToString())
    and Next i right...wad they do?

    Sorry but i am very weak in vb..

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Hey,

    For and Next go together to complete the looping of the checkedlistbox, so can't have one without the other.

    Code:
    MessageBox.Show(CheckedListBox1.CheckedItems.Item(i).ToString())
    All this is doing is showing the value of the current checkbox. Which is what I thought you wanted.

    To be honest though, if you are struggling with this, I would think you are going to struggle with sending text messages. I think you should maybe read some of the VB.Net Tutorials linked to at the top of this forum.

    Gary

  16. #16
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    Hmmm.. i got no problem with sending text msg cos my friend already help me..

    wad i need to do now is onli to loop and check for selected numbers den have a code to send to the numbers.( i have the codes for sending out the msg though)

    so wad i need now is to implement a if else statement for example i check that checkedlistbox for selected numbers if detected numbers that are check then it will send the msg to the numbers..
    else do nothing right?( i noe else do nothing is wrong..wad is the correct code not to do anything?)

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    As I have said before, you don't need to check for the check box being checked or not. The code I gave you is looping through ONLY the checked items, hence why I am using the CheckedItems collection. i.e. every item in the for loop is one that you want to do something with.

    Have you tried the code that I gave you?

    If you drop it into a form you should be able to see it working.

    Gary

  18. #18
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    i see.

    but do i have to state wad i need to do with the checked item? (for example send msg to them)

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Hey,

    I am not sure if we understand eahc other. assuming that the phone nu:ber you want is the checkbox value, what is passed into the MessageBox.Show function above should be able to be passed into your other method for sending the message.

    Can you post the code that you are trying to use, maybe it will help to see what you are currently looking at.

    Gary

  20. #20
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    Hmmm..post the whole program code?

  21. #21
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    not expecting the whole program, no, maybe just the population of the checkboklist, and then the main sms message function.

  22. #22
    New Member
    Join Date
    Feb 2009
    Posts
    9

    Re: [RESOLVED] CheckedListBox select all

    Hi. i dunno whether you are still here but this is the code that i use..


    Private Sub btnShutdownCom3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShutdownCom3.Click
    sendMsgTo(CheckedListBox1, strShutdownMsg)
    End Sub


    and


    Private Sub sendMsgTo(ByVal txtbox As CheckedListBox, ByVal msg As String)
    Dim strSendStatus As String = ""
    Dim index As Integer

    initialisePorts()



    For index = 0 To CheckedListBox1.CheckedItems.Count - 1
    MessageBox.Show(CheckedListBox1.CheckedItems.Item(index).ToString())
    Next index

    'initialisePorts()
    SerialPort1.WriteLine("AT+CMGS=" + txtbox.Items.Item(index) + vbCr) 'AT send sms command/selected txtbox
    txtMainPanel.Text += vbNewLine & ".....SMS Sending in Progress....." 'displays on the mainpanel (a textbox)
    txtMainPanel.Refresh()
    System.Threading.Thread.Sleep(750) 'delay
    SerialPort1.WriteLine(msg) 'sends shutdown/restartmsg
    System.Threading.Thread.Sleep(500) 'delay
    SerialPort1.WriteLine(Chr(26))
    System.Threading.Thread.Sleep(5500) 'delay
    strSendStatus = SerialPort1.ReadExisting()
    txtMainPanel.Text += vbNewLine + strSendStatus





    If InStr(strSendStatus, "OK") <> 0 Then
    txtMainPanel.Text += vbNewLine + "SMS message successfully sent!"
    txtMsg.Clear()
    MessageBox.Show("SMS message successfully sent")
    Else
    MessageBox.Show("Try again")

    End If



    SerialPort1.Close()

    End Sub

  23. #23
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] CheckedListBox select all

    Can I take it from the rep that you gave that you have it working?

    I can't see in the above where you pass the phone number to the serial port, but what you would need to do in the above woud be something like this:

    Code:
    Private Sub sendMsgTo(ByVal txtbox As CheckedListBox, ByVal msg As String)
    Dim strSendStatus As String = ""
    Dim index As Integer
    
    'initialisePorts()
    initialisePorts()
    
    For index = 0 To CheckedListBox1.CheckedItems.Count - 1
    
    SerialPort1.WriteLine("AT+CMGS=" + txtbox.Items.Item(index) + vbCr) 'AT send sms command/selected txtbox
    txtMainPanel.Text += vbNewLine & ".....SMS Sending in Progress....." 'displays on the mainpanel (a textbox)
    txtMainPanel.Refresh()
    System.Threading.Thread.Sleep(750) 'delay
    SerialPort1.WriteLine(msg) 'sends shutdown/restartmsg
    System.Threading.Thread.Sleep(500) 'delay
    SerialPort1.WriteLine(Chr(26))
    System.Threading.Thread.Sleep(5500) 'delay
    strSendStatus = SerialPort1.ReadExisting()
    txtMainPanel.Text += vbNewLine + strSendStatus
    
    If InStr(strSendStatus, "OK") <> 0 Then
    txtMainPanel.Text += vbNewLine + "SMS message successfully sent!"
    txtMsg.Clear()
    MessageBox.Show("SMS message successfully sent")
    Else
    MessageBox.Show("Try again")
    
    End If
    
    Next index
    SerialPort1.Close()
    
    End Sub
    Here I have wrapped the code to send the message within the for loop so that it will send a message for each checked item. Whether you need to put the number in the above, put:

    Code:
    CheckedListBox1.CheckedItems.Item(index).ToString()
    Having said that though, I am not sure that this is the correct approach. It might be better to isolate the sending of the message, and rather than pass a checkedboxlist, pass just the number and the message, and then do the looping elsewhere.

    Just a thought.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width