Showing Windows Forms - question

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hi!

If Windows Form -application has for example a button on Form1 which
Click-event opens other Form2-form like...

Dim f as New Form2
f.ShowDialog()
If (f.DialogResult = DialogResult.OK) Then
SaveText(f.txtInput.Text)
End If
f.Dispose()

....and when OK-button was pressed on Form2, it saves content of the TextBox
on the Form2 before closing Form2.

This is working fine like this way for now. But I want to keep Form1 as an
editable form when Form2 is open, how to this?

I tried simply changing line f.ShowDialog() to f.Show(), but then code
execution is going to the end of the Sub, and I can't catch when OK was
pressed on the Form2. I thing this is simple question. Hopefully you
understand what I'm trying to explain :)
 
Since you are using ShowDialog(), the form will show up modally and you wont
be able to edit the calling form (Form1). You will have to use Show() only.
As for saving the text of a textbox on form2, you can do that in the Click
event of the OK button in form2. Since your SaveText method is in Form1, you
can pass a reference to Form1 into Form2 and make SaveText as a friend
method. Here's how you could do it (there are other ways too):

' All this in Form2

Private frmForm1 As Form1

' Constructor of Form2
Public Sub New(ByVal frmCalling As Form1)
frmForm1 = frmCalling
End Sub

' OK Button Click in Form2
Private Sub btnOK_Click(ByVal sender as Object, _
ByVal e As EventArgs) Handles btnOK.Click
frmForm1.SaveText(Me.txtInput.Text)
End Sub

' In Form1
Dim f As New Form2(Me)
f.Show()

That should pretty much do what you are looking for.

hope that helps..
Imran.
 
Mika,

It is a simple question yes, however the showdialog is very easy for this.
When there is few processing it will as well not be bad, however when there
is time involving processing in one of your forms you will need
multithreading.

Here a sample I made some weeks ago.
It shows 3 forms the main, and one normal and one with threading.

http://groups.google.com/[email protected]


I hope this helps?

Cor
 
Hi Cor and thanks for your reply!

I looked and tried that sample in Google and after ...

Private Sub Button1_Click(...

.... following lines ...

AddHandler frm1.ready, AddressOf Frm1Ready
AddHandler frm1.ready, AddressOf frm2ready

.... causes following errors ...

'ready' is not an event of 'FormTest.Form2'.
'ready' is not an event of 'FormTest.Form2'.

Maybe I'm missing something?
 
Mika,

Thank you for making me attent on this, it is because the event in form2 is
named frm2Ready while i changed it later to "ready" however not in the
sample in form 2.

Here the complete sample again (Than I can use this on Google again)

\\\needs on form 1 one button and three textboxes
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
AddHandler frm1.ready, AddressOf Frm1Ready
frm1.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
frm2 = New Form2
frm2.itstop = Me.Top
frm2.itsleft = Me.Left + 400
frm2.Text = "In own thread"
AddHandler frm1.ready, AddressOf Frm2Ready
frm2.Show()
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
TextBox2.Text = message
frm1.Close()
MyThread.Abort()
End Sub
Private Sub frm2ready(ByVal message As String)
TextBox3.Text = message
frm2.Dispose()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MyThread.Abort()
End Sub
///
\\\Needs a form2 with one textbox
Friend Event ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
 
Back
Top