tlavalle71


I am creating an app and i need to use a checklist box as a filter or selcting which resources to show. But everytime i try to populate it with a table, it nevers seems to populate, using id as the value and the description as the text by the checkbox then when the checkbox is check it will filter out the id for that checkbox. Could someone help figure this out.

Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69


Can you post your code   it will help find your problem...

What type of control are you showing the results of the combobox selection

Some steps to get you started.

1.  First create the code that shows all the results.  (This makes sure you have a good connection and the underlining code is good)

2.  Create the combobox and make sure it has all the opptions (data)  you want.

3.  At that point deside if you want to filter the data at  the data source or in your app.  (If the data source is large always filter at the source)

 

 

 






Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

Ok, Here is the code:

Form:

Option Strict On

Option Explicit On

Public Class frmPatient

' Declaring module level variables

Private oldTime As Integer = 0

Private newTime As Integer = 0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Label1.Text = TimeString & " " & DateString

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Me.Close()

End Sub

Private Sub frmPatient_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'TODO: This line of code loads data into the 'Ntier_SCOADataSet.Resources' table. You can move, or remove it, as needed.

Me.ResourcesTableAdapter.Fill(Me.Ntier_SCOADataSet.Resources)

' This sub handles the initial reading of text file.

Dim Patient_Info As New List(Of String)

Dim record As String

Patient_Info = Module1.ReadTextFile()

' Determine if record should be appended based on timestamp

Integer.TryParse(Patient_Info(0), newTime)

If newTime > oldTime Or oldTime = 0 Then

' Concatenate strings

record = Module1.ConcatenateStrings(Patient_Info)

' Add record to appropriate list

Module1.AppendRecord(record, Patient_Info(3))

' set oldTime to newTime

oldTime = newTime

End If

End Sub

Private Sub UpdateQues(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick, xRefreshButton.Click

' This sub handles the refreshing of data when either the timer expires

' or the xRefreshButton is clicked.

' Note: timer expires in 60000 milliseconds or 1 minute.

Dim Patient_Info As New List(Of String)

Dim record As String

Patient_Info = Module1.ReadTextFile()

' Determine if record should be appended based on timestamp

Integer.TryParse(Patient_Info(0), newTime)

If newTime > oldTime Then

' Concatenate strings

record = Module1.ConcatenateStrings(Patient_Info)

' Add record to appropriate list

Module1.AppendRecord(record, Patient_Info(3))

' set oldTime to newTime

oldTime = newTime

End If

Module1.ShowRecords()

End Sub

Private Sub xResourceListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' This sub shows the correct lists when a resource is selected.

Module1.ShowRecords()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xPlacedButton.Click

' This sub is called when the xPlacedButton is clicked

' Note: follow the calls to the appropriate subs or functions.

Dim index As Integer

index = Me.xRecordsListBox.SelectedIndex

Module1.MoveToPlaced(index)

End Sub

End Class

module1:

Option Strict On

Option Explicit On

Module Module1

' Declaring module level lists

' Each resource will need two lists. Patient que & Placed List

Private AWS As New List(Of String)

Private AWS2 As New List(Of String)

Private XYG As New List(Of String)

Private XYG2 As New List(Of String)

Private JBY As New List(Of String)

Private JBY2 As New List(Of String)

Public Function ReadTextFile() As List(Of String)

' This function reads the text file and parses it appropriately.

' Returns a list to calling function or sub

Dim NewInfo As New List(Of String)

Dim read_line() As String

' path will be different depending on location of text file

Const path As String = "C:\hl7test"

Dim temp_MSH_pipes() As String

Dim temp_SCH_pipes() As String

Dim temp_NTE_pipes() As String

Dim temp_PID_pipes() As String

Dim temp_AIG_pipes() As String

Dim temp_REASON_carrots() As String

Dim temp_APPTTIME_carrots() As String

Dim temp_STATUS_carrots() As String

Dim temp_PATIENT_carrots() As String

Dim temp_RESOURCE_carrots() As String

Dim patient_Lname As String

Dim patient_Fname As String

'NewInfo(0) = file_time

'NewInfo(1) = appt time

'NewInfo(2) = patient full name

'NewInfo(3) = resource

'NewInfo(4) = reason

'NewInfo(5) = status

'NewInfo(6) = current time

'NewInfo(7) = comment

' assign each line of text file to element of array

read_line = My.Computer.FileSystem.ReadAllText(path & "\testnl7.txt").Split(Chr(13))

' splitting first line by pipes

temp_MSH_pipes = read_line(0).Split(Chr(124))

' don't need to do anything else except strip out the timestamp

' note: could not use entire number, so I eliminated the year.

' this string is for application purposes and will not be shown on form.

NewInfo.Add(temp_MSH_pipes(5).Substring(5))

' splitting second line by pipes into elements of an array

temp_SCH_pipes = read_line(1).Split(Chr(124))

' splitting preceding array by carrots(^) into elements of another array

temp_APPTTIME_carrots = temp_SCH_pipes(11).Split(Chr(94))

' strip out the appointment time from correct element of new array

NewInfo.Add(temp_APPTTIME_carrots(3).Substring(8, 4))

' repeating preceding steps to acquire rest of data

temp_PID_pipes = read_line(3).Split(Chr(124))

temp_PATIENT_carrots = temp_PID_pipes(5).Split(Chr(94))

patient_Lname = temp_PATIENT_carrots(0)

patient_Fname = temp_PATIENT_carrots(1)

' concatenating to names

NewInfo.Add(patient_Lname & ", " & patient_Fname)

temp_AIG_pipes = read_line(6).Split(Chr(124))

temp_RESOURCE_carrots = temp_AIG_pipes(3).Split(Chr(94))

NewInfo.Add(temp_RESOURCE_carrots(0))

temp_REASON_carrots = temp_SCH_pipes(6).Split(Chr(94))

NewInfo.Add(temp_REASON_carrots(1))

temp_STATUS_carrots = temp_SCH_pipes(25).Split(Chr(94))

NewInfo.Add(temp_STATUS_carrots(0))

' including timestring into list

NewInfo.Add(TimeString)

temp_NTE_pipes = read_line(2).Split(Chr(124))

NewInfo.Add(temp_NTE_pipes(3))

Return NewInfo

End Function

Public Function GetList(ByVal index As Integer) As List(Of String)

' This function determines which patient list to examine by examining the

' passed index. Returns the correct list to calling function or sub.

' Note: this will have to be expanded to include every resource.

Dim CorrectList As New List(Of String)

If index = 0 Then

CorrectList = XYG

ElseIf index = 9 Then

CorrectList = AWS

ElseIf index = 22 Then

CorrectList = JBY

End If

Return CorrectList

End Function

Public Function GetList2(ByVal index As Integer) As List(Of String)

' This function determines which placed list to examine by examining the

' passed index. Returns the correct list to calling function or sub.

' Note: this will have to be expanded to include every resource.

Dim CorrectList As New List(Of String)

If index = 0 Then

CorrectList = XYG2

ElseIf index = 9 Then

CorrectList = AWS2

ElseIf index = 22 Then

CorrectList = JBY2

End If

Return CorrectList

End Function

Public Function ConcatenateStrings(ByVal Patient_List As List(Of String)) As String

' This function takes a list and concatenates it with appropriate spacing for

' Patient list. Returns a record as string.

Dim record As String

record = Patient_List(1).PadRight(12, Chr(32)) & Patient_List(2).PadRight(26, Chr(32)) & Patient_List(3).PadRight(11, Chr(32)) & Patient_List(4).PadRight(11, Chr(32)) & Patient_List(5).PadRight(10, Chr(32)) & Patient_List(6).PadRight(10, Chr(32)) & Patient_List(7)

Return record

End Function

Public Sub AppendRecord(ByVal record As String, ByVal resource As String)

' This sub appends records in the patient que by determining what the resource is.

' These values are passed by the calling function or sub.

If resource Like "XYG" Then

XYG.Add(record)

ElseIf resource Like "AWS" Then

AWS.Add(record)

ElseIf resource Like "JBY" Then

JBY.Add(record)

End If

End Sub

Public Sub AppendRecord2(ByVal index As Integer, ByVal record As String)

' This sub appends records in the Placed list by determining what the resource is.

' These values are passed by the calling function or sub.

If index = 0 Then

XYG2.Add(record)

ElseIf index = 9 Then

AWS2.Add(record)

ElseIf index = 22 Then

JBY2.Add(record)

End If

End Sub

Public Sub ShowRecords()

' This sub shows the records for the appropriate resource

Dim show_who As Integer

Dim i As Integer

Dim my_list As New List(Of String)

' determine who is selected

show_who = frmPatient.xCheckedListBox1.SelectedIndex()

' get the correct list

my_list = GetList(show_who)

' clear out the Top list box

frmPatient.xCheckedListBox1.ClearSelected()

For i = 0 To my_list.Count - 1

' add the records from the list to the listbox

frmPatient.xCheckedListBox1.Items.Add(my_list(i))

Next

' get the correct list

my_list = GetList2(show_who)

' clear out the bottom list box

frmPatient.xCheckedListBox1.Items.Clear()

For i = 0 To my_list.Count - 1

' add the records from the list to the listbox

frmPatient.xPlacedListBox.Items.Add(my_list(i))

Next

End Sub

Public Sub MoveToPlaced(ByVal index As Integer)

' This sub moves records from the top list box to the bottom

' list box when the Placedbutton is clicked. It does this by

' determining who is selected and which record in the top list

' box is selected. With this information, it can get the correct

' list and add the record to the bottom list box and to the

' appropriate list that holds Placed records for the resource.

' An index value of the selected record to move is passed from

' calling function or sub.

Dim change_who As Integer

Dim top_list As New List(Of String)

Dim bottom_list As New List(Of String)

Dim Update As String

' determine which resource is selected

change_who = frmPatient.xCheckedListBox1.SelectedIndex

' get the correct list for the top listbox

top_list = GetList(change_who)

' get the correct list for the bottom listbox

bottom_list = GetList2(change_who)

' get the correct record from the top listbox

Update = top_list(index)

' remove the record from the top list

top_list.RemoveAt(index)

' add the record to the correct list that holds Placed records.

AppendRecord2(change_who, TimeString.PadRight(13) & Update)

' now just show the correct records for the lists.

ShowRecords()

End Sub

End Module






Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69

I see you have some VB6 or lower background. Lots of old school techniques..... Great commenting....

Your loading the CLB from the one of the arrays

XYG2, AWS, JBY after a click on the control. (I cann't find where you are loading the CLB at load.) I quickly scan the code so I could have missed it.

If I just missed the load procedure then run your app and check the arrays to see if your convertion from data set to array is correct.

When debuging does the arrays have data

As I could not find the CLB in form load I don't know witch array or dataset your starting with... If thier isn't some thing I'm missing the CLB doesn't load data until some data in it is click so it will never show anything...

Sorry for the quick scan if I did miss where your loading the CDL when the form opens or user does some thing then please post just it and i'll see if I can help more.






Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

Yep VB6. the instructor i had in college has done VB for a long time.

lets start over. I at this moment just trying to get a checkedlistbox to fill from a database table I know the value i want if checked but can not see check boxes or names. when i do a preview of the data that is binded it shows the table. where do i need to go from there.

Sorry for starting over I just want to understand this control.





Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69

Without knowing what you are binding to I can only give you the basics.

Assuming a data set. Easiest way just set the CLB.datamember = Field name and CLB.datasource = yourdataset. Remember when you get the object back it will be in dataviewrow type(selection change etc). If you already to that point let me know....

If you are using a array to load the data, your going to have to load the data manully.. if this is the case let me know and i'll post a example or different techniqes to use.






Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

by binding what table



Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69

    the table name that has the list data    

sorry but the above post will not work with .net 1 or 2.  it required 3 or custom control.






Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69

Sorry about that. Here are two opptions.

1. Create a custom user control with databound objects. The link details the steps for a list box but it works the same for checkedlistbox.

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSADD.v10.en/dnadvnet/html/vbnet08262002.htm Create a custom user control with databound objects.

or

2. Load data manully and keep track of the index. The example below used a type dataset using the employee table of the northwind DB. One CheckedListBox and one listbox with a single button.

Private col As New Generic.List(Of MyDataObject) ' Using a type collection I can easily keep track of the User ID or my row index.

'Load collection IN the form load or event you want.... **********************************

Me.CheckedListBox1.BeginUpdate()

For Each dr As NorthwindDataSet.EmployeesRow In Me.NorthwindDataSet.Employees

col.Add(New MyDataObject(dr.EmployeeID, dr.FirstName, dr.LastName)) 'Create a new object of MydataType with data....

Me.CheckedListBox1.Items.Add(dr.FirstName & " " & dr.LastName) ' Loads the check box.

Next

Me.CheckedListBox1.EndUpdate()

**************************************************************************************************************************************************************************

Private Sub ShowResultsInListbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.ListBox1.Items.Clear()

For Each x As Integer In Me.CheckedListBox1.CheckedIndices

Me.ListBox1.Items.Add(col.Item(x).EmplyoyeeID & " " & col.Item(x).LastName)

Next

End Sub

''''''''''''''''''''''''''''''''''''''''' Type collection details ''''''''''''''''''****************************************************************************************************************************************

Public Class MyDataObject

Private _EmployeeID As Integer

Private _FirstName As String

Private _LastName As String

Private Shared Counter As Integer

Public Sub New()

End Sub

Public Sub New(ByVal eID As Integer, ByVal eFirstName As String, ByVal eLastName As String)

EmplyoyeeID = eID

FirstName = eFirstName

LastName = eLastName

End Sub

Public Property EmplyoyeeID() As Integer

Get

Return _EmployeeID

End Get

Set(ByVal value As Integer)

_EmployeeID = value

End Set

End Property

Public Property FirstName() As String

Get

Return _FirstName

End Get

Set(ByVal value As String)

_FirstName = value

End Set

End Property

Public Property LastName() As String

Get

Return _LastName

End Get

Set(ByVal value As String)

_LastName = value

End Set

End Property

End Class






Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

CyberTaz,

I am going to try thesse out. I will get back to you to say if i got working or not.

Thanks





Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

I want to thank for you help cybertaz. but i got the answer but trudging along. here is what is did.

rivate Sub frmPatient_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'TODO: This line of code loads data into the 'Ntier_SCOADataSet.Resources' table. You can move, or remove it, as needed.

Me.xResourceT.Fill(Me.Ntier_SCOADataSet.Resources)

'This is used populate the checklistbox

Dim items As String

Dim r As Integer = 0

Dim c As Integer = 1

While r < xResourceT.GetData.Rows.Count

items = CStr(xResourceT.GetData.Rows(r).Item(c))

xCLBResource.Items.Add(items)

r = r + 1

End While





Re: how do i populate a checklistbox from a database in MSSQL server?

cybertaz69

Here is the code using your typed dataset. But thier is a issue down the road using either code. You just have the string and not a data index to link too.

Me.xResourceT.Fill(Me.Ntier_SCOADataSet.Resources)

for each dr as Resourcesrow in Me.Ntier_SCOADataSet.Resources

xCLBResource.Items.Add(dr.Myfieldname.tostring)

end for

GL




Re: how do i populate a checklistbox from a database in MSSQL server?

tlavalle71

ok, that i understand and i am working on it, there will be a second variable as the index.

thank you once again