Results 1 to 25 of 25

Thread: [RESOLVED] access to forms

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Resolved [RESOLVED] access to forms

    I have attached a small project.


    i have trouble getting access to controls on forms from other forms.



    form3 cannot get access to the textbox on form2.

    could someone look this over to see how I can access form 2 from form3??
    Attached Files Attached Files

  2. #2
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    I haven't opened your zip file, but this is a quick access method:

    Dim f As Form = Application.OpenForms("Form2")

    You now have a variable referencing Form2; do with it what you will...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    your code looks similar to the code below, it does not allow access to all forms, only some. open my code and look. I have approx. 35 forms and I want to use the method of calling them up that I already have if possible, other wise i will have too much code to change.




    dim o2 as form1



    in a command button:


    o2 = new form2


    o2.showdialog()

  4. #4
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    you seem to be labouring a different requirement here.

    if showdialog works, but you can't access the textbox, then I wager that you have not set the textbox's modifier property to friend or public...

    i don't open zip files, by the way.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    oh, ok.


    I will check the modifier property.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    i can modify the textbox from form 2 but not from form3.


    sorry, your solution did not help.

  7. #7
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    by the way, i sure as hell hope that you attempt to modify the textbox BEFORE you use the showdialog method. you realise, of course, that no code in form3 will execute until you have closed form2...?!

  8. #8
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    (in other words, use the show method, not the showdialog...)

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    I cannot use the .show method. it messes up the way the forms work.

  10. #10
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    since you have several forms, you will need to create a base form class which overloads showdialog (note that line 6 is for demo purposes only and should not be included in your production code):
    vb.net Code:
    1. Public Class Form2
    2.     Private _f As Form
    3.  
    4.     Overloads Sub ShowDialog(ByVal f As Form, ByVal e As MyEventArgs)
    5.         _f = f  ' _f now holds a reference to Form1, the public properties and fields of which can now be accessed
    6.         Me.TextBox1.Text = e.TextString  ' writes to the wretched textbox!
    7.         Me.ShowDialog(_f)
    8.     End Sub
    9. End Class
    10.  
    11.  
    12. Public Class MyEventArgs
    13.     ' whatever needs to be passed to the forms, probably as a collection. this example uses a string only
    14.     Private _msg As String
    15.  
    16.     Public Sub New(ByVal msg As String)
    17.         _msg = msg
    18.     End Sub
    19.  
    20.     ReadOnly Property TextString() As String
    21.         Get
    22.             Return _msg
    23.         End Get
    24.     End Property
    25. End Class
    You will build MyEventArgs to house the most efficient collection type for handling the info you will be sending to your 30-something forms. You will call derived classes like this:
    vb.net Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim f As Form2 = New Form2
    3.         Dim fe As MyEventArgs = New MyEventArgs("Hello World")
    4.         f.ShowDialog(Me, fe)
    5. End Sub

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    ok, i will start on it tomorrow.

    i notice that most times there seem to be "Line Numbers" in vb.net code or is this just for the forum only?


    thanks!!!!!

  12. #12
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: access to forms

    no. you can set line numbers to show in your vb ide by Tools > Options > Text Editor > Basic > General / (Display) Line Numbers.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    ok and thanks!!!

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: access to forms

    Quote Originally Posted by RonR
    ok, i will start on it tomorrow.

    i notice that most times there seem to be "Line Numbers" in vb.net code or is this just for the forum only?


    thanks!!!!!
    The line numbers you see in posts on this forum are created by this forum. They have no relation to the line numbers in the original code, if it even came from an IDE. While you can turn line numbers on in VS I don't really see the point. It takes away some space that could otherwise be displaying code for one thing. 24" wide-screen, 1920x1200 monitor in portrait mode and I work with VS maximised all the time and I often wish the screen was wider. I don't know how people cope with smaller monitors. Also, how often do you really need to to see the line numbers? The IDE shows the current line number in the status bar so if you really need to get to a particular line you can using that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: access to forms

    What is the problem with just doing this, it lets you do what you want ?
    Code:
    Form2.ShowDialog()
    
    'and
    
    Form3.ShowDialog()

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    Quote Originally Posted by user name
    What is the problem with just doing this, it lets you do what you want ?
    Code:
    Form2.ShowDialog()
    
    'and
    
    Form3.ShowDialog()

    no, it does not work when I have 3 forms on the screen and I want to access form 1.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    Quote Originally Posted by jmcilhinney
    The line numbers you see in posts on this forum are created by this forum. They have no relation to the line numbers in the original code, if it even came from an IDE. While you can turn line numbers on in VS I don't really see the point. It takes away some space that could otherwise be displaying code for one thing. 24" wide-screen, 1920x1200 monitor in portrait mode and I work with VS maximised all the time and I often wish the screen was wider. I don't know how people cope with smaller monitors. Also, how often do you really need to to see the line numbers? The IDE shows the current line number in the status bar so if you really need to get to a particular line you can using that.
    now i know where BOTH line numbers come from.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: access to forms

    Quote Originally Posted by jmcilhinney
    The line numbers you see in posts on this forum are created by this forum. They have no relation to the line numbers in the original code, if it even came from an IDE. While you can turn line numbers on in VS I don't really see the point. It takes away some space that could otherwise be displaying code for one thing. 24" wide-screen, 1920x1200 monitor in portrait mode and I work with VS maximised all the time and I often wish the screen was wider. I don't know how people cope with smaller monitors. Also, how often do you really need to to see the line numbers? The IDE shows the current line number in the status bar so if you really need to get to a particular line you can using that.

    you've got a 24" wide-screen monitor!?
    don't you just see a larger version of the same thing with a big monitor?

  19. #19
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: access to forms

    Quote Originally Posted by RonR
    no, it does not work when I have 3 forms on the screen and I want to access form 1.
    I have just opened your test project, changed the code to Form2.ShowDialog and Form3.ShowDialog and everything was ok.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    Quote Originally Posted by user name
    I have just opened your test project, changed the code to Form2.ShowDialog and Form3.ShowDialog and everything was ok.

    try to get the text to display in form 2 from form3

  21. #21
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: access to forms

    You mean by pressing "to form 2" button like i did ?
    Attached Images Attached Images  

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    Quote Originally Posted by user name
    You mean by pressing "to form 2" button like i did ?

    it does not work for me.

    maybe it is because I have vb 2008 xe?

  23. #23
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: access to forms

    Just as a test for you, i have opened it in 2008pro, changed the code and zipped it back up.
    Attached Files Attached Files

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: access to forms

    Quote Originally Posted by user name
    Just as a test for you, i have opened it in 2008pro, changed the code and zipped it back up.
    i thought that i had to use the: o1 as new form3


    does this create potential other issues??

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: access to forms

    Quote Originally Posted by .paul.
    you've got a 24" wide-screen monitor!?
    don't you just see a larger version of the same thing with a big monitor?
    If you have an A4 sheet of paper and an A3 sheet of paper, if the font is the same size on both then you get twice as much text on the A3 sheet. If you were to double the size of the font then then you get the same amount of text at twice the size.

    If you've got more pixels on a screen you can fit more on it. If your screen is 1024x768 and you have a window a 1024x768 then it occupies the whole screen. The same form on a screen at 1920x1200 occupies only a fraction of the entire screen.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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