• 0

working with forms in VB


Question

Hi guys, here is my problem, in vb I got a couple of forms and when I run the main form which has my buttons on it and when I want to click on any of the buttons the relevant form opens exactly how I want it, but on the forms that are open I have put a home button so when it is pressed it closes that page and goes back to main form..........well thats how I want it to work but it doesn't. This is what really happens....when a button is clicked it opens the relevant page which is ExhibitorForm.ShowDialog()and that sits on top of the main just how i want it and when I want to press the home button it closes that form and opens a new main form so I have 2 main forms.

Anybody got any ideas how I can resolve this?

Link to comment
https://www.neowin.net/forum/topic/1135466-working-with-forms-in-vb/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

I do this in VBA should be the same

mainform

2ndform

so when you click 2ndform button you want

2ndform.show

mainform.hide

and when you click home you want

2ndform.hide

mainform.show

this will popup the same form

  • 0

I've done that and it puts the blue squiggle line under it and so I put ME.hide() and it closed it and done the same thing open another main, not sure if i am doing something right/wrong in the main to do this.

here is my code for my main form....

Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ExhibitorForm As New ExhibitorForm

ExhibitorForm.ShowDialog()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim visitosForm As New VisitorsForm

VisitorsForm.ShowDialog()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim PermanentStaff As New PermanentStaff

PermanentStaff.ShowDialog()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim AgencyStaff As New AgencySatff

AgencyStaff.ShowDialog()

End Sub

End Class

  • 0

Just because you open a second form doesn't mean the main one has closed, so you don't actually have to do anything when you want to return to the first form - just close the second one.

Example:

You have "Form 1" open. You click "Button 1" on "Form 1" and this opens "Form 2." You click "Home" on "Form 2," which closes "Form 2." "Form 1" will still be open, so you need not do anything else.

  • 0

Can you please press "code" when pasting code. I don't read code without the pretty colors!

Fixed that for you :)


Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim visitosForm As New VisitorsForm
VisitorsForm.ShowDialog()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim PermanentStaff As New PermanentStaff
PermanentStaff.ShowDialog()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim AgencyStaff As New AgencySatff
AgencyStaff.ShowDialog()
End Sub

End Class [/CODE]

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.

  • 0

Fixed that for you :)


Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim visitosForm As New VisitorsForm
VisitorsForm.ShowDialog()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim PermanentStaff As New PermanentStaff
PermanentStaff.ShowDialog()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim AgencyStaff As New AgencySatff
AgencyStaff.ShowDialog()
End Sub

End Class [/CODE]

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.

OHHH LOOK AT ALL THE COLORS!

  • 0

Fixed that for you :)


Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim visitosForm As New VisitorsForm
VisitorsForm.ShowDialog()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim PermanentStaff As New PermanentStaff
PermanentStaff.ShowDialog()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim AgencyStaff As New AgencySatff
AgencyStaff.ShowDialog()
End Sub

End Class [/CODE]

I suppose the reason why you're seeing a new main form when you click the "Home" button on your child forms (which code you haven't posted), is because the handler for that button is something like:

[CODE]
Dim MainForm As New MainForm
MainForm.Show()
[/CODE]

Obviously, this creates and shows a brand new main form, hence the observed behavior. What you could simply do in that handler is not create any form and close the current form, i.e. call Close(). This will let the parent form show once again.

Sorry about the CODE thing, will try to do it next time.

Forgot aboutposting the code about the other form so here it is...

[CODE]

Public Class ExhibitorForm


Private Sub btnhome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhome.Click
Dim MainMenu As New Mainmenu
Me.Hide()
MainMenu.Show()


End Sub
End Class
[/CODE]

I have just changed it to close() and works.

Thanks for the help :D

  • 0

Hi guys, got another problem, I've put all my code in but it doesn't seem to work

This is the code for my button in the main menu.... once clicked it goes to my ExhibitorForm....Well it did before i put the code in on the ExhibitorForm now it throws up this message The 'Microsoft.ACE.OLEDB.12.0Data Source = Project.accdb' provider is not registered on the local machine. pointing at the con.open()



Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()

End Sub
[/CODE]

here is the code for my ExhibitorForm.....

[CODE]

Public Class ExhibitorForm
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim cb As New OleDb.OleDbCommandBuilder(da)



Private Sub ExhibitorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
dbSource = "Data Source = Project.accdb"

con.ConnectionString = dbProvider & dbSource
con.Open()

sql = "SELECT * FROM tblExhibitorCompanies"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Project")

MaxRows = ds.Tables("Project").Rows.Count
inc = -1
End Sub

Private Sub navigateRecords()
TxtCompID.Text = ds.Tables("Project").Rows(inc).Item(0)
TxtCompName.Text = ds.Tables("Project").Rows(inc).Item(1)
TxtCompAddress.Text = ds.Tables("Project").Rows(inc).Item(2)
TxtCompCity.Text = ds.Tables("Project").Rows(inc).Item(3)
TxtCompPhoneNum.Text = ds.Tables("Project").Rows(inc).Item(4)
TxtNumbStands.Text = ds.Tables("Project").Rows(inc).Item(5)
TxtPriceStands.Text = ds.Tables("Project").Rows(inc).Item(6)

End Sub


Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click
If inc > 0 Then
inc = inc - 1
navigateRecords()
ElseIf inc = -1 Then
MsgBox("No Records Yet")

ElseIf inc = 0 Then

MsgBox("First Record")
End If
End Sub

Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
Else
MsgBox("No More Rows")
End If
End Sub

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("Project").Rows(inc).Item(0) = TxtCompID.Text
ds.Tables("Project").Rows(inc).Item(1) = TxtCompName.Text
ds.Tables("Project").Rows(inc).Item(2) = TxtCompAddress.Text
ds.Tables("Project").Rows(inc).Item(3) = TxtCompCity.Text
ds.Tables("Project").Rows(inc).Item(4) = TxtCompPhoneNum.Text
ds.Tables("Project").Rows(inc).Item(5) = TxtNumbStands.Text
ds.Tables("Project").Rows(inc).Item(6) = TxtPriceStands.Text



da.Update(ds, "AddressBook")

MsgBox("Data Updated")

If inc <> -1 Then

End If

Dim dsNewRow As DataRow

dsNewRow = ds.Tables("Project").NewRow()

dsNewRow.Item("CompanyID") = TxtCompID.Text
dsNewRow.Item("CompanyName") = TxtCompName.Text
dsNewRow.Item("CompanyAddress") = TxtCompAddress.Text
dsNewRow.Item("City") = TxtCompCity.Text
dsNewRow.Item("TelephoneNumber") = TxtCompPhoneNum.Text
dsNewRow.Item("NumberofStands") = TxtNumbStands.Text
dsNewRow.Item("PriceofStands") = TxtPriceStands.Text



ds.Tables("AddressBook").Rows.Add(dsNewRow)

da.Update(ds, "AddressBook")

MsgBox("New Record added to the Database")

btnsave.Enabled = False
btnadd.Enabled = True
btnedit.Enabled = True
btndelete.Enabled = True

End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
btnsave.Enabled = True
btnadd.Enabled = False
btnedit.Enabled = False
btndelete.Enabled = False

TxtCompID.Clear()
TxtCompName.Clear()
TxtCompAddress.Clear()
TxtCompCity.Clear()
TxtCompPhoneNum.Clear()
TxtNumbStands.Clear()
TxtPriceStands.Clear()


End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)

If MessageBox.Show("Please confirm you want to Delete this Record?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Canceled")
Exit Sub

End If
ds.Tables("Project").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
navigateRecords()
da.Update(ds, "Project")
End Sub
End Class

[/CODE]

Any Ideas guys?

  • 0

Hi guys, got another problem, I've put all my code in but it doesn't seem to work

This is the code for my button in the main menu.... once clicked it goes to my ExhibitorForm....Well it did before i put the code in on the ExhibitorForm now it throws up this message The 'Microsoft.ACE.OLEDB.12.0Data Source = Project.accdb' provider is not registered on the local machine. pointing at the con.open()



Public Class Mainmenu

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ExhibitorForm As New ExhibitorForm
ExhibitorForm.ShowDialog()

End Sub
[/CODE]

here is the code for my ExhibitorForm.....

[CODE]
Private Sub ExhibitorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
dbSource = "Data Source = Project.accdb"

con.ConnectionString = dbProvider & dbSource
con.Open()
[/CODE]

Any Ideas guys?

Yes... Quite simply when you're combining the strings the '12.0' and the 'Data' are being combined into '12.0Data' because you're neglecting a required separator ';'.

  • 0

I dont know what you mean......do you mean i should write it as ..........ACE.OLEDB.12.0DATA?

No I mean you're missing the ';' separator between the provider and data source declarations when you combine them...


' as you can see below you have two variables one for provider, one for source '
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0"
dbSource = "Data Source = Project.accdb"

'here you are combining them into a connection string, with the ; separator, you end up with an invalid connection string'
con.ConnectionString = dbProvider & dbSource
'Simply add ; to the end of the provider string as such'
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
[/CODE]

This topic is now closed to further replies.
  • Posts

    • Wow, imagine you dump hundreds of hours into completing things and unlocking stuff and you lose it all. Back in the day when cheats were built into games, you could at least unlock things again that way without spending hundreds of hours again. But those days are long gone for some reason as no one builds cheats into games anymore. So it's even more painful that studio that's on its 6th installment **** it up so badly.
    • Spotify finally removes the disco ball app icon in the latest update by Ivan Jenic Image: Spotify Spotify has just released an update that removes its now infamous disco ball icon. The update reverts the app icon to the familiar flat green logo after weeks of mixed reactions online. The icon arrived on May 13 as part of the company's 20th anniversary celebration and was always intended to be temporary, though Spotify only confirmed that after the backlash started. The disco ball took the internet by storm, as the reception was split. A vocal group of users called it ugly and disorienting, with some iOS users noting that the 3D glowing effect made the app look like it was stuck mid-update. On the other end, the icon picked up a following of its own. Its retro, three-dimensional look immediately stood out against the flat, minimalist aesthetic that has dominated app design for years. It even started a small movement, spawning what people started calling "discomorphism," a mashup of disco and skeuomorphism. Other brands started posting disco ball versions of their own logos, probably in an effort to ride the wave of memes that flooded the internet during late May. Spotify has had a turbulent relationship with its user base lately. Besides the disco ball icon, which certainly wasn't appreciated by everyone, the company has also received backlash for its willingness to include AI-generated music on its platform. On May 17, Spotify promised the old icon would return “in a few weeks.” And now it looks like that time has finally arrived. So, whether you liked the disco ball or it made you uncomfortable, it’s now gone for good. The next time you update the Spotify app on your phone, the old, flat-design icon will return.
    • Playground Games confirms Forza Horizon 6 save wipe bug by Taras Buria Forza Horizon 6 was launched last month to critical acclaim (check out our review here), and it became a smash hit in an instant. Now, weeks into the launch, with die-hard fans clocking hundreds of hours, Forza Horizon 6 is facing a serious issue: save wipes. After multiple complaints on Reddit and social media, the studio issued a statement. The problem with missing saves came shortly after Playground Games promised the initial batch of gameplay tweaks and improvements. Unfortunately, there seems to be no temporary fixes for those affected by unexpected save wipes. However, the studio published a new support document with a few important steps users should try. First, affected gamers should open a support ticket immediately (go here to file one) so that the support team can try recovering the lost progress by reverting to an earlier save. Playground Games says this should be done the same day the issue occurs. Meanwhile, gamers are urged not to start new play sessions or create new saves. The studio also published a few things gamers should try to avoid to prevent potential progress loss: Ensure your Gaming Services app on PC or XBOX Series X|S console is fully up to date. On XBOX Series X|S consoles, disable Quick Resume for Forza Horizon 6: To disable Forza Horizon 6 from using Quick Resume, highlight the game box art anywhere in the console experience (Home, My Games & Apps, Pins, etc) and then press the Menu button, then go to Manage game and add-ons > Quick Resume settings > Disable Quick Resume. Ensure you are online when ‘quitting’ the game. Give your saved time to sync to the cloud before powering off or switching devices. Do not force quit the game during save screens. Do not power off the device during gameplay. Always "Quit" (console) or "Exit to desktop" (PC) once you've finished your play session, ensuring the save icon is not visible when you’re closing the game. Before turning off your console, shutting down your PC, or force-closing the Steam app, give your devices or clients at least a few minutes to ensure your latest progress has been synchronized with the cloud. This will reduce the risk of progress reversions as you switch between different platforms. XBOX Series X|S consoles, Steam, and the XBOX app on PC all include game save indicators that confirm your progress has been synced. You can read more about the bug in the official support document here. Forza Horizon 6 is currently available on PC (Steam and the Microsoft Store), Xbox Series X|S, and Game Pass. The game is also coming to PlayStation 5 later this year.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      522
    2. 2
      PsYcHoKiLLa
      199
    3. 3
      +Edouard
      158
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!