VB Express 2013 - My own form class is empty - vb.net

I´m creating desktop app. I tried to create my own form class like this:
Public Class TMyForm
Inherits Form
Protected ThisForm As Form
Protected IsOpened As Boolean
Protected Mode As FormMode
Enum FormMode
Gmail
Twitter
End Enum
Public Sub New(ByVal Form As Form)
Me.ThisForm = Form
End Sub
Public Sub SetUI(ByVal LoginTo As String)
If LoginTo Is Nothing Then
Me.IsOpened = False
Me.Close()
Else
Me.IsOpened = True
Select Case LoginTo
Case "Gmail"
Mode = FormMode.Gmail
Me.Text = "Gmail login"
Case "Twitter"
Mode = FormMode.Twitter
End Select
End If
End Sub
End Class
Now I would like to call this from my main form:
Public Class SideBlock
Protected LoginForm As New LoginForm
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetLayout()
End Sub
Private Sub GmailToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GmailToolStripMenuItem.Click
Dim LoginForm As New TMyForm(Me.LoginForm)
LoginForm.SetUI("Gmail")
LoginForm.Show()
End Sub
End Class
As you can see I call my TMyForm class with existing form called LoginForm. After clicking on the Gmail button the login form appears but its epmpty. There should be textboxs etc. But all I see is empty form.
Did I missed something? Thank you

Related

How can i pass parameters from datagridview to an open or main form without opening new form

I was thinking how can i pass parameters using constructors from datagridview which is in another form to my main form without opening new one. here's my code
-----------Main form constructor----------------------------------------
Public Sub New(customerID As Integer, fullName As String, phoneNumber As String, emailID As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_customerID = customerID
_fullName = fullName
_phoneNumber = phoneNumber
_emailID = emailID
End Sub
-----------------------------second form edit button click ----------------------------
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
Dim formMain As New frmMain(CInt(Me.dgvCustomerInfo.Item(0, Me.dgvCustomerInfo.SelectedRows(0).Index).Value),
Me.dgvCustomerInfo.Item(1, Me.dgvCustomerInfo.SelectedRows(0).Index).Value,
Me.dgvCustomerInfo.Item(2, Me.dgvCustomerInfo.SelectedRows(0).Index).Value,
Me.dgvCustomerInfo.Item(3, Me.dgvCustomerInfo.SelectedRows(0).Index).Value)
Me.Hide()
'formMain.Hide()
formMain.Show()
Thanks
You could create a property in your second form which is a list of the type of value that you want to pass and then when you create the form, assign your values to the property. Finally process the property using code in the form2.Shown event like this
Public Class Form1
Private Sub test()
Dim f2 As New form2
f2.formparameters = {4, 5, 6, 7}
f2.Show()
End Sub
End Class
And in your 2nd form write something like this..
Public Class form2
Friend Property formparameters() As Integer()
Private Sub form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'process parameter data here
Me.Update()
End Sub
End Class

change GroupControl visibility of opened usercontrol from the form vb.net

I have one 'UserControl' that opened inside form,
this usercontrol has a 'GroupControl'
how can I make this 'GroupControl' hidden or visible when user click on a button that beings on the form
using vb.net
In the user control:
Public Property GroupControlVisible() As Boolean
Get
Return Me.GroupControl1.Visible
End Get
Set(value As Boolean)
Me.GroupControl1.Visible = value
End Set
End Property
Try this :
In you UserControl add a procedure how set visibility to your GroupControl :
Public sub SetVisibility (V as boolean)
YourGroupControl.visible=v
End Sub
In your form
Public Class Form1
Dim uc As New MyUserControl
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.Add(uc)
uc.Dock()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
uc.SetVisibility(False)
'NB :MyUserControl is name of your usercontrol
End Sub
End Class
To Simplify, you can use :
MyParentForm.UserControlName1.GroupControlName.Visible = False
or
CType(MyParentForm.Controls("UserControlName").Controls("GroupControlName"), _
GroupControl).Visible = False
But the best way is create a property which allows changing the Visible property of the GroupControl in UserControl Class like this :
Public Property GroupControlVisibility() As Boolean
Get
Return Me.GroupControlName.Visible
End Get
Set(value As Boolean)
Me.GroupControlName.Visible = value
End Set
End Property

Can't Remove Listview Item From Second Form

I am having an issue when trying to delete ListView Items from a second form.
For example, if I use the following command on Form1 it works:
Listview1.SelectedItems(0).Remove
However, if I attempt to remove from Form2 like so:
Form1.Listview1.SelectedItems(0).Remove
I get the following error:
"Invalid argument=value of '0' is not valid for 'index'. Parameter name: index"
I then tried to get a count of items from the listview on Form2 and it gives me a return of 0
Form1.Listview1.Items.Count
I'm not sure what my problem is.
Update
I have posted a brief example of my code (using your suggestion as I can understand it):
frmShowMessages
Private Sub ViewMessage()
Dim frm As New frmViewMailMessage
frm.Show()
End Sub
Public Sub DeleteItem(ByVal index As Integer)
lsvReceivedMessages.Items(index).Remove()
End Sub
frmViewMessage
Private instanceForm as frmShowMessages
Private Sub frmViewMailMessage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
instanceForm = New frmShowMessages()
End Sub
Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
instanceForm.DeleteItem(_index)
End Sub
Hopefully my code can help identify where my issue is.
In VB.net usually you get a default Form instance for each of your Form. Probably you are creating an instance of Form1 and then you are trying to access ListView1 of default instance.
E.g.
Sub ButtonClick()
Dim f As New Form1()
f.Show()
' at this point if you access f's ListView you will get correct count
f.ListView1.Items.Count
' however if you try to access default instance it will NOT have any item
Form1.ListView.Items.Count
End Sub
It means your instance f is NOT equal to default Form1 instance.
Solution can be, make the f variable as class level variable and use it everywhere. Or if Form1 will have only 1 instance, then you can use the default instance everywhere.
Personally I would NOT go with direct control accessing over forms. I would create a Public method which should return the data as list to the caller, in this case your Form2.
UPDATED-2:
As per your given scenario, I am simplifying things for you, and doing implementation using Event.
Public Class frmShowMessages
Private Sub btnOpenMessage_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenMessage.Click
Dim frmView As New frmViewMessage(Me.ListView1.SelectedItems(0).Index)
AddHandler frmView.MessageDeleted, AddressOf DeleteMessageHandler
frmView.Show()
End Sub
Private Sub DeleteMessageHandler(sender As Object, e As frmViewMessage.MessageDeletedEventArgs)
Me.ListView1.Items.RemoveAt(e.MessageIndex)
End Sub
End Class
Public Class frmViewMessage
' a class which will be used for Event communication
Public Class MessageDeletedEventArgs
Inherits EventArgs
Public Property MessageIndex As Integer
Public Sub New(ByVal iIndex As Integer)
MyBase.New()
Me.MessageIndex = iIndex
End Sub
End Class
' main event which will alert the parent that a message deletion should be done
Public Event MessageDeleted As EventHandler(Of MessageDeletedEventArgs)
' private variable that will hold the MessageIndex
Private Property MessageIndex As Integer
' method that is responsible to raise event
Protected Overridable Sub OnMessageDeleted()
RaiseEvent MessageDeleted(Me, New MessageDeletedEventArgs(Me.MessageIndex))
End Sub
' we want to create this Form using the MessageIndex of ListView
Public Sub New(ByVal iMessageIndex As Integer)
Me.InitializeComponent()
Me.MessageIndex = iMessageIndex
End Sub
' the delete button will raise the event to indicate parent that
' a deletion of message should be done
Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
Me.OnMessageDeleted()
End Sub
End Class

Call a tabPage from a separate window form

I have literally just started programming VB today; so forgive my incompetence.
I currently have two window forms, one to navigate though different pages via TabControl (Which is called BusinessSalesPage.vb) and the other is separate (BusinessQuestion.vb). The second window form opens when a button is pressed on BusinessSalesPage.vb.
When the second window opens it has two buttons, I would like the user to be able to click one button which then takes them to the third tab from the TabControl that is on the first window (BusinessSalesPage.vb). Thanks in advance.
Here is my code:
Public Class BusinessQuestion
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl
Friend WithEvents TabPage3 As System.Windows.Forms.TabPage
Private Sub yesButn_Click(sender As Object, e As EventArgs) Handles yesButn.Click
mainMenu.SelectedTab = TabPage3()
End Sub
End Class
Here is the error I am getting:
NullReferenceException was handled - Object reference not set to an instance of an object.
Edit
Public Class BusinessSalesPage
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage2
BusinessQuestion.Show()
End Sub
End Class
#LarsTech I tried this as well but mine is having issues with the code. On First Form when I type the mainMenu.SelectedTab = TabPage 1 I get error: "SelectedTab is not a member of System.Windows.Forms.MainMenu and TabPage1 is not declared. It may be inaccessible due to its protection level. On Second Form it doen not find InitializeControls, only InitializeComponents. I am in VS 2010 and here is my code:
First Form:
Public Class WCC
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage1
Dim MSO As New MSO2014CC(mainMenu:=, TabPage1:=)
MSO.ShowDialog()
End Sub
End Class
Second Form:
Public Class MSO2014CC
Inherits System.Windows.Forms.Form
Friend WithEvents mainMenu As System.Windows.Forms.TabControl
Public Sub New(mainMenu As TabControl, tabPage1 As TabPage)
InitializeComponent()
Me.mainMenu = mainMenu
Me.TabPage1 = tabPage1
End Sub
End Class
You need a reference to the actual instance of the mainForm. You currently don't.
You can try passing a reference in the constructor:
Public Class BusinessQuestion
' code...
Public Sub New(mainMenu As TabControl, tabPage3 As TabPage)
InitializeControls()
Me.mainMenu = mainMenu
Me.TabPage3 = tabPage3
End Sub
' code...
End Class
Your BusinessSalesPage should probably look something like this:
Public Class BusinessSalesPage
Inherits System.Windows.Forms.Form
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mainMenu.SelectedTab = TabPage2
Dim bq As New BusinessQuestion(mainMenu, TabPage3)
bq.ShowDialog()
End Sub
End Class

Form data erased after Windows form is closed

I have a subsidiary form where I can enter data and then save it before closing the form and going back to using the main form.
When I re-open the subsidiary form, I cannot see the changes in the data that I had entered earlier.
Can anyone tell me where I'm wrong ?
MainForm.vb
Public Class Maincls
oTestObj as New Testcls
oTestObj.XYZ = "XYZ"
Private Sub SoftwareSettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SoftwareSettingsToolStripMenuItem.Click
Testcls.tbXYZ.Text = oTestObj.m_XYZ
Testcls.Show()
End Sub
End Class
Form_Testcls.vb
Public Class Testcls
Structure Params
Dim m_XYZ as String
End Structure
Dim oParams as Params
Public Sub New ()
InitializeComponent()
End Sub
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
XYZ = tbXYZ.Text
Me.Hide()
End Sub
Public Property XYZ() As String
Get
Return Me.oparams.m_XYZ
End Get
Set(ByVal value As String)
Me.oparams.m_XYZ = value
End Set
End Property
End Class
I think in windows forms the work around for this is to create a static class and add properties according to your requirement. Then populate these static properties on closing of your form. Now you can use the value set in the static data members, unless otherwise you change them on any other event.
Edit: In vb.net the Static is actually NonInheritable