calling one form from another when button is click - vb.net

I am having two forms in vb where main form contains button and on button click event form2 will load.
I wrote this code:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
Dim d As BlankPage1 = New BlankPage1
d.show()
End Sub
where Blankpage1 is the name of second form. The problem is after clicking button, Blankpage1 doesn't appear.

Try BlankPage1.show() only, don't initialize first.

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
form2.show() //name of the form
End Sub

Try like this :
Dim d As New BlankPage1
d.MdiParent = Me 'or the main form's name(MDI Parent Form)
d.StartPosition = FormStartPosition.CenterScreen
d.Show()
d.Focus()

Related

How do I add items to a listbox inside a user control?

I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.

Bringing a Form to the front of a Tab

When I use Me.BringToFront() to Form4 it doesn't bring the form to the front as you can see in the picture:
And when I use Form1.Tab.SendToBack() to the Tab, the Tab disappear.
How can I bring Form4 to the front and prevent the Tab from disappearing?
Im able to solve it. I turn the propeties of Form1 IsMdiContainer to False then use this code
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim F As Form4
F = New Form4
F.Show()
F.Owner = Me
End Sub

Bring MDIChild form to front if already open

I've been struggling to get this to work...I have a button on a MDIchild form that opens another MDIchild form, but if the form is already open, it does not recognize it and opens a new one instead of bringing it to front. This is the code i've got:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MDIForm4 As New Form4
MDIForm4.MdiParent = Me
MDIForm4.Show()
End Sub
This works for the button to open the new form, and then I tried adding this:
If Not Form4 Is Nothing Then
Form4.BringToFront()
End If
But with no positive outcome. Does someone have any ideas?
Regards,
Jorge Brito
Here is how I typically do that:
For Each f As Form In Application.OpenForms
If TypeOf f Is frmTest Then
f.Activate()
Exit Sub
End If
Next
Dim myChild As New frmTest
myChild.MdiParent = Me
myChild.Show()
Notice this uses Application.OpenForms, you can use your Me.MdiChildren (assuming Me = this MDI form) if you want just the children of your main form.
Fixed now!
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each f As Form In Application.OpenForms
If TypeOf f Is Form4 Then
f.Activate()
Exit Sub
End If
Next
Dim MDIForm As New Form4
MDIForm.MdiParent = Form2
MDIForm.Show()
End Sub
I was defining the MDI parent on the wrong form!

How I can switch between Forms with VB?

hi i have a vb application with two forms. With Form A I can start Form B and Form A is then visible=false. If I click on the red "X" on the right corner I want that form B close and Form A is visible true.
How I can do this?
You can setup something like this in FormB:
Private objFromForm As FormA
Sub New(FromForm As FormA)
InitializeComponent()
objFromForm = FromForm
End Sub
Private Sub FormB_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
objFromForm.Visible = True
End Sub
Basically, when FormA calls the constructor for FormB, it passes a reference to itself. Then, in FormB's "FormClosing" event, you can use that reference to make FormA visible again before FormB closes. Here's an example of this from FormA's side, with a button that makes a new instance of FormB visible and passes the reference to itself (FormA) to the FormB constructor:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objNewForm As New FormB(Me)
objNewForm.Show()
Me.Visible = False
End Sub
Basically you can do like this ..
When calling FormB form FormA
Me.Visible = False
FormB.Showdialog
In FormB FormClosing event
FormA.Visible = True

data from datagridview not displayed into textbox (Visual Studio 2010)

I've been dealing with this for hours!!! I have 2 forms: in one form (form1) I have my layout with textboxes, etc... in the other (datagrid_form2) I have a datagridview where I choose the item, with a doubleclickcell event, to be loaded in a specific textbox of the first form (form1).
I have a button next to the textbox of the form1 that call the datagrid_form2, once the element in the datagrid_form2 is chosen the textbox of the form1 is loaded with that value.
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text )
Me.Close()
End Sub
As you can see I have the cellDoubleclick event that should load the value of the selected cell into the textbox of my form1, but it doesn't display anything in the textbox(txtmybox). in the debug the value is chosen correctly, so is not a problem of code, simply the value is not being passed at the textbox.
Any ideas? hints?
thanks in advance
p.s. I'm working with visual studio 2010 .vb project!
It seems that you are messing with the forms.
You are creating a new instance of Form1 but you don't show it.
I suggest to read this.
Also your question is similar to this
Edit:
It is not clear from your questions how you want to achieve what you ask.
You have a form with a dataGridView (I name it frmDgv) and a second form (form1) that you want to show the cell content from yours datagrid.
Is this form (form1) already opened? or you want to open a new one each time that you double click? And if you want to open it each time you want multiple instances or one in modal ?
So I'll try to include everything:
->The form will open each time
frmDgv
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
'If you want to open a Form1 each time you double click in an cell
f1.Show
'If you want a modal style info
'f1.ShowDialog
'f1.Dispose
End Sub
->The form is already open (I wouldn't follow this)
frmDgv
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
'When you open this form for first time you will set:
Dim f1 as new Form1
mLinkForm1=f1
f1.Show
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
End Sub
Edit 3 (I don't have Visual Studio right now , so my code is not tested)
Form: datagridview
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
LinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Me.Close()
End Sub
Form: Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
datagridview.LinkForm1=Me
datagridview.Show
End Sub
Try this and inform me.
Change this part
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
to
form1panel.txtmybox.Text = data.CurrentCell.Value
This is for string value ...
You need to show your child form:
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text)
form1panel.Show();
//Me.Close()
End Sub