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!

Is there an easy way of reseting a form?

Status
Not open for further replies.

faust13

Programmer
Aug 7, 2001
176
US
Is there an easy way of reseting a form? I need to reset the form back to its initial runtime state before the user did anything to it (i.e. typed in text boxes, populated arrays, etc.).

Thanks! --------------------------------------
Is George Lucas Kidding...
Noise Core: 7.62
 
I have always made a "setdefault" function and for everything that can change I have the default setting in this function. You can also do some short cuts like:

Public Sub EmptyTextBoxes(ByRef frm As System.Windows.Forms.Form)
Try
Dim ctrl As System.Windows.Forms.Control
For Each ctrl In frm.Controls
If TypeOf ctrl Is System.Windows.Forms.TextBox Then
ctrl.Text = ""
End If
If TypeOf ctrl Is System.Windows.Forms.Label Then
'Labels with prefix lblCap are not to be changed.
If Mid(ctrl.Name, 1, 6) <> &quot;lblCap&quot; Then
ctrl.Text = &quot;&quot;
End If
End If
Next
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub


Public Sub EmptyListView(ByRef lv As System.Windows.Forms.ListView)
Try
Do While lv.Items.Count > 0
lv.Items.RemoveAt(0)
Loop
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub

I hope this helps. You could also close the form and reopen it.

Kris
[pc3]
 
There is no real disadvanage of doing so but it can be &quot;processor and memory hungry&quot;, if you are geting data from the database, or genneraly do something intense on the onload of the form.

Camel
 
just an aside. I have done the closing and reopening of forms as well for resetting. The biggest problem I ran into is even though you think the form is gone from memory it still stays resident.

john.
 
Hello there,
In some controls (e.g. Textbox) there is a method ResetText.
You can always use those to reset them to a 'blank' state).

Please note that if you have set the text of (say) the textbox to &quot;HelloWorld&quot; on your on load event, after the reset it will be blank

Camel
 
I just found this code from Microsoft's &quot;101 VB.NET Samples&quot;

' Refresh the data by telling the form to reload. This will get data from the
' database without sending any changes. All changes are abandoned.
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
' Simply tell the Form to reload
frmMain_Load(Me, New System.EventArgs())
End Sub

I have not tried it, but when I saw it I remembered of this post.

Kris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top