Question programmatically add text to a databound textbox

jamie123

Well-known member
Joined
May 30, 2008
Messages
82
Programming Experience
Beginner
I have a textbox databound to my binding source. It changes its value whenever my combo box changes its value, they're both bound to the same source. On one option on my combo box, i'd like to be able to programmatically change the value of my textbox, this is what I have right now but it does nothing but retrieve the value from its datasource.

If cboDesc.Text = "Sales Tax" Then
txtFee.Text = dblTax

End If

cboDesc is my combo box
"Sales Tax" is the string in which when selected I want to change the txtFee.text's value
txtFee is the text box databound

thanks for the help!
 
Sure, I have a textbox and a combo box that are both databound to the same binding source, as i cycle through the combo box, the text box goes through the associated values. Example, combo box is on James, text box is on 1, combo box moves to Mike, text box is on 2. Let's say I have a third name, Matt. Matt is in the binding source, just like James and Mike, but instead of displaying its value in the textbox that would be 3, I don't want it to display that, instead, when the combobox moves to Matt, I would like it to display "5". So i use the event that i posted above to determine when the combo box is on Matt, and then programmatically add the text to the text box as "5", like this:

If cboDesc.Text = "Matt" Then
txtTextBox.Text = 5
End If

But that does not work, instead it still displays the binding source data
 
Your question still is not quite clear. I'll assume the numbers you mention (1 for James, 2 for Mike) are the Primary Key values of the record. If this is true, you won't and shouldn't change that value.
If this is an incorrect assumption, please be clear with what you are asking. Perhaps you could explain what you would like your program to do and a better solution could be found.
 
No they're not the primary key, what else do you need? i''ll give you some code but i don't know what else to explain. Maybe this will help, this is the simplest way I can put the question:

I want to know how to programmatically (textbox.Text, in code) add text to a databound text box, because when I try it, it won't let me. Is this usually the case? If i have a command to programmatically add text to a databound text box, should it work? Because that's exactly what I'm trying to do, no strings attached.

Here is my exact code.


Private Sub cboDesc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDesc.SelectedIndexChanged
If cboDesc.Text = "Sales Tax" Then
txtFee.Text = Form1.dblTax
End If
End Sub


cboDesc is databound to FinancialBindingSource
txtFee is databound to FinancialBindingSource
both were databound in the properties pane
Neither of the are primary keys. The values that are in both cboDesc and txtFee are fixed, like I said, they are databound. When I get to the value "Sales Tax" on cboDesc, txtFee.Text shows up as 0.00, because it has no value in the database. This is because the value is calculated on my form. I want to set txtFee.Text to dblTax when cboDesc displays "Sales Tax". That's all I want to do. What I have above does this:
1. cboDesc.Text displays "Sales Tax"
2.The value dblTax passes through to txtFee.Text ( I see it as I set breakpoints)
3.The subroutine finishes, I look at the form, and txtFee.Text still displays its databound value "0.00"

Please forgive me if I failed to provide enough info, this is my first time programming and i'm trying to give as many details as i feel relevant.

Thanks for the help!
 
comboBox.Text is only relevant in situations where the combi is being used in DropDown mode. Given that you've bound your combo and you appear to be using it to nav across rows I'd suggest that you have made it a DropDownList.. in this case you'll need to use one of the other properties, like SelectedValue.. Use the debugger, pause the app and have a look at the local external proeprties of the combo in context
 
Well could that be the problem? Because like I said, when i set breakpoints, it stops on "Sales Tax" and sets the txtFee.Text to dblTax, I watch it do that, and it tells me txtFee.text is equal to dblTax's value,b ut then it never shows up and i'm guessing this is because the text box is databound and its databound value is overwriting its programmatic value.
 
all i wanna know is if a textbox is databound, how can i programmatically write something else in it and have it show up?

that's all i want to do.

thanks!
 
i normally write the data into the underlying data model to which the textbox is bound, then call resetbindings on the relevant bindingsource, passign a False to indicate that data only has changed. ymmv
 
textbox -> bindingsource -> mytypeddatatable


arrow represents binding
column bound to is called Col1


DirectCast(DirectCast(bindingsource.current, DataRowView), MyTypedDataTableRow).Col1 = "New value"
bindingsource.ResetBindings(false)
 
textbox -> bindingsource -> mytypeddatatable


arrow represents binding
column bound to is called Col1


DirectCast(DirectCast(bindingsource.current, DataRowView), MyTypedDataTableRow).Col1 = "New value"
bindingsource.ResetBindings(false)

I'm using a dataset and strongly typed data adapters, i hate to sound annoying but I have no idea how to re-structure that into a dataset accommodating form, could you please re-word with datasets and table adapters instead of datarows? or does it even matter?


Also, I have had some progress on my own, using this code:

Private Sub cboDesc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDesc.SelectedIndexChanged
If cboDesc.Text = "Sales Tax" Then
EbtblsDataSet.AcceptChanges()
MsgBox(Form1.dblTax)
txtFee.Text = Form1.dblTax
txtFee.Refresh()
cboDesc.Refresh()
txtFee.Focus()
txtCPT.Focus()
End Sub

It now displays the dblTax in the textbox, like I want it to, the only thing is when it does this it resets cboDesc back to its original databound value, instead of staying on one of its databound options, "Sales Tax" , don't know why or how to fix that
 
I gave you the code:

DirectCast(DirectCast(bindingsource.current, DataRowView), MyTypedDataTableRow).Col1 = "New value"
bindingsource.ResetBindings(false)


Granted I didnt know what you called your bindingsource or column, nor what value you wanted so I did require you to put some effort in. If you'd like me to come and write your program for you, there would be a charge for that :)

Quit it with all that refreshing and focusing and things! You'll make your UI very annoying to use!
 
Back
Top