-2

I'm developing a correspondence app. using VB.NET. So, I have a CheckedListBox that fills in with items once its parent form shows up, and its item collection is derived from a specific column within a table in a database via an SQL code.

So, assuming that I have filled in all required data in that form, and clicked the save button, it would save all checked items' text to the new row. However, I need to save another text other than mentioned in that CheckedListBox. Let's say it is (the text I want to save to that new row) a text mentioned in the same table, but in a different column. Here is the code I have used just to assure that I'm good to a certain point.

Since a week, and until now, I've been stock with that code.

Actually, I have tried some codes of the While..End While and Do While..Loop for the sake of changing the Str in the MessageBox with the column rows I want, but no one worked.

Try 
 Dim Str As String = ""
 If (checkedListBox1.CheckedItems.Count > 0) Then
     Dim i As Integer = 0
     Do While (i < checkedListBox1.CheckedItems.Count)
         If (Str = "") Then
             Str = checkedListBox1.CheckedItems(i).ToString
             label1.Text = Str
         Else
             Str = (Str + (", " + checkedListBox1.CheckedItems(i).ToString))
             label1.Text = Str
         End If

         i = (i + 1)
     Loop

     ' Make your connection to the DataBase and insertion code starting within that area.
     MessageBox.Show(("Your selection is for : " + Str))
 Else
     MessageBox.Show("Please select one name at least to work it out!")
 End If


 'While (checkedListBox1.CheckedItems.Count > 0)
  'checkedListBox1.SetItemChecked(checkedListBox1.CheckedIndices(0), false)
  'End While

Catch ex As Exception MessageBox.Show((ex.Message + ex.ToString)) End Try

Fahad J.
  • 1
  • 1
  • I don't know how to post images to clarify my question – Fahad J. Sep 08 '19 at 20:37
  • Where are you writing a row to a table? I'm seeing no database interaction at all in this code. – David Sep 08 '19 at 20:40
  • This is a test code to assure that code is working fine ot a certain point. So, instead of the messagebox that has the ***Str*** , I need the code to post the other text reffering to at least one checked item. – Fahad J. Sep 08 '19 at 20:50
  • Can you please guide me to posting images to show you the whole thing – Fahad J. Sep 08 '19 at 20:52
  • Ok then, what other text do you want to use? Where do you try to use this other text? Currently you're adding `checkedListBox1.CheckedItems(i).ToString` to the `Str` variable. If you want to add something else, what's stopping you? – David Sep 08 '19 at 20:52
  • OK, Let me explain my issue. I have a table with five columns, the CheckedListBox is filled with Column #1 where it has the department full name. Also, there is column #3 in same table, but it has the acronym of each department. the plot is I need a code to show the values or the acronyms that are in Column #3 not those which are already displayed in the CheckedListBox (from Column #1). That's it. – Fahad J. Sep 08 '19 at 21:56
  • 1
    *"OK, Let me explain my issue"*. By all means, explain your issue, but do it in the question, not in a comment. No one should have to read all the comments to understand what you're talking about. – jmcilhinney Sep 08 '19 at 23:41
  • Do you mean that you will check All rows that are not listed in checkedListBox1? – user11982798 Sep 09 '19 at 05:13

1 Answers1

0

I think I understand what you want. You want to display one value and be able to retrieve a corresponding value.

Create a simple class with 2 properties.

Create a List(Of T) to hold the objects containing the 2 values. I retrieved the data from a database but it could come from any source. You would replace the column names and table name with the real ones in the select string.

Bind your CheckedListBox to the List(Of T). Set the DisplayMember and ValueMember to the names of the Properies of the class.

You can retrieve the the second string by looping through the CheckedItems and asking for the "ValueString"

Public Class CorrespondenceType
    Public Property DisplayString As String
    Public Property ValueString As String

    'A parameterized constructor makes it easy to create your objects
    'with properties
    Public Sub New(Display As String, Value As String)
        DisplayString = Display
        ValueString = Value
    End Sub

End Class

Then on the Form

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Fill CheckBoxList
    Dim dataList = GetCorrespondenceTypes()
    CheckedListBox1.DataSource = dataList
    CheckedListBox1.DisplayMember = "DisplayString"
    CheckedListBox1.ValueMember = "ValueString"
End Sub

Private Function GetCorrespondenceTypes() As List(Of CorrespondenceType)
    Dim dt As New DataTable
    Dim lstTypes As New List(Of CorrespondenceType)
    Using cn As New SqlConnection(My.Settings.BuildersConnectio)
        Using cmd As New SqlCommand("Select DisplayColumn, ValueColumn From CorrespondenceTypes;", cn)
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using
    For Each row As DataRow In dt.Rows
        lstTypes.Add(New CorrespondenceType(row(0).ToString, row(1).ToString))
    Next
    Return lstTypes
End Function

And to get the second string

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lst As New List(Of String)
    For Each item As CorrespondenceType In CheckedListBox1.CheckedItems
        Debug.Print(item.ValueString)
    Next
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27