Can't read textbox value when Visible = false

I have a winform where I read the value of a textbox in code.

It works fine when the control is visible. But, if I set visible = false, I get a null reference error.

I was hoping to have hidden textboxes for internal reference without cluttering the form. Does Visual Studio not allow that?
KapriceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rspahitzCommented:
Windows Application Forms definitely allow you to access invisible controls.  Is this a form control or a web control (not sure how visible property works on web controls.)

Can you show some of the code around where the error occurs when hidden?

Another option is something like this (where TB is the name of the text box):

TB.visible = True
tbValue = TB.Text' save the value for use elsewhere
TB.visible = False

Usually this will not impact visibility to the user unless you refresh in between
KapriceAuthor Commented:
It's a slight bit convoluted.

I'm using DevExpress Winforms validation.

So, a validator gets passed a control and a custom class. The custom class gets passed the control and the control's value.

In this particular case, my validation must read a different (hidden) control in addition to the one passed as a parameter. Because it's a separate class, I found I could not reference the form's controls, directly. Thus I have the control.FindForm call.

Here's my custom class.  It works perfectly when TicketIDTextEdit is visible. Fails (error below the code) when the textbox is hidden. No other changes to either data or code.

	Public Class TicketNbrValidationRule
		Inherits ValidationRule

		Private mydataContext As New PanTicketsDataContext


		Public Overrides Function Validate(control As Control, value As Object) As Boolean
			' Must be unique across entire MemberPanTickets table
			Dim bReturn As Boolean = False
			Dim parentForm As FrmTicketForm = control.FindForm()

			If String.IsNullOrEmpty(value.ToString) Then
				Me.ErrorText = "The Ticket Number is required."
			Else

				Dim ticketCount = (From tick In mydataContext.MemberPanTickets _
											Where tick.TicketNbr = value.ToString _
											And tick.TicketID <> parentForm.TicketIDTextEdit.Text _
											Select tick.TicketNbr).Count

				If (ticketCount = 0) Then
					bReturn = True
				Else
					bReturn = False
					Me.ErrorText = "That Ticket ID has already been used."
				End If

			End If


			Return bReturn
		End Function
	End Class

Open in new window


The error:
System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Conversion from string "" to type 'Double' is not valid.
  Source=System.Data.Linq
rspahitzCommented:
And if the control is visible, it works?  The error is strange.  Why is it trying to cast a string to double?  Do you know what line that is? I wonder if it's a bug in the DevExpress piece (although I'm not familiar with that so not sure.)
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

KapriceAuthor Commented:
Yep. It works when visible.

The line is the Linq call. I thought the error was odd, too. I figure it has to do with assumptions linq is making.

I'll try setting the form's value to a variable outside the linq line and use the variable inside it.

The textbox IS a DevEx bound textbox, so I suppose it's possible DevEx is keeping invisible controls from being readable. But, that just seems silly.
KapriceAuthor Commented:
I changed the code to this:

				Dim tickId As Long = parentForm.TicketIDTextEdit.Text
				Dim ticketCount = (From tick In mydataContext.MemberPanTickets _
											Where tick.TicketNbr = value.ToString _
											And tick.TicketID <> tickid _
											Select tick.TicketNbr).Count

Open in new window


resulting in this error:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "" to type 'Long' is not valid.

so, it's not Linq -- other than Linq guessed it was a double. In my modified code, I declared it as long.

Note that the error shows the value (that it's trying to convert) is "". So, it's clearly not reading the textbox when it's hidden.
rspahitzCommented:
So do you have the ability to show the text box, run your code, then hide the textbox?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KapriceAuthor Commented:
I'll experiment with that and report back.
KapriceAuthor Commented:
Good call. Changing my code to this (where I set visible on, read the value, then set visible off) works great. No flashing on screen, either.

				parentForm.TicketIDTextEdit.Visible = True
				Dim tickId As Long = parentForm.TicketIDTextEdit.EditValue
				parentForm.TicketIDTextEdit.Visible = False
				Dim ticketCount = (From tick In mydataContext.MemberPanTickets _
											Where tick.TicketNbr = value.ToString _
											And tick.TicketID <> tickId _
											Select tick.TicketNbr).Count

Open in new window

KapriceAuthor Commented:
Thank so much for your prompt help.
rspahitzCommented:
Great!  I was hoping the screen wouldn't flash,otherwise we'd have to set ScreenUpdate false.
Glad you got it working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.