473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Does WinForm return anything?

Ken
How can I have my WinForm return something to the caller so it knows which
button is clicked? The following is calling a system message box, just want
to do the samething with my own winform. Thanks.

If MsgBox("Data added.", MsgBoxStyle.OKCancel, "Message") = MsgBoxResult.OK
Then
' do something

End If
Nov 21 '05 #1
3 2810
Ken,

"Ken" <Ke*@discussions.microsoft.com> schrieb:
How can I have my WinForm return something to the
caller so it knows which button is clicked?


Set the form's 'DialogResult' property and check the value of this property
after closing the form:

\\\
Dim f As New FooForm()
Select Case f.ShowDialog()
Case DialogResult.OK
...
Case DialogResult.Cancel
...
Case...
...
...
End Select
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
In article <43**********************************@microsoft.co m>, Ken wrote:
How can I have my WinForm return something to the caller so it knows which
button is clicked? The following is calling a system message box, just want
to do the samething with my own winform. Thanks.

If MsgBox("Data added.", MsgBoxStyle.OKCancel, "Message") = MsgBoxResult.OK
Then
' do something

End If

Simple example...

Create a solution with two forms, form1 and form2 copy in the following
code:

' Form1 Code
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(196, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "&Show Custom Dialog"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(212, 34)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dialog As New Form2
Dim result As DialogResult = dialog.ShowDialog()
Select Case result
Case DialogResult.Abort
MessageBox.Show("Abort Selected")
Case DialogResult.Cancel
MessageBox.Show("Cancel Selected")
Case DialogResult.Retry
MessageBox.Show("Retry Selected")
End Select
End Sub
End Class

'Form2 Code
Imports System.Windows.Forms
Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.DialogResult =
System.Windows.Forms.DialogResult.Abort
Me.Button1.Location = New System.Drawing.Point(8, 12)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Abort"
'
'Button2
'
Me.Button2.DialogResult =
System.Windows.Forms.DialogResult.Cancel
Me.Button2.Location = New System.Drawing.Point(92, 12)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Cancel"
'
'Button3
'
Me.Button3.DialogResult =
System.Windows.Forms.DialogResult.Retry
Me.Button3.Location = New System.Drawing.Point(176, 12)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 2
Me.Button3.Text = "Retry"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(262, 48)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form2"
Me.ShowInTaskbar = False
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
End Class

HTH
--
Tom Shelton [MVP]
Nov 21 '05 #3
Ken
Very well. Thank you all.

"Tom Shelton" wrote:
In article <43**********************************@microsoft.co m>, Ken wrote:
How can I have my WinForm return something to the caller so it knows which
button is clicked? The following is calling a system message box, just want
to do the samething with my own winform. Thanks.

If MsgBox("Data added.", MsgBoxStyle.OKCancel, "Message") = MsgBoxResult.OK
Then
' do something

End If

Simple example...

Create a solution with two forms, form1 and form2 copy in the following
code:

' Form1 Code
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(196, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "&Show Custom Dialog"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(212, 34)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dialog As New Form2
Dim result As DialogResult = dialog.ShowDialog()
Select Case result
Case DialogResult.Abort
MessageBox.Show("Abort Selected")
Case DialogResult.Cancel
MessageBox.Show("Cancel Selected")
Case DialogResult.Retry
MessageBox.Show("Retry Selected")
End Select
End Sub
End Class

'Form2 Code
Imports System.Windows.Forms
Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.DialogResult =
System.Windows.Forms.DialogResult.Abort
Me.Button1.Location = New System.Drawing.Point(8, 12)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Abort"
'
'Button2
'
Me.Button2.DialogResult =
System.Windows.Forms.DialogResult.Cancel
Me.Button2.Location = New System.Drawing.Point(92, 12)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Cancel"
'
'Button3
'
Me.Button3.DialogResult =
System.Windows.Forms.DialogResult.Retry
Me.Button3.Location = New System.Drawing.Point(176, 12)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 2
Me.Button3.Text = "Retry"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(262, 48)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form2"
Me.ShowInTaskbar = False
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
End Class

HTH
--
Tom Shelton [MVP]

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jdionne | last post by:
I have a WInform app that runs a DTS. When I developed it on the SQL box and executed it, it ran fine. I used the Trusted connection parameter for the DTS. I don't want to use passwords. I...
0
by: Thom Little | last post by:
..NET Framework 1.1 I have a C# WinForm that invokes a C# WebForm using System.Diagnostics.Process.Start( ) to invoke the default browser and access a specific URL. This works great but I can...
1
by: i | last post by:
Hi, I'm trying to get a seperate class, initialized by a form class, to manipulate certain objects on the form (ex: add to a listbox). The manipulation will occur via a thread that is not the...
2
by: Wayne | last post by:
I have a winform that runs on its own, is it possible to wrap a stand-alone winform in a class? More accurately, is it possible to use a class to call an already established winform?
5
by: Matthew Hood | last post by:
Here's a interesting question for the guru's out there. I've created a VB.NET class library with a couple of forms that I have successfully got to work from within MS Access using COM interop and...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
3
by: Nick Gilbert | last post by:
Hi, In my VS.NET 2005, if I choose Build Clean Solution, the BIN folder is not touched. Shouldn't it delete all the dll and pdb files in that folder first? Instead, I'm finding I have to do it...
6
by: WT | last post by:
Hello, Using VS2005. I have an assembly library that can be called from a Web site asp.net application or from a winform application. From this library I need to retrieve a path using simply a...
1
by: jabbari | last post by:
Hello, Please Help us...! I have a big problem ,so i searched on google and other search engine ,then I realized that so many other people have the same problem and they, all, have'nt been able to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.