how to manage pass value set by dialog box in VB form application to its caller - vb.net

I have a form Main that calls open a dialog, which gets a text value response and that value would need to be used in Main. I was wondering how to achieve this where Main would have access to this after the dialog closes. The minimal example of this is as follows:
Private Class Main
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btnOpenDialog.Click
Dim dialog As New Dialog
dialog.Show()
End Sub
End Class
and Dialog
Private Class Dialog
Private response As String
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
response = txtResponse.Text
Me.Close()
End Sub
End Class

Just provide a pass-through property for the Text of the TextBox:
Public ReadOnly Property Response As String
Get
Return txtResponse.Text
End Get
End Property
It's hard to know for certain but you probably out to be displaying the form as a modal dialogue:
Dim response As String = Nothing
Using dlg As New Dialog
If dlg.ShowDialog() = DialogResult.OK Then
response = dlg.Response
End If
End Using
'...
Now you don't need a Click event handler for your Button as you can just set its DialogResult property to OK in the designer.

In that case the simple solution could be the following:
Private Class Main
Private Sub btnOpenDialog_Click(sender As Object, e As EventArgs) Handles btnOpenDialog.Click
Dim AskDialog As New Dialog
Dim Response As String
Response = AskDialog.DialogShow()
End Sub
End Class
and Dialog
Public Class Dialog
Private response As String
Public Function DialogShow() As String
Me.ShowDialog()
Return response
End Function
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
response = txtResponse.Text
Me.Close()
End Sub
End Class

Related

How to Trigger button click from another form VB Net

I have three forms in total and i want to trigger one of the button on form 3 to be triggered automatically when form 1 loaded
Form 1
Public Class frmIOMain
' In This Form load I want to trigger the above mentioned button
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' I want to Trigger the Above mentioned button here when my form is loaded
' But it is not working for me
frmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
End Sub
End Class
Form 2
Public Class TestEquipmentManagement
Public EquipementTable As New DataTable("EquipmentTable")
Public EquiTypeSelection As String
Public EquiManufacturerSelection As String
Public EquiList_PK As New List(Of Integer)
Dim targetEquipmentList As New List(Of Model.equipment)
Private equipDB As Model.Entities = Nothing
Public Shared viewManager As ViewManager
Private equipment As New List(Of Model.equipment)
'Dim WithEvents excNewPFM As New VBACom
Public EquipCalTable As New DataTable("EquipCalTable")
Public Sub New()
Dim todayplusoneyear As Date
todayplusoneyear = Date.Today
todayplusoneyear = todayplusoneyear.AddYears(1)
'Assign current db
equipDB = frmIOMain.db
End Sub
End Class
Form 3
Public Class frmUpdateDueDates
Private EquipmentUpdates As UpdateCalibrationsViewModel
Private _success As Boolean = False
Public Sub New(db As Entities)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
EquipmentUpdates = New UpdateCalibrationsViewModel(db, New CAQ23(), False)
'Add Handlers
AddHandler EquipmentUpdates.OnProgressChanged, AddressOf progressChangedHandler
AddHandler EquipmentUpdates.OnInfotextChanged, AddressOf infoTextChangedHandler
prgUpdates.Maximum = EquipmentUpdates.intProgressMax
End Sub
Public Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
End Class
I want "cmdUpdate_Click" Button which is on form 3 to be triggered when my form 1 is loaded
Can Anyone tell me how i can do that?
Firstly, create an instance of the form, instead of using its default form instance. Calling a click handler across forms isn't a good idea. The handler may use the arguments sender As Object, e As EventArgs and from outside of the containing class, you can't assume you know that. Better practice would be to create a method which performs the click within the form, such as
Public Class frmUpdateDueDates
Public Sub cmdUpdateClick()
cmdUpdate.PerformClick()
End Sub
Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
End Class
Public Class frmIOMain
Private myFrmUpdateDueDates As frmUpdateDueDates
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myFrmUpdateDueDates = New FrmUpdateDueDates()
myFrmUpdateDueDates.Show()
'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
myFrmUpdateDueDates.cmdUpdateClick()
End Sub
End Class
And you can change the access modifier of the click handler back to Private
Even better would be to put the work into a different method which the click handler calls. Then the other form doesn't even need to know the button exists. Such as
Public Class frmUpdateDueDates
Public Sub DoUpdating()
cmdUpdate.Enabled = False
_success = EquipmentUpdates.startUpdating()
cmdCancel.Text = "Close"
End Sub
Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
DoUpdating()
End Sub
End Class
Public Class frmIOMain
Private myFrmUpdateDueDates As frmUpdateDueDates
Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
myFrmUpdateDueDates = New FrmUpdateDueDates()
myFrmUpdateDueDates.Show()
'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
myFrmUpdateDueDates.DoUpdating()
End Sub
End Class

Underlines of MaskedTextBox disappear

i have a problem in Windows Forms. I've created a form where I can enter the first and last name and click on the Search-Button to show another form with the following code:
Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click
If Me._clsfrmChild Is Nothing Then
Me._clsfrmChild = New clsFrmChild
End If
If Me._clsfrmChild.ShowDialog = False Then
Me._clsfrmChild.ShowDialog(Me)
End If
In the second form I have a MaskedTextbox:
Empty MaskedTextBox
Whatever I do, If I close the second form with Visible = False and reopen it again, the MaskedTextBox looks like this:
MaskedTextBox without underlines
I close the second form that way:
Private Sub btnAbort_Click(sender As Object, e As EventArgs) Handles btnAbort.Click
Me.Visible = False
End Sub
Does someone of you know why this problem is caused?
This is really not the way you supposed to work with dialogs. The proper way is this
dim result as DialogResult
using f as Form = New MyForm()
result = f.ShowDialog()
' here you can work with form's public properties etc
end using
' optionally here you can continue massaging the result
if result = DialogResult.Ok then
' do for ok result
else
' do for other result. You can have severul results - add elseif
end if
Here is how to make a dialog with results in general. This will be similar to how MessageBox.Show() works.
Public Class clsFrmChild
Private Sub New()
InitializeComponent()
End Sub
Public Shared Shadows Function Show() As DialogResult
Dim result As DialogResult
Using f As New clsFrmChild()
result = f.ShowDialog()
End Using
Return result
End Function
Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click
DialogResult = DialogResult.OK
Close()
End Sub
Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
DialogResult = DialogResult.Cancel
Close()
End Sub
End Class
Note the constructor is privatized so there is no more Me._clsfrmChild = New clsFrmChild. You will see the displaying of the modal dialog is much simpler when called like MessageBox
Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click
Dim result = clsFrmChild.Show() ' static method call instead of instance method
Select Case result
Case DialogResult.OK
MessageBox.Show("Ok")
Case DialogResult.Cancel
MessageBox.Show("Cancel")
End Select
End Sub
If you are not interested in returning a standard DialogResult, you could change the return type to whatever you like such as a custom class, with more information (such as you want to return a string in addition to DialogResult) i.e.
Public Class clsFrmChildResult
Public Property Text As String
Public Property DialogResult As DialogResult
End Class
...
Public Shared Shadows Function Show() As clsFrmChildResult
Dim result As clsFrmChildResult
Using f As New clsFrmChild()
Dim dr = f.ShowDialog()
result = New clsFrmChildResult With {.Text = TextBox1.Text, .DialogResult = dr}
End Using
Return result
End Function
...
Private Sub btnSearchUser_Click(sender As Object, e As EventArgs) Handles btnSearchUser.Click
Dim result = clsFrmChild.Show()
Select Case result.DialogResult
Case DialogResult.OK
MessageBox.Show("Ok")
Dim myString = result.Text
Case DialogResult.Cancel
MessageBox.Show("Cancel")
End Select
End Sub

simple dialog like msgbox with custom buttons (vb)

I want to ask user for example "Do you want to go right or left?".
To have simple Code I use MSGBOX with a prompt like:
"Do you want to go right or left"
Press "YES for 'right' / NO for 'left'"
Then I process Yes/No/Cancel that was pressed. This works but is ugly and in some cases hard to understand.
Also in Addition in some cases I have more than 2 choices - but that is probable another question...
You can create one dynamically
Public Module CustomMessageBox
Private result As String
Public Function Show(options As IEnumerable(Of String), Optional message As String = "", Optional title As String = "") As String
result = "Cancel"
Dim myForm As New Form With {.Text = title}
Dim tlp As New TableLayoutPanel With {.ColumnCount = 1, .RowCount = 2}
Dim flp As New FlowLayoutPanel()
Dim l As New Label With {.Text = message}
myForm.Controls.Add(tlp)
tlp.Dock = DockStyle.Fill
tlp.Controls.Add(l)
l.Dock = DockStyle.Fill
tlp.Controls.Add(flp)
flp.Dock = DockStyle.Fill
For Each o In options
Dim b As New Button With {.Text = o}
flp.Controls.Add(b)
AddHandler b.Click,
Sub(sender As Object, e As EventArgs)
result = DirectCast(sender, Button).Text
myForm.Close()
End Sub
Next
myForm.FormBorderStyle = FormBorderStyle.FixedDialog
myForm.Height = 100
myForm.ShowDialog()
Return result
End Function
End Module
You see you have options as to what buttons are present, the message, and title.
Use it like this
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result = CustomMessageBox.Show(
{"Right", "Left"},
"Do you want to go right or left?",
"Confirm Direction")
MessageBox.Show(result)
End Sub
End Class
In my example, the prompt is "Do you want to go right or left?" and the options are "Right" and "Left".
The string is returned as opposed to DialogResult because now your options are unlimited (!). Experiment with the size to your suiting.
You need to create your own "custom" msgbox form according to your needs, and its better to create a reusable control - pass your "question" string via the constructor of your custom control.
You need some "way" to get the user decision from out side your custom msgbox - one way is use DialogResult Enum for that.
Here is a basic example i just wrote to demonstrate that, please see the comments i have added inside the code.
create a new project with 2 forms, Form1 will be the main form that will call the custom msgbox and Form2 will be the custom msgbox:
Form1:
Form2:
Code for Form1:
Public Class Form1
Private Sub btnOpenCustomMsgbox_Click(sender As Object, e As EventArgs) Handles btnOpenCustomMsgbox.Click
Dim customMsgbox = New Form2("this is my custom msg, if you press yes i will do something if you press no i will do nothing")
If customMsgbox.ShowDialog() = DialogResult.Yes Then
' do something
MsgBox("I am doing some operation...")
Else
' do nothing (its DialogResult.no)
MsgBox("I am doing nothing...")
End If
End Sub
End Class
Code for Form2:
Public Class Form2
' field that will contain the messege
Private PromtMsg As String
Sub New(ByVal promtmsg As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' set global field with the argument that was passed to the constructor
Me.PromtMsg = promtmsg
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' set the msg label
Me.lblPromtMsg.Text = Me.PromtMsg
End Sub
Private Sub btnCustomYes_Click(sender As Object, e As EventArgs) Handles btnCustomYes.Click
' user choosed yes - return DialogResult.Yes
Me.DialogResult = DialogResult.Yes
Me.Close()
End Sub
Private Sub btnCustomNo_Click(sender As Object, e As EventArgs) Handles btnCustomNo.Click
' user choosed no - DialogResult.no
Me.DialogResult = DialogResult.No
Me.Close()
End Sub
End Class
it can be much more sophisticated but if you explore that example i hope you will understand the general idea.

Property in Dialog is never set before the dialog closes?

I open a dialog with 2 buttons. Those buttons should set a property to a value and then close the form. My main form should then grab that property. Whenever I try to grab the value it's always blank.
My Main Form
Using SelectNextForm As New TubeSelectTo()
SelectNextForm.Focus()
If TubeSelectTo.ShowDialog(currentWO, status) = Windows.Forms.DialogResult.OK Then
MessageBox.Show(SelectNextForm.numberOfBins)
End If
The dialog
Private numberBins As String
Public Overloads Function ShowDialog(ByVal woID As String, ByVal currStatus As ResourceStatus) As DialogResult
Return Me.ShowDialog()
End Function
Public ReadOnly Property numberOfBins() As String
Get
Return numberBins
End Get
End Property
Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click
numberBins = "1"
DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub btn2_Click(sender As System.Object, e As System.EventArgs) Handles btn2.Click
numberBins = "2"
DialogResult = Windows.Forms.DialogResult.OK
End Sub
So why isn't my property being set?
Your issue is here:
If TubeSelectTo.ShowDialog(currentWO, status) = Windows.Forms.DialogResult.OK Then
You are using the default instance of the TubeSelectTo form, rather than the SelectNextForm instance that you created.
Change it to:
If SelectNextForm.ShowDialog(currentWO, status) = Windows.Forms.DialogResult.OK Then

vb.net returning string as dialog result

I'm creating an AddIn for Autodesk Inventor, the AddIn is a simple button in the ribbon.
When the user presses the button a new form is created as dialog.
Private Sub ButtonClick()
Dim oWindow As New CopyDesignForm(string1, string2)
oWindow.ShowDialog()
End Sub
The user will then do some operations and a file path as string is the result of his actions. I would now like to return this value so my AddIn can process the file.
But I can't seem to find a good example of this. I can only find an excellent sample of how to pass the ok or cancel result. But not how to get to a variable of the dialog.
Link to ok and cancel result sample
You can add a string property to the dialog and set the value of the property in your dialog,Then after showing the dialog, check if the dialog result was OK, then read the property.
Code for your customm dialog:
Public Class MyCustomDialog
Public Property SomeProperty As String
Private Sub OKCommandButton_Click(sender As Object, e As EventArgs) _
Handles OKCommandButton.Click
Me.SomeProperty = "Some Value"
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
Private Sub CancelCommandButton_Click(sender As Object, e As EventArgs) _
Handles CancelCommandButton.Click
Me.SomeProperty = Nothing
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
End Class
Code for your usage of custom dialog:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New MyCustomDialog
If (f.ShowDialog() = DialogResult.OK) Then
MessageBox.Show(f.SomeProperty)
End If
End Sub
End Class