• 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

    • Close, but it is for any application starting, not just during boot time. In fact, it probably will not improve boot time at all because during a boot, the CPU is already boosting, so a pre-boost wouldn't change anything. I agree that it isn't exciting (especially considering every other OS already does it), but it is a good thing, even if they are just playing catch up.
    • Any so called performance increase will be in milliseconds, which nobody will actually notice in real world usage.
    • All it does is use the CPU more efficiently during boot to speed up boot times. That's it. Yawn....
    • It's not a one or the other kind of thing. Software should run efficiently, and the operating system should appropriately manage the CPU clocks. You could have the best most optimized software on earth, and it will still run faster if the CPU does a better job of boosting as needed. All this is doing is pre-boosting the CPU based on user actions, instead of waiting for the normal detection mechanism to kick in. If the OS knows it is about to need more CPU, why shouldn't it use that knowledge? It's the same idea of downshifting before passing someone, instead of just burying your foot into the peddle and waiting for the transmission to figure out what you want to do.
    • Audacity 3.7.8 by Razvan Serea Audacity is a free, open source digital audio editor and recording application. Edit your sounds using cut, copy, and paste features (with unlimited undo functionality), mix tracks, or apply effects to your recordings. The program also has a built-in amplitude-envelope editor, a customizable spectrogram mode, and a frequency-analysis window for audio-analysis applications. Built-in effects include bass boost, wah wah, and noise removal, and the program also supports VST plug-in effects. You can use Audacity to: Record live audio. Record computer playback on any Windows Vista or later machine. Convert tapes and records into digital recordings or CDs. Edit WAV, AIFF, FLAC, MP2, MP3 or Ogg Vorbis sound files. AC3, M4A/M4R (AAC), WMA and other formats supported using optional libraries. Cut, copy, splice or mix sounds together. Numerous effects including change the speed or pitch of a recording. Write your own plug-in effects with Nyquist. And more! See the complete list of features. Audacity 3.7.8 changelog: #10688 Fixed an exception thrown when pasting into a newly-created track (Thanks, David Bailes (@DavidBailes)!) #10870, #10884, #10775, #10629 Fixed tone generation, waveform-scale setting, SetClip Name parameter, and clip-boundary command names for scripting and macros (Thank you, David Bailes (@DavidBailes)!) #11106 Fixed the loading of presets for the Distortion effect (A million thanks, David Bailes (@DavidBailes)!) #10947 Fixed paste into an empty audio track not preserving the source sample rate (Thanks, Juan Gabriel Colonna (@juancolonna)!) #10776 Allowed AltGr modifier in label and clip name editing (Thanks, Davide Peressoni (@DPDmancul)!) #9938 Added options to choose where silence is truncated (start/middle/end) (Thanks, Noah Rosenfield (@nosenfield)!) #9935 Added Podcast 2.0 chapters JSON export for label tracks (Thanks, Noah Rosenfield (@nosenfield)!) #10103 Improve UI on HiDPI displays on Linux/wxGTK (Thanks, Ivan A. Melnikov (@iv-m)!) #10099 Fixed MixerBoard Mute and Solo button display (Thanks, Ivan A. Melnikov (@iv-m)!) #10681 Fixed multichannel FLAC import #10999 Fixed envelope being broken after joining clips Download: Audacity 64-bit | Standalone ~20.0 MB (Open Source) Download: Audacity 32-bit | Standalone Download: Audacity ARM64 | Standalone View: Audacity Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • 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
      500
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      71
  • Tell a friend

    Love Neowin? Tell a friend!