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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each Question 1

Status
Not open for further replies.

gleeb

IS-IT--Management
Feb 10, 2004
19
US
Hi I am cycling through text boxes on a form with

For Each HoldText As Control In Me.Panel1.Controls
If TypeOf (HoldText) Is TextBox Then
If HoldText.Modified Then
PatientHasChanges = True

I get an error that 'Modified' is not a member of 'System.Windows.Forms.Control'. This is understandable.

If I try this:
For Each HoldText As TextBox In Me.Panel1.Controls
If HoldText.Modified then
PatientHasChanges = True

It will work unless there is a non-textbox control on the Panel such as a DataGridView.

Does anyone know of a nice way to be able to just check if any textboxes on a penal were modified?

Thanks
 
Hmm That really doesn't work. I get this error:

Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.TextBox' on the If line

This is the code:
For Each HoldText As Control In Me.Panel1.Controls
If CType(HoldText, TextBox).Modified Then
PatientHasChanges = True

If I code this:
For Each HoldText As TextBox In Me.Panel1.Controls
If CType(HoldText, TextBox).Modified Then
PatientHasChanges = True

I get the error on the For Each line
 
Code:
For Each HoldText As Control In Me.Panel1.Controls
  If HoldText.gettype is gettype(textbox) andalso _
     CType(HoldText, TextBox).Modified Then
    PatientHasChanges = True

Check the type of the control to make sure you are working with a text box before checking the .Modified property.

AndAlso is a shortcut opperator. If the first condition fails, the second condition will not be checked.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Alternatively you can continue to use TypeOf (which I personally prefer) as follows:

Code:
  Private Sub buttonCheckFields_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCheckFields.Click

    Dim PatientHasChanges As Boolean = False
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox AndAlso _
         CType(HoldText, TextBox).Modified Then
        PatientHasChanges = True
        Exit For
      End If
    Next
    If PatientHasChanges Then
      'process data here
    End If
    MessageBox.Show("Patient has changes = " + PatientHasChanges.ToString)
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox Then
        CType(HoldText, TextBox).Modified = False
      End If
    Next

  End Sub


Hope this helps.

[vampire][bat]
 
Lets make that even better.

Code:
Private Sub buttonCheckFields_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCheckFields.Click

    Dim PatientHasChanges As Boolean = False
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox AndAlso _
         CType(HoldText, TextBox).Modified Then
        MessageBox.Show("Patient has changes")
        'process data here
        Exit For
      End If
      MessageBox.Show("Patient has no changes")
    Next
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox Then
        CType(HoldText, TextBox).Modified = False
      End If
    Next

  End Sub


Christiaan Baes
Belgium

"My new site" - Me
 
We have now reached the point where there is no need for the variable PatientHasChanges, so ...

Code:
Private PatientHasChanges As Boolean

Private Sub buttonCheckFields_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCheckFields.Click

    PatientHasChanges = False
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox AndAlso _
         CType(HoldText, TextBox).Modified Then
        PatientHasChanges = True
        MessageBox.Show("Patient has changes")
        'process data here
        Exit For
      End If
      MessageBox.Show("Patient has no changes")
    Next
    For Each HoldText As Control In Me.Panel1.Controls
      If TypeOf HoldText Is TextBox Then
        CType(HoldText, TextBox).Modified = False
      End If
    Next

  End Sub

... at least gives a reason for the existence of the variable - and enables the fact that data has changed to be be used by other parts of the form.


Hope this helps.

[vampire][bat]
 
Ah yes, but if the OP had used MVC then that would be the task of the bussinesobject(s).



Christiaan Baes
Belgium

"My new site" - Me
 
Ok, thanks for the help. Now I'm curious what's MVC? I am having a hard enough time learning the fifty million ways to accomplish something and narrowing it down to the 10 that work best depending on who you talk to or what you read in any given day. "Don't bind", "use datasets and data adapters", "dont use data adapters", "use classes", "dont bind", "bindings ok", You get the idea.
 
Try this book.

"Professional design patterns in VB.NET: Building Adaptable Applications" by Tom Fischer, John slater, Peter Stromquist and Chaur G. Wu

And try looking for the CSLA.net framework from Lhotka. An reading his books. You will learn a lot from it.

"Don't bind", "use datasets and data adapters", "dont use data adapters", "use classes", "dont bind", "bindings ok"

Are all true but it depends the situation. But in your case I think you needed an object patients that had a property changed so that way you can just check if your patient has changed by calling the changed property (it's not that simple but you get the main idea). And you can make your bussinesobjects bindable.



Christiaan Baes
Belgium

"My new site" - Me
 
Thanks for the recommended books. I do have a patient object which I instantiate upon opening the form. I assume I would need to bind the objects members to the textboxes to detect changes in the object and update the Db if necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top