VB.Net Handling Multiple Forms into Panel - vb.net

I have tried to find an answer to this already, but cannot find one that answers this question.
I have a Master Form which contains two panels. In the master Form I am trying to write a subroutine to handle the loading of a form into one of the panels.
One panel always contains the same form and the code which works for this is:
'Configure Toolbar Import
Dim toolbarHandler As _pnl_header = New _pnl_header()
toolbarHandler.Size = pnlHeader.Size
toolbarHandler.TopLevel = False
pnlHeader.Controls.Add(toolbarHandler)
toolbarHandler.Show()
The panel successfully shows the form _pnl_header as expected.
The second panel will change the displayed form depending on user input, so rather than having to write the above code for every eventuality i would like one Public Sub to handle them all...
I've started writing a sub along the lines of:
Public Sub LoadContentPanel(WhichForm As Form)
Try
Dim contentHandler As WhichForm = New WhichForm()
contentHandler.Size = pnlContent.Size
contentHandler.TopLevel = False
pnlContent.Controls.Add(contentHandler)
contentHandler.Show()
Catch ex As Exception
MsgBox("Unable to Handle Content Panel Change. Error: " & ex.Message, vbOKOnly + vbCritical, "Load Error")
End Try
End Sub
However this fails as 'WhichForm' is not defined - how is best to correct this? or is there a better alternative?
Thanks

Without going into what you are doing I can explain where the error comes from.
Here you declare argument variable WhichForm of type Form
Public Sub LoadContentPanel(WhichForm As Form)
. . . . .
Code is incorrect in the next declaration line. WhichForm is a variable and not a type. Hence
Dim contentHandler As WhichForm = New WhichForm()
is invalid at As WhichForm. Because after As you need a type name. If you did
Dim contentHandler As Form = New Form()
it would work.
It seems that all you need to do is remove Dim contentHandler As WhichForm... and rename argument WhichForm to contentHandler.

Related

Update value in another form's textbox via VBA

Hopefully this makes sense. I'm frustrated that I cannot figure this out. I have a simple Access 2010 database. I have a simple form inside it that helps the user input some specific information. This data entry situation can occur on two other forms in the database. Rather than have two copies of the "helper" form where the VBA code has hard-coded control references, I wanted to make it more universal by passing the name of the form that calls it by using the openArgs parameter.
When the time comes to transfer the values BACK to the form that needs the information, the helper form attempts to do this like so:
Private Sub cmdOk_Click()
Dim theFormName As String
Dim theForm As Form
theFormName = Me.OpenArgs
Set theForm = Forms.Item(theFormName)
If Not IsNull(theForm.Name) Then
theForm.txtLongitude.Value = Me.lblLongitude.Caption
theForm.txtLatitude.Value = Me.lblLatitude.Caption
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
The variable theForm is populated correctly and theForm.Name returns the correct name of the form so that part works fine. The problem is that theForm.<controlName>.Value does not. When I run the code, I get an
application-defined or object-defined error (Run-time Error 2465)
I've tried all sorts of permutations for the control reference from the current open form to a second open form but I cannot get the syntax right. I've tried:
theForm!txtLongitude.Value ("..can't find the field txtLongitude..")
theForm.Controls("txtLongitude").Value ("..cant find the field...")
I have two suggestions. If one works, let me know and I'll edit my answer to only include the one that works.
Try changing theForm.txtLongitude.Value = Me.lblLongitude.Caption
to Forms!theForm!txtLongitude.Value = Me!lblLongitutde.Caption and theForm.txtLatitude.Value = Me.lblLatitude.Caption to Forms!theForm!txtLatitude.Value = Me!lblLatitude.Caption
If you've already tried that or it doesn't work, try declaring variables and "pulling" the values out of one form before putting them in the other form. (Also make sure the data type of both are the same.)
Private Sub Command4_Click()
Dim theFormName As String
Dim theForm As Form
Dim txtLong As String
Dim txtLat As String
txtLong = Me.lblLongitude.Caption
txtLat = Me.lblLatitude.Caption
theFormName = Me.OpenArgs
Set theForm = Forms.Item(theFormName)
If Not IsNull(theForm.Name) Then
theForm.txtLongitude.Value = txtLong
theForm.txtLatitude.Value = txtLat
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub

Problems when calling a public sub

I'm facing a deadend When trying to call this sub :
Public Sub backblue(ByVal frm As Form, ByVal boxname As String)
For i = 1 To 3
CType(frm.Controls(boxname & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
End Sub
with button click event :
Private Sub Button1_click and bla bla....
backblue(Me, "txb1_")
End Sub
Can anybody show me a suggestion to fix the code.
It throws "Object Referrence not set to an instance bla bla" error
For information the textbox names are :
txb1_1 , txb1_2 , txb1_3
(these are some of the many textboxes in the form that i want its bakcolor changed)
and these three textboxes are already created through designer, not from execution.
i did check the textboxes names and there's nothing wrong.
the form class is also public.
if they are the only textboxs on said form you can just loop through
For Each box as Textbox In frm.Controls
box.BackColor = Color.LightBlue
Next
This error will occur if you do not declare the Form class to be public.
Also, make sure the textbox names are really correct, although this will probably cause a different error.
If you create the textboxes during execution, make sure they are initialized with New and added to the form's Controls collection.
Try this....
Public Sub backblue(ByVal frm As Form, ByVal prefix As String)
For i = 1 To 3
Dim bxName as String = prefix & i.ToString()
Dim bx as TextBox = CType(frm.Controls(bxName), TextBox)
If bx Is Nothing Then
MsgBox("Unable to find text box " +bxName)
Dim mtch() As Control = frm.Controls.Find(bxName, true)
If mtch.Length> 0 then
bx = mtch(0)
Else
Continue For
End if
End If
Bx.BackColor = Color.LightBlue
Next
End Sub
Although, a better solution would be to either create the textboxes inside a control and pass that control to BackBlue or to create an collection that has the controls and pass that in. Which brings up what is most likely yor problem your control is contained in a sub component and thus is not in the main form control collection
Alternative, you could use either the tag of the control or create a component control that implements IExtenderProvider and add it to the form --all of the above would effectively allow you to define the controls and/how they should be handled at designtime.
It may really seem that the names generated by this loop may not be the names of the original textboxes. My suggestion is before setting this Color property verify that the names generated by this loop are indeed the actual names. Maybe output this in a messagebox:
MessageBox.Show(boxname & i.ToString()) for each loop before you set the property

I can't show a form that isn't top level with form.ShowDialog()

My startup form is a modal security form which works fine. But, if the user "logs out", the security form must be displayed again as a modal dialog. This last step is where everything goes wrong. It shows the form, in front of my other forms, but it's not modal...
First, I call a method that's written in a module, because I have to be able to call this method from every form I want.
Public Sub CallWaiterKey()
Dim oForm As frmWaiterKey = New frmWaiterKey()
Try
If mWaiterKey.Length > 0 And mWaiterKeyType.Length > 0 Then
If Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 2 Then
oForm.TypeOfKey = 2
ElseIf Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 1 Then
oForm.TypeOfKey = 1
End If
'here it goes wrong
oForm.ShowDialog()
End If
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
When I call oForm.ShowDialog() (that's the frmWaiterKey), it comes up but isn't modal.
I can still click the buttons that are placed on frmMenu, the form from which I called CallWaiterKey().
Am I doing something wrong here?
Or should I make the call in an other way?
(My VB sucks so ignore syntax errors)
To achieve what you are asking, specify the hosting form.
Public Sub CallWaiterKey(ownerForm as Form)
Dim oForm As frmWaiterKey = New frmWaiterKey()
' ....
'here it goes wrong
oForm.ShowDialog(ownerForm)
' ....
End Sub
I don't use ShowDialog; but I believe that you need to specify the window owner to enforce the modality. If I'm wrong here, others will correct me.
oForm.ShowDialog(me)
** HOLD ON ** I will alter this in a second, I just recalled that you're calling from a module, me doesn't evaluate in a basic module.
Here is a MSDN reference

Close a form from another form

I face a problem with a multiple forms application
I have the mainForm and several mdiForms.
One of these child forms (frmDashboardManager) is open new ownedforms (frmDahboard) outside the mainForm
No I want to check if a frmDahboard is Open and if Yes to close it.
Here is what I have:
Dim fDash As New frmDashboard
fDash = isDashboardOpen(tempDash)
If fDash IsNot Nothing Then
fDash.Close() 'HERE I GET THE ERROR
End If
Private Function isDashboardOpen(ByVal dash As clsDashboard) As frmDashboard
isDashboardOpen = Nothing
Try
'search if Dashboard is already open
For Each fr As frmDashboard In Me.OwnedForms
If fr.My_Dashboard.Id = dash.Id Then
isDashboardOpen = fr
Exit For
End If
Next
Catch ex As Exception
gError.GetAppEx(ex, FORM_NAME & ".isDashboardOpen")
Finally
End Try
End Function
The error that I get is :
Object reference not set to an instance of an object.
The crazy thing is that I checked and isDashboardOpen returns actually a frmDashboard (also that's why fDash.Close() is executed).
Any ideas?
Thanks
I just found my error.
I disposed twice a user control that I have in frmDashboard.
I corrected that and everything works fine.
Thank you for your time.

Refresh combo box in Windows Form

HI,
This question has been asked a lot on the web but all of them seems to be confused with windows forms and asp.net forms so I had to finally come here for answer.
I have a Main form (a window form) in VB.NET which has a Combo Box to display a list of countries. A sub form activated from a "Button" on the Main form allows users to add Countries to their list. But After Adding a new entry the Combo Box on the Main form does not show the update (while it is still open). I have to exit the main form and relaunch it to see the update.
The populate function in shown below. I call this method after adding new countries but still; not good. I have used Invalidate function of combo box but of no use as well.
Any Help is appreciated. Thanks
Public Sub PopulateCountry()
Dim rsLocal As New ADODB.Recordset
Try
Dim sSQLCommand As String
Me.CountryList.Items.Clear()
'get the list of countries from the local database
sSQLCommand = " SELECT * FROM CountryList order by CountryDesc"
rsLocal.Open(sSQLCommand, cnn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
'Display All the countries
While rsLocal.EOF = False
Dim Country As New modGV.cbItem
Country .ID = rsLocal.Fields("CountryID").Value
Country.name = rsLocal.Fields("Country Desc").Value.ToString.Trim
Me.CountryList.Items.Add(Country)
rsLocal.MoveNext()
End While
rsLocal.Close()
Catch ex As Exception
Debug.WriteLine("PopulateCountry: exception occurred " & ex.Description)
Finally
If rsLocal.State = ConnectionState.Open Then
rsLocal.Close()
End If
End Try
End Sub
Use object.refresh
http://www.devx.com/vb2themax/Tip/18646