View Full Version : Binding Passed Data


Matty
08-01-2006, 11:11 AM
Hi,

I’m a VB.NET newbie, so bear with me. I’m building a class library that I’m designing my re-usable forms in, and then inheriting them into my projects. The first base form I’m trying to make is one that will pop up when the user clicks Edit List next to a combobox (to obviously add/edit values to the combobox’s list). This would be used several times on my main form, since there are several comboboxes on the form.

I’m trying to make my class as generic as possible, so I’m thinking I’ll have to pass a dataset of some sort to my popup form and then bind my popup form with the passed data when the popup form loads. The data would be really simple – just one table with one column in it (such as a list of manufacturers). I’m just not entirely sure what objects I need to create in my base class (dataset, tableadapter, bindingsource, etc), or how to use them together.

I’ve only done typed datasets before (and that was only to create my main form in this project), so could you point me to some sort of tutorial to get me on the right path? Thanks.

Edit: I'm using VS 2005.

Kodo
08-01-2006, 02:32 PM
Probably related datatables in a dataset.
you might want to expose some public properties on your class so your other classes can grab them easily.

I'm not entirely sure what you're trying to do.. screen shots would give me a better idea. :)

Matty
08-02-2006, 06:46 AM
I've attached two screenshots to this post -- my main form and the find form that pops up.

This is a snippit of the code I'm using to try to do the find:


Public Class frmBaseFind
Public frmMainForm As Form
Public dvFindData As DataView
Public strPKName As String
Public intReturnValue As Integer

Private Sub imgFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgFind.Click
'Finds record in the passed dataview matching the search criteria entered
Try
Dim strFilter As String

strFilter = "" & Me.cboFindIn.SelectedValue & "='" & Me.txtFind.Text & "'"

dvFindData.RowFilter = strFilter

If dvFindData.Count > 0 Then
intReturnValue = dvFindData(0).Item(strPKName)

'***Here's where I'm stuck***

End If

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub



And this is the code I'm using to bring up the Find form and pass variables:

Private Sub imgFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgFind.Click
'Pop up the Find form to find a record, passing the form to grab the find fields from (this form)
' and the data to search through (EquipPool table)
Try
Dim dvEquipPool As DataView

dvEquipPool = New DataView
dvEquipPool.Table = Me.DsEquipPool.EquipPool

frmFind.frmMainForm = Me
frmFind.dvFindData = dvEquipPool
frmFind.strPKName = "EquipPoolID"
frmFind.ShowDialog()

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Kodo
08-02-2006, 06:33 PM
a few things to tweak first. These tweaks may change your flow so lets start with them first.

Dim dvEquipPool As DataView

dvEquipPool = New DataView
Can be changed to
Dim dvEquipPool As New DataView()

dvEquipPool.Table = Me.DsEquipPool.EquipPool

frmFind.frmMainForm = Me
YIKES!! don't pass your entire form. Use properties to expose only the data you need. so on your find form , create public properties that your main form can populate before you display the find form. Make sense? Then in your find form, create an event that fires when the search button is clicked.. handle this event in your main form. make sense?

I attached a VERY VERY basic example of handling an event from form2 to form1. I hope this helps point you in the right direction.

Matty
08-03-2006, 05:31 AM
Kodo,

I actually messed up -- I got my two threads mixed up. Let's just assume those last two posts by us are in the other thread, ok? :o

I've attached the proper popup form that I'm wanting to use to edit the comboboxes on the main form.

Basically what I want to do is have my popup form to automatically be sourced and bound to what the source of the corresponding combobox is. So if I click the Edit button next to the Manufacturer combobox, it should pop up this second form and have it bound to the Manufacturers table. I'm just not sure what objects I need to create in my popup form to do this.

In Access, I could just set the recordsource property of the form and that did most of the work.

By the way, thanks for taking a look at the code and setting me straight. I'm quite sure that's the first time I've passed Me before, so it's not some sort of bad habit. :) Now that I've thought about it, I think I can just put the stuff I want from "Me" into an array and pass that to the Find form.

Thanks again.

Matty
08-03-2006, 05:41 AM
Oh, and thanks for the code sample. I'll try to get that to work with my two forms and see how it goes.

Matty
08-03-2006, 08:49 AM
Kodo,

I've tried to make your sample fit to my situation, and I'm nearly there. I just need a bit of clarification on how this all works. I've searched around on custom events and I've gotten confused. Maybe you can explain it a bit better. :)

Here's my rudimentary understanding of custom events:

On my second form (in my case frmFind), I declare a public event so that it can be referenced by other forms. I then raise it using RaiseEvent on the same form, wherever I want to trigger that event (in my case when I click imgFind).

On my main form (in my case frmEquipPool), I declare an instance of frmFind, using WithEvents so that I can reference any public events it has. I then create a new sub that has the actual code the event's going to run and put a Handles after it to link it up to the above declared event.

So on my Find form, I have this:


Public Class frmBaseFind
Public dvFindData As DataView
Public strPKName As String

Public Event SearchMain(ByVal intPKValue As String)
.
.
.
Private Sub imgFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgFind.Click
'Finds record in the passed dataview matching the search criteria entered
Try
Dim strFilter As String
Dim intReturnValue As Integer

strFilter = "" & Me.cboFindIn.SelectedValue & "='" & Me.txtFind.Text & "'"

dvFindData.RowFilter = strFilter

If dvFindData.Count > 0 Then
intReturnValue = dvFindData(0).Item(strPKName)
RaiseEvent SearchMain(intReturnValue)
End If

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


And then on my main form I have this:


Public Class frmEquipPool

Private WithEvents frmSearch As New frmFind
.
.
.

Private Sub frmSearch_SearchMain(ByVal intPKValue As Integer) Handles frmSearch.SearchMain
Try
'***This is where I'll put my Search code***
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


But I'm getting an error on my Handles statement. It says that the Sub can't handle the Event because the signatures are different. It looks like it's wanting the parameter in my event, but your example didn't have that.

Am I missing something?

Kodo
08-03-2006, 01:38 PM
you declared your type in your event as string and your handler is looking for a an integer..
adjust your datatypes to match.

Matty
08-03-2006, 07:18 PM
Ah, I'm an idiot. Thanks. :o

Kodo
08-03-2006, 07:30 PM
it was apparent that your keyboard didn't have the sense enough to make the change for your :p

Matty
08-09-2006, 10:40 AM
Okay, I’ve got the spelling all correct now, I think. :) But now my sub on my main form isn’t firing when I hit the RaiseEvent line in my Find form. I’ve stepped through your example and yours fires properly, but I step through mine and it doesn’t. It gets to the RaiseEvent line, goes thru it successfully and then just continues on without flipping over to my main form code and popping up the messagebox. I think I may just need a second set of eyes to take a look at it. Here’s where my code is at now:

Find Form:

Public Class frmBaseFind
Public pnlPanel As Panel
Public dvFindData As DataView
Public strPKName As String

Public Event SearchMain(ByVal intPKValue As Integer)
.
.
.
Private Sub imgFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgFind.Click
'Finds record in the passed dataview matching the search criteria entered
Try
Dim strFilter As String
Dim intReturnValue As Integer

strFilter = "" & Me.cboFindIn.SelectedValue & "='" & Me.txtFind.Text & "'"

dvFindData.RowFilter = strFilter

If dvFindData.Count > 0 Then
intReturnValue = dvFindData(0).Item(strPKName)
RaiseEvent SearchMain(intReturnValue)
End If

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class


Main Form:

Public Class frmEquipPool
Dim WithEvents frmSearch As New frmFind

Private Sub frmSearch_SearchMain(ByVal intPKValue As Integer) Handles frmSearch.SearchMain
Try
MsgBox(intPKValue)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


If it makes any difference frmBaseFind is in a separate project that compiles to a class library. I then add an “inherited form” to my main project, choose frmBaseFind from the class I compiled and rename it frmFind. The only reason I keep it separate is that I’ll be using this find form on other projects and it made sense to keep those types of forms separate and re-usable. Would that have anything to do with it?

Matty
08-18-2006, 08:01 AM
I got the event to fire, so here's what I did (for anyone else checking out this thread):

Originally, my line to open the Find form was just simply frmFind.Show. It looks like that was opening a different instance of frmFind; one different from the frmSearch one I declared at the top of the code. If I change that line of code to frmSearch.Show, it opens the correct instance of the form. That is, the instance of the from that I have the custom event in.

Once the correct instance of the Find form was opened, the event fired.

Kodo, thanks for leading me on to the correct path. :)

Kodo
08-18-2006, 03:21 PM
You're welcome..
sorry bout that.. I didn't see the other instance declared :P