Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array of contol data validations - need direction 1

Status
Not open for further replies.

kimprogrammer

Programmer
Sep 15, 2008
160
CA
Hello
Could I get some direction.
I created array of controls. There is an hour textbox, minute combobox and a reason combo box.
I have created handlers and validation for the hour textbox.
Works ok

But now I need to validate a hour,minute and reason combination.
Not quite sure how this works together - I have the code started

'
'validate Hours and reason codes data Entry
'
Private Sub cboAdjReason_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

'check if Absents
If (CType(sender, ComboBox).Text) = "ABSENT" Then
IF 'need to check of there is data in hours textbox but it is not cast
MessageBox.Show("testing", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
CType(sender, ComboBox).Text = ""
End If
End If

End Sub

Thanks for the help
Kim
 
Are you saying that you have an array which has one textbox and two comboboxes? Basically just three elements?

OR

do you have an array of textboxes for hours, an array of comboboxes for minutes and an array of comboboxes for reasons?
 
Basically the first one; an array with one textbox and two comboboxes. But I do have three lines of this within the array.

So I have an array 14 in length - looks like this:
txthours1(x) , cbominutes1(x), cboreason1(x)
txthours2(x) , cbominutes2(x), cboreason2(x)
txthours3(x) , cbominutes3(x), cboreason3(x)



Thanks for the help
Kim
 
Well, with them all being in the same array, when you cast to ComboBox in your event handler, it won't work when a textbox is the sender.

I would probably create three handlers or separate it out to differentiate by control type.
 
So you mean one handler to take care of
txthours1(x) , cbominutes1(x), cboreason1(x)

one to take care of
txthours2(x) , cbominutes2(x), cboreason2(x)

and another to take care of
txthours3(x) , cbominutes3(x), cboreason3(x)

If this is what you meant I still not sure how to connect/validate the three fields together
txthours1(x) , cbominutes1(x), cboreason1(x)



Thanks for the help
Kim
 
I think I understand the addhandler

I have set one up already to validate the hours. Below is the code and it works.
I think maybe it's the cast(CType) and/or sender I don't quite understand.

AddHandler Me.txtAdjHour1(x).Validating, AddressOf txtAdjHours_Validating
AddHandler Me.txtAdjHour2(x).Validating, AddressOf txtAdjHours_Validating
AddHandler Me.txtAdjHour3(x).Validating, AddressOf txtAdjHours_Validating
-----
'
'validate Hours data Entry
'
Private Sub txtAdjHours_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

'check if hours are within range
If (CType(sender, MaskedTextBox).Text) <> "" Then
If Decimal.Parse(CType(sender, MaskedTextBox).Text) > 24 Or Decimal.Parse(CType(sender, MaskedTextBox).Text) < (-24) Then
MessageBox.Show("Hours are not within range (24 to -24)", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
CType(sender, MaskedTextBox).Text = ""
End If
End If


calculate_Totals()

End Sub
---------------------

Thanks for the help
Kim
 
Well, sender is an object. When an control raises an event, it typically passes in sender which refers to itself. But since it's not typed to the appropriate subclass, you need to convert it to the specific type of control to access the control specific properties.

Keep in mind that if you end up with a larger array, sometimes it will be easier to set the event handlers in a loop. For example:

Code:
For Each tb As TextBox In MyTextBoxArray
  AddHandler tb.Validating, AddressOf....
Next
 
Yes but in the case of what I want to do - I have the addhandler if there is a change to the reason combobox and then it gets sent into the validation routine. But how do I send the right textbox with the hours into the subroutine so I can check it?

Thanks for the help
Kim
 
Maybe
Do I set up another Ctype to get the index of sender and then use the name of the txtbox with that sent index in the if statement

Thanks for the help
Kim
 
Yes but in the case of what I want to do - I have the addhandler if there is a change to the reason combobox and then it gets sent into the validation routine. But how do I send the right textbox with the hours into the subroutine so I can check it?

I don't understand this part. Are you talking about the calculate_Totals() routine? If so, can you post the code for that?
 
No I'm not talking about the totals

I understand how the validation was done with a handler attached to one field when it changes. ie the hours field gets modified and it goes to the hour field validation.

So with the reason code if it changes it needs to go to the reason code validation. But I don't understand how I tie the matching hours field in the validation

I need something like: if reasoncode = "absent" and hours <> 8; put out an error

I've cast the reason code into validation routine. But I wasn't quite sure about what I do with the hours.

I thought maybe I do another cast that would give me the array number of the reason code and I could use that to find the corresponding hours field.

Thanks for the help
Kim
 
I think what you are saying is that you have sets of controls. Each set has Hour, Minute and Reason?

I would go ahead and make a routine to call which validates all of the sets each time.

For example:

Code:
For i As Integer = 0 To NumberOfElementsInYourArray - 1 
  If HourTextBoxes(i).Text.Trim <> "8" And ReasonComboBoxes(i).SelectedValue.ToString <> "absent" Then
  'Do something
Next

Note that I used different names for your Control arrays for illustration purposes.

If you need to notify the user which set is invalid, the simplest solution would be to highlight the backcolor of each control in the invalid set(s) to something like yellow. Then, before the your code hits the For loop, set them all back to default.
 
RiverGuy has you taken care of on this one, but I would add one thing. You may have your reasons for an array, but personally generally I would go with a collection or in this instance a ControlCollection. They are just much more versatile. If that is the scope of what you want to do with them then I wouldn't bother changing it now as it may just make it more confusing to switch, but I would look into them for the future.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
On second thought I would just use a collection instead of a ControlCollection for ease of use.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
River Guy - Ok thank you - that was what I was looking for. I thought you could/should only put error checking within a validation routine when a field was changed or selected. It was just the approach I needed to understand.

Sorwen - thanks for the info on collections - when I first started the project the only info I came across to do this (have multiple controls that worked the same way)was to create an array of panels with controls - so my screen is almost complete so it would make no sense to change now.
And when you have no training or experience with vb.net programming and you are training yourself to do a project you don't quite know the best way to design it because you've never seen how it all works together. But should I ever get the opportunity to work with vb.net again I will look into collections (but hopefully it will be with Senior vb.net developers who can help guide the way so I can build something in a more optimum manner).

Thanks for the help
Kim
 
No problem. Keep this one idea in mind. It's quite common that event handlers will not have much code. Instead, they often call other routines where most of the processing occurs, passing in parameters sometimes.
 
I can understand those first steps. Yeah, if it isn't broken then don't fix it. :) Someone pointed me to collections when I needed to ReDim any array and could not. For somethings they are a lot better to use other times it's just easier to deal with an array.

I found this place when I was working in VBA and stumbled over to this part when I had to make the switch and was totally on my own. There is a great bunch of people here that helped me out back then and still do from time to time. Self taught on a lot of things for .net means there are a lot of holes because I just never needed to think about some things before. I'm a semi-graduate of Tek-Tips U. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen - Then I must be in my first year of Tek-Tips U. And I agree that everyone that has help me on this site has been great. Although I'm sure I've frustrated a few with my lack of knowledge. Because only having experience as a cobol mainframer - and then being dropped into this OOP and VB.net and SQL server, crystal reports world -all at the same time - all on my own with three books and your site has been an experience.

Thanks for the help
Kim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top