Hi I've migrated VB6 code to VB.Net where in VB6 one of the functionality uses modal dialogue which allows user to copy few text from parent form. But in Vb.Net ShowDialog doesn't allow user to copy anything as you guys know it just disables the parent form.
My question is, Is there a way I can enable parent form or else minimize child form to copy few text from parent form?
please don't suggest to use show instead of ShowDialog because I want to achieve this only using ShowDialog.
This VB6 Code.
Form.Show vbModal, objParent
migration wizard has below code
Form.ShowDialog
The answer may be one of design, instead of a technical workaround to .ShowDialog(). Let's take your parent form, for example, with text that may be copied for pasting within a popup modal form. I don't know the data in your parent form, so let's call it a Widget.
Public Class Widget
Public Property ID As Integer = 0
Public Property TextThatMayBeCopied As String = String.Emtpy
End Class
In your parent form's code, you would load this data into a Widget object from a database, a file, whatever.
Private _widget As Widget = Nothing
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Assume we want the Widget with ID of 123
_widget = MyFunction.WhichLoadsWidgetDataAndReturnsWidgetObject(123)
DisplayData()
End Sub
Private Sub DisplayData()
txtID.Text = _widget.ID
txtTextThatMayBeCopied.Text = _widget.TextThatMayBeCopied
End Sub
Private Sub btnShowDialog_Click(sender As Object, e As EventArgs) Handles btnShowDialog.Click
_widget.TextThatMayBeCopied = txtTextThatMayBeCopied.Text.Trim
Dim f As New MyShowDialogForm(_widget)
f.ShowDialog
End Sub
Your target form MyShowDialogForm would take in it's own constructor an object of type Widget:
Private _widget As Widget = Nothing
Public Sub New(widget As Widget)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_widget = widget
End Sub
You can now access the data passed to form MyShowDialogForm via the _widget object, for example, in a button click event for btnCopyText, or however you need.
The key takeaway here is to use a method of exchanging data within different forms. Typically it becomes very messy code to use the Form classes themselves as the encapsulation for data. Instead, use classes for encapsulating data and moving it around your app.
'For example we have 2 form, form1 (main) and form2 (child)
'This Form as Main
Public Class Form1
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'if you don't have timer in child form, you must click again this button
'after you form back awhile to this form as main form
Form2.ShowDialog()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
End Class
'This Form as Child
Public Class Form2
Private Sub btnToMainAWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToMainAWhile.Click
'you hide this current form to caller form, you must back here again,
'if not this form always active in background
'or if you have timer1 here with enabled property =false here, you can add this:
'Timer1.Interval = 10000
'Timer1.Enabled = True
Me.Hide()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
Me.Dispose()
End Sub
'if you have timer1 and you will wait for many seconds back to main form, add this:
'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Timer1.Enabled = False
' Me.ShowDialog()
'End Sub
End Class
Related
I had already designed a windows form (Form1) with a DataGridView on it, I also have a ribbon with a button on it in which I want when I click on it, it run a code like bellow:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles
Button2.Click
Dim f As Form1
f = New Form1
f.Show()
'(Room for my question)
end sub
I want to be able to access my datagridview user control to add columns and rows programmatically.
How should I do that?
You can create a Public Sub on form one which where you will put the changes that you want make on the the Datagridview then call it on the Button2_Click event of your Form2.
Example:
Form 1:
Public Sub AddToDGV()
With DataGridView1
.Rows.Add(Form2.TextBox1.Text, Form2.TextBox2.Text, Form2.TextBox3.Text)
End With
End Sub
Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Form1.AddToDGV()
End Sub
This question already has answers here:
VB.Net Passing values to another form
(2 answers)
Closed 5 years ago.
I have 2 forms with text boxes and buttons and I want the first form to open the second form. I then proceed to type a value in the text box in the second form so that when I click the button on the second form it must close and pass the value of the text box to the first form text box.
I have been trying to do this with no luck so far...
I am currently using vb.net in visual studio 2015
Here is my code:
Public Class Form1
WithEvents fr2 As New Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2.Show()
End Sub
Private Sub fr2_passvalue() Handles fr2.passvalue
Me.txtform1.Text = fr2.txtform2.Text
End Sub
End Class
Public Class Form2
Event passvalue()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue()
Me.Close()
End Sub
End Class
I have tried accessing the controls directly but it does not work as vb.net now uses classes for everything. I cannot seem to figure out how to get around this.
Redefine your event to pass a string. Then use the Form2 event to pass the value to the handler.
Public Class Form2
Public Event passvalue(text As String)
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue(txtform2.Text)
Me.Close()
End Sub
End Class
Handle it in Form1, updating Form1's TextBox.
Public Class Form1
WithEvents fr2 As Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2 = New Form2()
fr2.Show()
End Sub
Private Sub fr2_passvalue(text As String) Handles fr2.passvalue
Me.txtform1.Text = text
End Sub
End Class
As you had it before, Form1 was accessing controls on Form2 directly, which defeats the purpose of the event.
You could simply define a global variable (Public), which would be accessible from both forms.
I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class
If I have a value stored into a textbox of form1 and I have to pass that value into an another textbox of another form2. What is the method to do this passing values from one form to another?
There are a no. of ways.
1.Using TextChanged event.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Using Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
Using LostFocus Event:
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
Form2.TextBox1.Text = TextBox1.Text
End Sub
Similarly you can work with every events.
In order to retrieve a control's value (e.g. TextBox.Text) from another form, the best way is to create a module and create a property for the private variable.
An example of a property to hold a customer's first name:
Module modPrivateVariables
Private strCustomerFirstNameSTR As String
Public Property getCustomerFirstNameSTR() As String
Get
Return strCustomerFirstNameSTR
End Get
Set(ByVal strCustomerFirstName As String)
strCustomerFirstNameSTR = strCustomerFirstName
End Set
End Property
End Module
Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text. For example, if you had a textbox named (txtCustomerFirstName), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text.
The textbox's text will now be assigned to getCustomerFirstNameSTR property. Now you'll be able to access this property's value from anywhere and from any form in your application. For example if you had a textbox in another form say Form2 called "txtBoxInForm2" you can call txtBoxInForm2.Text = getCustomerFirstNameSTR.
If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty. The main thing to understand is that when you create a variable in one form (class) and try to access its value from another form (another class) then the variable has to be re-instantiated once.
This happens then the variable is reset to its default value which is an empty string. This will cause you to keep getting nothing (an empty textbox) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods within the property itself (the get and set methods).
if both the forms are running, then you can use
form2.TextBox1.Text=form1.TextBox1.Text
Else you can declare a Public String variable in Form2, on any event,
dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show
and on Form2 Load,
Me.TextBox1.Text=StrVariable
In Form1.vb make sure you use an event such as Button.Click and inside that
Dim obb As New Form2
obb.val = Me.TextBox1.Text()
obb.Show()
Me.Hide()
In Form2.vb use a property called "val"
Public Property val As String
And on an event like MyBase.Load
TextBox1.Text = val
You could use the button1_click and state there:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 before your form2 main class you state:
Public property pass as string
On the load state
Textbox1.text=pass
Now, when you click on the button on form1, form2 will show and the textbox1 on form2 will have the same text as the one in form1. Provided you use this only with text box, labels or other kind of STRING or will work.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' timer1 must be set to true
Timer1.Start() Form1.Show() Me.Hide()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form1.TextBox13.Text = TextBox1.Text
I have some forms in my application and need to switch between them. I dont want to open another form over other form, in short I want the user to get a feel that he is working on one form only. Right now I am doing something like this but this is not what I want
Public Class Form1
Dim fo As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.TopLevel = False
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
End Class
I also need to send data to other form like list of some class.
Look into UserControls(Composite Controls). They will allow you create a custom layout with events and properties and add it to your panel. I have used this to swap in/out edit pages for my applications.
Here is a very simplistic example:( 1 Form, 2 Panels, 2 UserControls)
Form1
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub UserControl11_SendText(value As String) Handles UserControl11.SendText
UserControl12.SetText(value)
End Sub
Private Sub UserControl12_SendText(value As String) Handles UserControl12.SendText
UserControl11.SetText(value)
End Sub
End Class
UserControl
Public Class UserControl1
Public Event SendText(ByVal Text As String)
Public Sub SetText(value As String)
Label1.Text = value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent SendText(TextBox1.Text)
End Sub
End Class
Have you considered using a TabControl? This will let you create different screens, and you (or the user) can switch between them easily.