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

Form Change 2

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
In vb6 I used code to loop through the objects in a form to see if they changed. If so, a boolean was set to true. something like this:
Code:
Public Sub FormChange(ByRef bFormChange As Boolean)
    Dim obj As Object
    For Each obj In frmSStab
        If TypeOf obj Is TextBox Then
            If obj.DataChanged Then
                bFormChange = True
                mdiMain.Caption = txtFirstname.Text & " " & txtLastname.Text & "*"
            End If
        End If
    Next obj
End Sub
However, when I use the same code in vs 2008 and irrespective of the form, I get 'express is of type ..., which is not a collection'. Is there a way to accomplish the same?

Thanks.
 
Something like the following is close to what you want:

Dim c As Control
For Each c In Me.Controls
MsgBox(c.Name)
Next

However, I would probably create event handlers for the controls to handle textChanged event and let that set the property on the form or take whatever action necessary.

J
 
Thanks for the response.

In vb6 I used the event handlers (I think) to call FormChange which then did the work. Is that what you mean?
Code:
Private Sub cboMemento_KeyPress(keyascii As Integer)
    Call FormChange(bFormChange)
End Sub

Private Sub cboProfession_Click()
    Call FormChange(bFormChange)
End Sub
 
Create an event handler for each object you want to know when its text changes. Inside the event handler, take whatever action you would like to update the form level property or caption:

1)For a text box, while in design view, double click the text box. You can also go to code view, find the desired object in the object list to the left, then choose an event from the drop down list on the right.
2)This will create an event handler for the textChanged event.
3)Inside this event handler, create the necessary code to execute when text is changed in this control. You can also use the same handler for multiple controls if you want similar functionality for multiple controls.
Code:
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'set a form level property denoting something has changed
'you will need to create the property for the form...
me.FormHasChanged = True
End Sub

There are other ways to accomplish this but this is most in alignment with your original code and question.
 

That will work, but you don't have to use a different event handler for each control. With .NET you can have one event handler assigned to multiple controls. Note that you do need a different event handler for each kind of event (e.g., click, keypress, etc.), and you can have more than one event handler for the same event on the same control, although I've yet to find a use for this. Here's an example using a button's Click event:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click

'do some stuff

End Sub

You can get the name of the control that triggered the event from the 'sender' property:

Dim sName As String

sName = sender.Name

So you can take different actions depending on which control was clicked.

Hope this helps.




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks to both for the responses. If I want to add a datagrridview and the text changes such adding a new row or changing the spelling in an existing row, would that be CellValueChanged?
 
It is sometimes difficult to answer a question without having full background. I initially answered the simple question you had suspecting you needed a more robust solution but not wanting to confuse you by complex examples.

My guess would be that you are probably binding your controls to a dataset? If not, you may want to investigate doing that. When they are bound to a dataset, you will have much of the functionality you are probably looking for within the dataset properties such as dataset.HasChanges, AcceptChanges, RejectChanges, etc. You may also want to read up on the BindingSource control.

If you are building a form to multiply two numbers, it may be enough to have event handlers tell you when the user changes value A or B so you can recalculate. If you have a complex form with many types of controls, validation and business logic, it is probably well worth the effort to implement some of the robust .NET controls designed for assisting with these tasks. They are fairly intuitive, easy to get working initially, but something you can definitely grow with going forward.

J
 
thanks for the response. No binding. To date I have not seen the need to. How does that change things?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top