How do you set a variable via BindingNavigator or such?

KWRandolph

Member
Joined
Mar 14, 2010
Messages
6
Programming Experience
10+
Hello,

I am using the BindingNavigator to allow a person to move through the records coming back from my database.

The values in the database are for books. I use the ISBN value as part of a image name that will be displayed in a picturebox.

For instance, the ISBN for one of the books is '0735615500'.

I want the picturebox that is in the group to use the ISBN for the image name, so it would be '0735615500.jpg'

As such, I want to concatenate 0735615500 and .jpg so that as the forward/backward buttons are clicked and the record changes, the image for the book changes as well.

What is the best way to go about this?

If I have to, I can put the image name itself in the database, but it seems redundant to do so, when the ISBN that the image name is based on is already there.

Here is a link to the layout of the form. http://i832.photobucket.com/albums/zz248/kevinwrandolph/bookformsample1.gif

Here is a link to the form after it is executed. http://i832.photobucket.com/albums/zz248/kevinwrandolph/bookformsample2.gif

Best regards,
Kevin Randolph
 
This doesn't really have anything to do with the BindingNavigator. That's just there for the user's benefit. The real work is done by the BindingSource. When the selection in a bound control changes, for whatever reason, the BindingSource raises the CurrentChanged event. In the event handler, you can get the Current item from the BindingSource and process the data appropriately.
 
Hello,

Thanks for the reply.

Could you provide a code sample for how to reference the value and set it to a variable that can have the .jpg concatenated onto the end and then used for the image.fromfile() ?

The syntax for making a label use a current ISBN10 (ISBN10 is the name of the field in the database) value from the recordset is

BooksBindingSource - ISBN10

If that is put into
myLabel.DataBindings.text
then the text for the label will show the current ISBN10.

I just can't seem to get the syntax write for assigning the BooksBindingSource value to a variable.

I'm probably missing something basic, but for the life of me, I can't figure out what it is.

Best regards,
Kevin Randolph
 
You are missing something basic:
In the event handler, you can get the Current item from the BindingSource and process the data appropriately.
Where are you getting the Current property of the BindingSource? It will give you the currently selected item, whatever that might be. Once you've got the object you can access any of its properties or methods just you would any other time.
 
I got the value from what is used as the reference for ISBN10 in a label. . .

Hello,

I got the value from what is used as the reference for ISBN10 in a label that Visual Studio auto-generated.

BooksBindingSource - ISBN10

If I assign that value to the DataBinding for a new Label or such, it outputs the ISBN10 exactly as desired.

My only issue is that I don't know what syntax to use to write 'BooksBindingSource - ISBN10' to a variable.

Best regards,
Kevin Randolph
 
Hello,

As I mentioned before, what I am hoping someone can give me, is the syntax for assigning
BooksBindingSource - ISBN10

to a variable.

If I put that value in as a databinding for a label or such, it works perfectly.

But I want to be able to set it directly as a variable that can be used for other things.

Hmm.

I think I figured out a rather sloppy way to do it.

I could set a dummy label or such, set it's text value as BooksBindingSource - ISBN10 and then set my variable to equal the value of the dummy labels text.

Ick! There has to be a better way to do it.

Something like

myVariable = BooksBindingSource - ISBN10

would be nice. Needless to say, that chucks errors :)

Best regards,
Kevin Randolph
 
Sweet! I got it working.

I figured out an ugly work around. I'm sure there is a better, more elegant way to do this, but since nobody provided the answer, it is better than nothing.

Since I was able to set the value of a label's text value to the databinding for ISBN10, I realized that I could then concatenate '.jpg' to that value and then set the picturebox.image to use the label's text value for the fromfile.

I'll try to see if I can figure out how to do it directly in the picturebox rather than using the label, but the value of label.text isn't really validated, so it was easy to use.

If someone has some code for how to do this properly/more cleanly, feel free to share.

I'm still learning VB, so I'm positive that there is a better way to do this than anything I'm likely to come up with on my own any time soon.

Best regards,
Kevin Randolph
 

Latest posts

Back
Top