all groups > dotnet windows forms databinding > august 2006 >
You're in the

dotnet windows forms databinding

group:

Cannot read value from invisible textbox control


Cannot read value from invisible textbox control MartinMCU
8/27/2006 4:54:01 PM
dotnet windows forms databinding:
Hello All,

I am developing a Windows Form to allow master/child records to be viewed
and edited in a SQL Server 2005 Database. I am developing in Visual Basic
2005, and am running into a bit of a problem. Simply put, I can not get the
value from a hidden textbox control on the form.

This form is laid out such that the master record is displayed in individual
textbox fields, with its child records being displayed in a DataGridView
control on the same form. There are three fields on this page that I want to
hide: The primary key of the master record, the primary key of the child
records, and the foreign key of the child records. For this to work, however,
the value of the master primary key has to be inserted into the foreign key
field of new child records. The code I used for this is given below:

Private Sub TblMedicationsPricesDataGridView_DefaultValuesNeeded(ByVal
sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewRowEventArgs) Handles
TblMedicationsPricesDataGridView.DefaultValuesNeeded
With e.Row
.Cells("fkMedicationIDColumn").Value = Me.PkMedicationIDTextBox.Text
End With
End Sub

where Me.PkMedicationIDTextBox is the textbox on the form that displays the
master record's primary key, and e.Row.Cells("fkMedicationIDColumn") is the
foreign key field of the child records. When Me.PkMedicationIDTextBox is
visible on the form, this code works perfectly. However, upon setting
Me.PkMedicationIDTextBox.Visible = False, the following exception is
generated:

The following exception occurred in the DataGridView:

System.Exception: is not a valid value for Int64. --->
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
And then it contiues with more that I don't care to type out....

What is happening is that I am getting Me.PkMedicationIDTextBox.Text = NULL,
which obviously doesn't convert to an integer very well. So, what is the best
way to solve this little dilemma? I tried the following code, but it doesn't
always return the correct value for the foerign key, and thus child records
get saved to an incorrect parent.

Private Sub TblMedicationsPricesDataGridView_DefaultValuesNeeded(ByVal
sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewRowEventArgs) Handles
TblMedicationsPricesDataGridView.DefaultValuesNeeded
With e.Row
.Cells("fkMedicationIDColumn").Value =
Me.TblMedicationsBindingSource.Current.Row.fkManufacturerID.ToString()
End With
End Sub

Any thoughts? Or is there a better way to retrieve the foreign key value
than reading from a hidden textbox? Personally, I'd rather not have the extra
control mucking up that form in design view anyways. Thanks in advance!

Re: Cannot read value from invisible textbox control Bob Powell [MVP]
8/31/2006 9:11:58 PM
Text box controls are a very loose wrapper around the Win32 text box. The
data is stored in the underlying window. When the textbox is invisible the
underlying window doesn't exist so there's nowhere to store the data...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



[quoted text, click to view]

Re: Cannot read value from invisible textbox control George
9/7/2006 5:43:49 PM
I have need to hide certain info on my form, too. I use labels and drop them in a corner of my groupbox.

Rather than turn the visibility off, I set the label's forecolor and backcolor to equal the groupbox's backcolor so they can't be seen by the user, or even by me when I'm in the designer. If I move the groupbox, the hidden labels automatically go along with it.

These labels are bound to a table, so, if I manually change the contents of the label, I have to manually set focus to the label and then set focus somewhere else for it to update the table with the new contents.

May not be the best way of doing things, but seems to be working okay, so far.
---
AddThis Social Bookmark Button