Property in Dialog is never set before the dialog closes? - vb.net

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

Related

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

how to manage pass value set by dialog box in VB form application to its caller

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

VB: Populate a textbox with popup from txt

I am creating a vb file, and I have a txt file with some that I want to populate a textbox.
What it is currently doing: It is choosing an option that i input in the textbox that I have created.
What I want it to do: Create a popup with every option from a textbox file, show it on screen, let me choose, and then populate another textbox with my choice.
Current code and screenshot:
Public Class Form1
Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
Dim f As New Form2
Try
f.Owner = Me
'
' Before showing the child form populate TextBoxes
'
f.TextBox1.Text = "1"
f.TextBox2.Text = "2"
f.TextBox3.Text = "3"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Box = (From T In f.Controls.OfType(Of TextBox)()
Where Not String.IsNullOrWhiteSpace(T.Text)
Select T Order By T.Name).FirstOrDefault
If Box IsNot Nothing Then
Me.TextBox1.Text = Box.Text
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Form 2
Public Class Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Box = (From T In Controls.OfType(Of TextBox)()
Where Not String.IsNullOrWhiteSpace(T.Text)
Select T Order By T.Name).FirstOrDefault
If Box IsNot Nothing Then
CType(Me.Owner, Form1).TextBox1.Text = Box.Text
End If
CType(Me.Owner, Form1).ActiveControl = CType(Me.Owner, Form1).cmdClose
Close()
End Sub
End Class
Image:
edit: now i only need to make the checkbox into locked textboxes, one line from the text file each.

VB.NET how to populate a ComboBox in an UserControl from a Form

I have an UserControl where data from MySQL table loads in a ComboBox while it is loading.
Private Sub LoadFeeGroup()
Try
OpenConnection()
qry = "SELECT GroupId, GroupName FROM master_fee_group WHERE Stat='1' ORDER BY GroupName ASC"
Dim da As New MySqlDataAdapter(qry, conn)
Dim ds As New DataSet
da.Fill(ds, "master_fee_group")
With CmbGroup
.DataSource = ds.Tables("master_fee_group")
.DisplayMember = "GroupName"
.ValueMember = "GroupId"
End With
If CmbGroup.Items.Count > 0 Then
CmbGroup.SelectedIndex = 0
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
CloseConnection()
End Try
End Sub
And this Subroutine is being called when the UserControl is being loaded.
Private Sub AdmissionFeeUc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadFeeGroup()
End Sub
Now user can add any Fee Group Name (if not added previously) by opening a form.
Now I want to call this LoadFeeGroup() subroutine from that form so that user can see the added Fee Group Name in the ComboBox of the UserControl after closing the form.
Something like...
Private Sub FormFeeGroup_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
' Code for calling the UserControl subroutine..
End Sub
I have tried to call the Subroutine like below,
but failed.
How can I do that ?
UPDATE
I have added a button in the UserControl.
Private Sub BtnNewGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewGroup.Click
Dim frmFeeGroup As New FormFeeGroup
Dim dlgres As DialogResult = frmFeeGroup.ShowDialog()
If DlgRes <> DialogResult.OK Then
Return
Else
LoadFeeGroup()
End If
End Sub
And in the FormClosing event
Private Sub FormFeeGroup_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
Return
Else
'nothing to do
End If
End Sub
And in the Close button in the form,
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
My purpose is Partially served. Partially because, If I open the Form from the menu, the ComboBox is not updated.
You could create your own constructor for the form where you must pass a user control instance to it. That way you cannot create an instance of the form without specifying which user control it should be "tied" to.
For instance, put this in your form's code:
Public Property TargetFeeUc As AdmissionFeeUc
Public Sub New(ByVal FeeUc As AdmissionFeeUc)
InitializeComponent() 'Must be called before anything else.
Me.TargetFeeUc = FeeUc
End Sub
Then to create a new form instance you'll always be forced to give it an AdmissionFeeUc control.
'If written inside the user control's code:
Dim frmFeeGroup As New FormFeeGroup(Me)
'If written somewhere else:
Dim frmFeeGroup As New FormFeeGroup(<user control instance here>)
'...for example:
Dim frmFeeGroup As New FormFeeGroup(AdmissionFeeUc1)
And whenever you want to update the user control from your FormFeeGroup form you just need to call:
TargetFeeUc.LoadFeeGroup()
EDIT:
To get the user control instance if it was created dynamically you have to set the control's Name property when creating it, then you can reference it via:
<target control or form>.Controls("<user control name here>")
'For example:
Me.Controls("MyFeeUc")
'Or for sub-controls:
Me.Panel1.Controls("MyFeeUc")

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