Solved

Clearing all fields in another form

Posted on 2004-08-15
12
193 Views
Last Modified: 2010-04-23
Hi,

I've made 2 forms; Application and Results, where Results displays the information entered in Application. Application has a public sub (btnClear_Click) that clears all fields in the form, which I allso want to run from Results, clearing the fields in Application.

(The point is by clicking OK in Results it clears all the fields in Application and then Results closes itself)

Please help if u can, I need to solve this for an assignment due today(!!)

Cheers!
0
Comment
Question by:Qiddees
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Hi there..
in your Results form, inside the click event handler of OK button, use...

Dim ApplForm As New Application
ApplForm.btnClear_Click(Nothing, Nothing)

-Baan
0
 

Author Comment

by:Qiddees
Comment Utility
Hi, thanks for your quick reply!

I did as suggested but Application does not clear its fields (Results form closes as it's supposed to do after Application has cleared).

Clicking on the clear button in the Application form still works. This is the code for it:

-----------------
Public Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtSurname.Text = ""
        txtDate.Text = ""
        txtForenames.Text = ""
        txtContactNo.Text = ""
        txtPostalCode.Text = ""
        cbNationality.SelectedIndex = -1
        txtPassportNo.Text = ""
        txtAddress1.Text = ""
        txtAddress2.Text = ""
        txtEducation.Text = ""
        rbDivorced.Checked = False
        rbFemale.Checked = False
        rbMale.Checked = False
        rbMarried.Checked = False
        rbSingle.Checked = False
        cbCourse.SelectedIndex = -1
End Sub
--------------

Could this has something to do with it?
0
 

Author Comment

by:Qiddees
Comment Utility
Third line in last post should be:

I did as suggested but Application does not clear its fields (Results form closes as it's supposed to do after trying to clear Application fields).
                                                                                                                                                         ^^^^^^^^^^^^^^^^^^^^^
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Or you can handle it all from the Application form by delcaring a variable of type Results as WithEvents, allowing you to trap when the Results form has been closed and call btnClear_Click() as shown below.

Public Class Application
    Inherits System.Windows.Forms.Form

    Private WithEvents r As Results

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        r = New Results
        r.Show()
    End Sub

    Private Sub r_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles r.Closed
        btnClear_Click(Nothing, Nothing)
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    End Sub

End Class

0
 

Author Comment

by:Qiddees
Comment Utility
Sounds even better!
How do I declare a variable as WithEvents?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Hi again,
why don't you use MODULE ?  Instantiate all the forms there. Then you can access any form from any other form.
Add a Module to your project. and write this inside... Module...End Module.

Module MyModule1
    Public F1 As New Form1
    Public F2 As New Form2

    Sub main()
        F1.showdialog()
    End Sub
End Module

Then to access F1's subs from F2, just use..
F1.Btn_Clear(Nothing,Nothing)

-Baan
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It's all in my previous post.  =)

If you want the Results form to be Modal (meaning you can't continue with or select any other form in your application until the Results form is closed) then it would be much simpler to open it using ShowDialog() like this:

Public Class Application
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As Results = New Results
        ' Pass info to your results form...
        r.ShowDialog() ' Code will not continue until Results is closed...
        btnClear_Click(Nothing, Nothing) ' Clear the current Application form...
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    End Sub

End Class
0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility

Your are thinking to difficult.

> you make 2 forms. One main (FormApplication) and one other (FormResults).


Try to explain it as simple as possible:


> Put this code in the form FormResults:
----------------------------------------------------------------------------------------
    Public myCaller As FormApplication
----------------------------------------------------------------------------------------



> Then put this into your main form (Application) in the on_load_event :

----------------------------------------------------------------------------------------
        Public FormFormClone As Form_Result = New FormResult
        FormResultsClone.myCaller = Me
----------------------------------------------------------------------------------------

> Make the from FormResultsClone visible (or hide it) using this code (put this under a button_click_event in FormApplication:
----------------------------------------------------------------------------------------
   FormResults.show
   FormResultsClone.hide
----------------------------------------------------------------------------------------


Now you will be able to access from your Application the form Results, but also the way round!


So you can e.g. clear all textboxes in FormResults from FormApplication by this code:
----------------------------------------------------------------------------------------
FormResultsClone.textbox1.text = ""
...
..
.
FormResultsClone.textbox9.text = ""
----------------------------------------------------------------------------------------


Try it and maybe you like it!

Daniël
0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility

Sorry made a mistake, the code

   Public FormFormClone As Form_Result = New FormResult

Must be outside(!) the on_load_event of FormApplication...


--------------- furthermore:

The code behind the OK button on the FormResult should be:

   FormApplication.btnClear_Click


And add this code to the Sub  btnClear_Click:

  FormResultsClone.hide
-------------------------------------------------------------------------

0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility
too bad..

in my last post:

  FormApplication.btnClear_Click

must be:

  mycaller.btnClear_Click
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 500 total points
Comment Utility

okay.. I made you an example:

     http://www.danieltrommel.com/download/expl02.zip

unzip this solution and you'll know.
0
 

Author Comment

by:Qiddees
Comment Utility
Hi! Thanks for all your effort!!

I had to hand in the assignment without solving the problem yesterday, but I've tried it now and it works perfectly!

Cheers! :)

-Qiddees-
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now