Results 1 to 10 of 10

Thread: Show and Hide Forms ( New to .net)

  1. #1

    Thread Starter
    Addicted Member chatty's Avatar
    Join Date
    Aug 2002
    Location
    in a house
    Posts
    202

    Show and Hide Forms ( New to .net)

    Hi

    I have two forms ( Form1, Form2).
    On Form1 i have a button, when i click that button form1 must close and Form2 must Show.

    How do I do that????

    Thanx

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    VB Code:
    1. 'in the form1
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim f As New Form2(Me)
    5.         f.Show()
    6. End Sub
    7.  
    8. 'in the form2
    9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.    Me.f.Close()
    11. End Sub
    12. 'to the constructor of the form2
    13.  Dim f As Form
    14.     Public Sub New(ByVal f As Form1)
    15.         MyBase.New()
    16.  
    17.         'This call is required by the Windows Form Designer.
    18.         InitializeComponent()
    19.         Me.f = f
    20.         'Add any initialization after the InitializeComponent() call
    21.  
    22.     End Sub
    23.  
    24. 'to the module...make it as a startup object
    25. Sub main()
    26.         Dim f As New Form1()
    27.         f.Show()
    28.         Application.Run()
    29.     End Sub
    or this one
    VB Code:
    1. ' form2
    2. 'in the main form
    3. public logged as boolean=false
    4. 'in the form load
    5. dim x as new form1()
    6. me.addownedform(x)
    7. x.showdialog()
    8. if logged = false then
    9.    application.doevents()
    10. end if
    11.  
    12. 'in the  form1
    13. dim m as form2
    14. 'in the formload
    15. m= me.owner
    16. 'in button click
    17. m.logged = true
    18. close()
    19.  
    20. make the form2 as startup object...

    courtesy of dynamic sysop
    Last edited by mar_zim; Jul 28th, 2004 at 02:04 AM.

  3. #3
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040
    this is very simple,

    VB Code:
    1. dim x as new form2
    2. x.show
    3. me.close

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by maged
    this is very simple,

    VB Code:
    1. dim x as new form2
    2. x.show
    3. me.close
    Does that work?

  5. #5

    Thread Starter
    Addicted Member chatty's Avatar
    Join Date
    Aug 2002
    Location
    in a house
    Posts
    202
    Thanx
    Next step is to load a Datagrid from a SQL Database

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    maged i dont think your code will work...

    about loading the datagrid from sql
    VB Code:
    1. 'cn is an sqlconnection
    2. dim dt as new datatable("sampletable")
    3. dim da as new sqldataadapter("select * from categories",cn)
    4. da.fill(dt)
    5. datagrid1.datasource = dt

  7. #7
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Good morning to everyone, dear friends.
    Dear Maged, I think your is the simplest way, but if the form in which this code is placed is the default start up object, as probably is, closing Form1, will cause your app. end.
    I suggest to change:

    dim x as new form2
    x.show
    me.close


    in

    Dim f2 As New Form2
    Me.Hide()
    f2.ShowDialog()
    Me.Close()

    Form1 is not really closed, until Form2 closes, but it seems so anyway.

    Obviously you could really close Form1,starting from a module, running Form1, then when it will be closed, running Form2, but probably it's a good idea, at this moment, to stay on simple solutions. There will be time to worry on these subjects, for our friend chatty!
    Anyway, it should be something like (By memory...check syntax before use. Only idea is important!!!):


    Public Sub Main 'In a module and declared as starting object in project's properties

    Dim F1 as new Form1
    F1.ShowDialog() 'The button in F1 will close F1 itself
    Dim F2 As New Form2
    F2.Showdialog()
    End

    End Sub


    Good job!
    Live long and prosper (Mr. Spock)

  8. #8

    Thread Starter
    Addicted Member chatty's Avatar
    Join Date
    Aug 2002
    Location
    in a house
    Posts
    202
    How do i Update, Insert or Delete a record without using a dataset.
    Do i use ExecuteNonQuery?
    Last edited by chatty; Jul 28th, 2004 at 03:16 AM.

  9. #9
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    a slight modification of mzims solution
    VB Code:
    1. '  on form1
    2.    Shared Sub Main()
    3.       Dim f As New Form1()
    4.       f.Show()
    5.       Application.Run()
    6.    End Sub
    7.  
    8.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.       Dim f As New Form2(Me)
    10.       f.Show()
    11.    End Sub
    12.  
    13. '  on form2
    14.    Dim f As Form
    15.    Public Sub New(ByVal f As Form)
    16.       MyBase.New()
    17.  
    18.       'This call is required by the Windows Form Designer.
    19.       InitializeComponent()
    20.       Me.f = f
    21.  
    22.       'Add any initialization after the InitializeComponent() call
    23.  
    24.    End Sub
    25.  
    26.    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.       Me.f.Close()
    28.    End Sub
    29.  
    30.    Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    31.       Application.Exit()
    32.    End Sub
    though module can have this run but i don't recommend modules because the main purpose of module are structures and enums. the thing that needs no encapsulation. cheers mate.

    remember: application.run() has nothing on it so we should .exit() this thing. hope this helps mate. if not, sorry.

  10. #10
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    mendhak is good at this ado.net thing. i'm sure he can give exact details. but executeNonQuery() is used for non queries. sample: update, delete, entry or even create a table/database through code. one sample, a basic one cause i'm a noob too. hehehe.
    VB Code:
    1. dim cn as new sqlconnection()
    2. cn.connectionstring=[i]conn_string[/i]
    3. cn.open()
    4. dim cm as new sqlcommand("insert into table_name values('field1','field2')",cn)
    5. cm.executenonquery()
    where table_1 has 2 fields with data type string. cheers mate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width