[Resolved] [2008] Changing DataBindings Text Property for TextBoxes Using Code
Sorry if that title didn't make much sense. I'm new to databindings and VB.NET - I used to use ADODC with VB6. Once I've set up databindings I can set a text box or other controls to link with a database's specific field by changing the Text property under Data > DataBindings. I want to change the field the text box links to at different times using code. How can I do this?
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
As far as I'm aware, once a Binding has been created you cannot change the property of the data source that it's associated with. You'd have to create a new Binding object to replace the existing one, which would look like this:
vb.net Code:
Me.TextBox1.DataBindings.Remove(Me.TextBox1.DataBindings("Text"))
Me.TextBox1.DataBindings.Add("Text", dataSource, "new property name")
That's an unusual thing to want to do, although not unheard of. Do you mind if I ask why?
1 Attachment(s)
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Sorry I'm replying late. I've been without access to VB.NET for a few days, so couldn't respond properly. I tried the code, but unfortunately got errors. First, I'll explain why I want to do this. For the second year of my college system, I'm expanding upon my supermarket management system.
http://xs135.xs.to/xs135/09032/staff_screen169.jpg
I've designed a tab interface and have done the programming in VB.NET. Switching from tab to tab works fine. When switching from tab to tab though, the same text boxes that are on top of the tab control remain in place. Hopefully you can see what I'm getting at here. This is the code I used to test it...
Code:
Me.DataBindings.Remove(Me.txtBox1.DataBindings("ColleagueID"))
Me.txtBox1.DataBindings.Add("ColleagueHoursPerWeek", UberData, "Staff")
Unfortunately, I get an error. See the attachment. I'm guessing I've done something stupid. And yes, I did link txtBox1 to ColleagueID before I compiled.
Edit: Didn't realise the attachment would display it in my post like that - sorry it's so huge. =/
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Let's look at that line that's throwing the exception and analyse what it's doing. First, you're trying to get a Binding from a TextBox that is bound to the TextBox's ColleagueID property? Does the TextBox have a ColleagueID property? You're then trying to take that Binding from the TextBox and remove it from the form. That doesn't make sense either. I think you need to take another look at the code I posted because you haven't done what I did.
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Right, I can see the multiple stupid errors I made. First of all, I read it in a hurry and mustn't have took in the part about removing the Text property. D'oh. So I ended up thinking "Text" was something to replace. Secondly, even if that was the case I didn't type your code up properly. I can be a blundering fool at times. :D
I might still be making stupid errors, but I've changed the code to what I believe it should/may be. Unfortunately I think I've done something wrong on the second line as I'm having problems. (I'm assuming dataSource is to be replaced with this.) Also, I'm wanting to make it bind to the ColleagueHoursPerWeek field in the Staff table in this case.
http://xs435.xs.to/xs435/09034/codeoffail335.jpg
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
You should read the IntelliSense hints. When you typed "Me.txtBox1.DataBindings.Add(", it shows you that you must add an object with type 'System.Windows.Forms.Binding'. You instead entered 3 strings....
It should be:
Code:
Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueID"))
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Quote:
Originally Posted by Hamish
You should read the IntelliSense hints. When you typed "Me.txtBox1.DataBindings.Add(", it shows you that you must add an object with type 'System.Windows.Forms.Binding'. You instead entered 3 strings....
It should be:
Code:
Me.txtBox1.DataBindings.Add(New Binging("Text", UberData, "ColleagueID"))
What Intellisense tells you is that there are seven overloads of the Add method, only one of which requires you to pass a Binding object. The other six have signatures that match the six overloads of the Binding constructor. There's no point creating a Binding explicitly and passing that to the Add method unless you need to create the Binding and add it in different methods. Otherwise you would always just let the Add method create the Binding object for you. It doesn't hurt to creat it yourself but why do this:
vb.net Code:
Me.txtBox1.DataBindings.Add(New Binging("Text", UberData, "ColleagueID"))
when you can do this:
vb.net Code:
Me.txtBox1.DataBindings.Add("Text", UberData, "ColleagueID")
and get the same result?
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
I changed the code...
Code:
Me.txtBox1.DataBindings.Remove(Me.txtBox1.DataBindings("Text"))
Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueHoursPerWeek"))
But I still get an unhandled exception error upon execution of the second line. Sorry I lack the experience to figure this one out. I can give you the code if you're generous enough to fix it.
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Quote:
Originally Posted by jmcilhinney
It doesn't hurt to creat it yourself but why do this:
vb.net Code:
Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueID"))
when you can do this:
vb.net Code:
Me.txtBox1.DataBindings.Add("Text", UberData, "ColleagueID")
and get the same result?
I stand corrected. :)
Quote:
Originally Posted by ShaunC193
But I still get an unhandled exception error upon execution of the second line. Sorry I lack the experience to figure this one out. I can give you the code if you're generous enough to fix it.
What is the error message?
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Click here to see the error. I've got a feeling I've made a silly mistake, so if you prefer I can upload the source code onto my server. I just wish this didn't require such a workaround in the first place.
I might consider changing the programming method entirely, and instead have the text boxes' text change by retrieving it from the database with code, and have the database's data changed by replacing it with the text boxes' text, if that made sense. That way I wouldn't be binding the text boxes, which isn't necessarily a requirement anyway.
Oh, and I realise I should have changed the second line to jmcilhinney's recommendation, but since they produce the same result it shouldn't matter right now.
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
That error suggest that you don't have a column "ColleagueHoursPerWeek" in your DataSource.... do you?
[edit]
I went back up to your original explanation of what you want to do and see that you're emulating a tab control by changing the bindings on your text boxes when another 'tab' is selected. Just wanted to point out that there is a built-in tab control in VB.NET that you might want to check out.
[/edit]
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
I most certainly do. Unless it's not pointing to the staff table for some reason?
http://xs135.xs.to/xs135/09035/data_sources653.png
Another computing student has told me about the tab control, but he's also aware that I'm generally an awkward and stubborn piece of work, and have to finish what I start. Thanks for the suggestion, but I've got quite far with this tab system and definitely want to see if I can make something great out of it... even if that means having to ask for a little too much help!
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Hmmm. Not sure about this, but I think you should user UberData.Staff as DataSource for your binding then. You could have multiple fields with the same name in different tables in your database, so you need to be specific which one you want to use.
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
...You fixed it. :eek:
I'm an idiot - I can't believe I didn't even think of trying UberData.Staff. Hahaha. :rolleyes: Thanks. It works flawlessly now.