|
-
Jan 8th, 2009, 05:24 AM
#1
Thread Starter
Lively Member
[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?
Last edited by ShaunC193; Jan 16th, 2009 at 10:45 AM.
-
Jan 8th, 2009, 06:20 AM
#2
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?
-
Jan 13th, 2009, 06:31 AM
#3
Thread Starter
Lively Member
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
Last edited by ShaunC193; Jan 13th, 2009 at 06:36 AM.
-
Jan 13th, 2009, 08:10 AM
#4
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.
-
Jan 15th, 2009, 04:40 AM
#5
Thread Starter
Lively Member
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. 
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.
-
Jan 15th, 2009, 04:59 AM
#6
Hyperactive Member
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"))
Last edited by Hamish; Jan 15th, 2009 at 09:15 AM.
-
Jan 15th, 2009, 07:26 AM
#7
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
 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?
-
Jan 15th, 2009, 08:42 AM
#8
Thread Starter
Lively Member
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.
Last edited by ShaunC193; Jan 15th, 2009 at 08:45 AM.
-
Jan 15th, 2009, 09:12 AM
#9
Hyperactive Member
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
 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. 
 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?
Last edited by Hamish; Jan 15th, 2009 at 09:15 AM.
-
Jan 16th, 2009, 09:37 AM
#10
Thread Starter
Lively Member
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.
Last edited by ShaunC193; Jan 16th, 2009 at 09:41 AM.
-
Jan 16th, 2009, 10:00 AM
#11
Hyperactive Member
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]
Last edited by Hamish; Jan 16th, 2009 at 10:11 AM.
-
Jan 16th, 2009, 10:26 AM
#12
Thread Starter
Lively Member
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?

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!
-
Jan 16th, 2009, 10:34 AM
#13
Hyperactive Member
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.
-
Jan 16th, 2009, 10:43 AM
#14
Thread Starter
Lively Member
Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code
...You fixed it. 
I'm an idiot - I can't believe I didn't even think of trying UberData.Staff. Hahaha. Thanks. It works flawlessly now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|