8 Replies - 1620 Views - Last Post: 04 November 2008 - 06:27 PM Rate Topic: -----

#1 aem1354  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-October 08

How to call the listbox in class?

Posted 03 November 2008 - 12:24 AM

i not familiar with listbox and i donno how to call it
Private lboxSource As listBox = Nothing
Private lboxSelected As listBox = Nothing
i am using this code to call the listbox but it doesn't work...


this is my code


Imports System.Data.OleDb

Public Class clsListBox
	Private strConnectionString As String = ""
	Private lboxSource As listBox = Nothing
	Private lboxSelected As listBox = Nothing

	Private strErrorMessage As String = ""

	Public ReadOnly Property ErrorMessage() As String
		Get
			Return strErrorMessage
		End Get
	End Property

	Public Sub AddSelectedData()

		If Me.lboxSource.items.count <= 0 Then
			strErrorMessage = ("No Data Available.")
			Exit Sub
		End If

		Me.lboxSelected.Items.Add(Me.lboxSource.SelectedItem.ToString)
		Me.lboxSource.Items.Remove(Me.lboxSource.SelectedItem)
		Me.lboxSource.Refresh()
	End Sub

	Public Sub RemoveData()
		If Me.lboxSelected.items.Count <= 0 Then
			strErrorMessage = ("No Data Available.")
			Exit Sub
		End If
		lboxSource.Items.Add(lboxSelected.SelectedItem.ToString)
		lboxSelected.Items.Remove(lboxSelected.SelectedItem)
		lboxSelected.Refresh()

	End Sub

	Public Sub AddAllData()

		If Me.lboxSource.Items.Count <= 0 Then
			strErrorMessage = ("No Data Available.")
			Exit Sub
		End If
		For i As Integer = 0 To Me.lboxSource.Items.Count - 1
			Me.lboxSource.SelectedIndex = i
			Me.lboxSelected.Items.Add(Me.lboxSource.SelectedItem.ToString)
		Next
		Me.lboxSource.Items.Clear()
		Me.lboxSource.Refresh()
	End Sub
	Public Sub RemoveAllData()
		If Me.lboxSelected.Items.Count <= 0 Then
			strErrorMessage = ("No Data Available.")
			Exit Sub
		End If
		For i As Integer = 0 To Me.lboxSelected.Items.Count - 1
			Me.lboxSelected.SelectedIndex = i
			Me.lboxSource.Items.Add(Me.lboxSelected.SelectedItem.ToString)
		Next
		Me.lboxSelected.Items.Clear()
		Me.lboxSelected.Refresh()
	End Sub

	Private Function GetDataTable( _
		ByVal strConn As String, _
		ByVal strSQL As String) As DataTable
		Dim con As New OleDbConnection
		Dim cmd As New OleDbCommand
		Dim adt As New OleDbDataAdapter
		Dim dst As New DataSet
		Dim tbl As New DataTable
		GetDataTable = tbl
		Try
			con.ConnectionString = strConn
			con.Open()
			cmd.Connection = con
			cmd.CommandText = strSQL
			adt.SelectCommand = cmd
			adt.Fill(dst)
			tbl = dst.Tables(0)
			GetDataTable = tbl
			tbl.Dispose()
			dst.Dispose()
			adt.Dispose()
			cmd.Dispose()
		Catch ex As Exception

		Finally
		End Try
	End Function

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: How to call the listbox in class?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 12:39 AM

You need to create a new instance of a listbox.
Try

Imports System.Data.OleDb

Public Class clsListBox
	Private strConnectionString As String = ""
	Private lboxSource As listBox = new listbox
	Private lboxSelected As listBox = new listbox


Was This Post Helpful? 0
  • +
  • -

#3 aem1354  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-October 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 12:44 AM

i have try the code before.. but its still cant work....
ermm

any ideas??
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 01:18 AM

what are trying to do?
Was This Post Helpful? 0
  • +
  • -

#5 aem1354  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-October 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 01:32 AM

i want to call this class at the form
the form have listbox button add,remove.
i just want to call it.

but i have problem when i call it in the form.
Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 05:03 PM

In the code you posted I don't see where you add it to your form, nor do I see where you create an instance of the listbox control, lboxSource will always be null/nothing and you will never be able to do anything with it. Are you trying to create the controls dynamically then add them to the form, or are you trying to reference controls that are already on your form?
Was This Post Helpful? 0
  • +
  • -

#7 aem1354  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-October 08

Re: How to call the listbox in class?

Posted 03 November 2008 - 06:41 PM

View Postmagicmonkey, on 3 Nov, 2008 - 04:03 PM, said:

In the code you posted I don't see where you add it to your form, nor do I see where you create an instance of the listbox control, lboxSource will always be null/nothing and you will never be able to do anything with it. Are you trying to create the controls dynamically then add them to the form, or are you trying to reference controls that are already on your form?



yes..i am trying to reference the control that are already have in my form... so how do i do it??
Was This Post Helpful? 0
  • +
  • -

#8 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: How to call the listbox in class?

Posted 04 November 2008 - 08:31 AM

Okay, I think you are trying to create a class that processes 2 listboxes, what you are missing is the logic that passes the references to the forms listboxes, you could pass them in the class constructor...

Public Class clsListBox
    Private strConnectionString As String = ""
    Private lboxSource As listBox = Nothing
    Private lboxSelected As listBox = Nothing
    Private strErrorMessage As String = ""

    'Add this
    Public Sub New(Source as ListBox, Selected as ListBox)
      lboxSource = Source
      lboxSelected = Selected
    End Sub
'...



Then when you create an instance of clsListBox you will have to supply the listbox controls from the form as parameters. I think this is what you want???

This post has been edited by magicmonkey: 04 November 2008 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#9 aem1354  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-October 08

Re: How to call the listbox in class?

Posted 04 November 2008 - 06:27 PM

yup..hehe now i get it...
its works...
i need to do like this rite?

public sub AddData(byref lbozSource as windows.form.listBox,....)

tankzz alot because give me the ideas..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1