Results 1 to 13 of 13

Thread: [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Resolved [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2

    I am just learning VB.Net and I'm having a problem with one of the exercises. The excersie is a simple textbox example using two forms (Form1 & Form2).

    > Form1 holds a text box (TextBox1) and an Options button.

    > TextBox1 is instantiated as follows (This is extracted IDE code):

    Public WithEvents TextBox1 As System.Windows.Forms.TextBox

    Me.TextBox1 = New System.Windows.Forms.TextBox

    Me.TextBox1.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, _ CType(0, Byte))
    Me.TextBox1.Location = New System.Drawing.Point(8, 8)
    Me.TextBox1.Multiline = True
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(512, 504)
    Me.TextBox1.TabIndex = 0
    Me.TextBox1.Text = ""

    > The click event of the options button instantiates a new Form2 as follows:
    Dim N As New Form2
    N.ShowDialog()

    > Form2 consistes of two groups of radio buttons (Color and Border Style) and two buttons (Accept & Cancel).

    > The click event of Accept button tests the states of the radio buttons and sets the attributes of Form1.TextBox1 according using the following code (line continuation added) :

    Dim N As New Form1
    If rbBlue.Checked = True Then TextBox1.ForeColor = _ System.Drawing.Color.Blue

    If rbRed.Checked = True Then N.TextBox1.ForeColor = _ System.Drawing.Color.Red

    If rbGreen.Checked = True Then N.TextBox1.ForeColor = _ System.Drawing.Color.Green

    If rbBlack.Checked = True Then N.TextBox1.ForeColor = _ System.Drawing.Color.Black

    If rbBorder3D.Checked = True Then N.TextBox1.BorderStyle = _ System.Windows.Forms.BorderStyle.Fixed3D

    If rbBorderFlat.Checked = True Then N.TextBox1.BorderStyle = _ System.Windows.Forms.BorderStyle.FixedSingle

    Me.Hide()

    ** The application runs without any problems (i.e. no compile or runtime errors) but if you set the color and border style options on Form2 (Options Form), when Form2 closes the attributes of Form1.TextBox1 have not changed.

    I've experimented by placing code for chaging the attributes for TextBox1 directly in the Form1.Options button and it works perfectly, however, I want to make it work as it was intended.

    I've also tried adding a N.Show() command at the end of the Accept button on Form2. This shows the new instance of Form1 (N) with the TextBox attributes correctly set but I now have two instances of Form1 showing!

    I want to be able to set the attributes of a TextBox on one form, from another form.

    I'm sure this is fairly simple but I just cant figure out what I'm doing wrong.

    Anyone, Please help.

    Email: [email protected]

    Rob C

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Thats because your creating a second instance of Form1...
    VB Code:
    1. Dim N As New Form1
    You need to access your form2's parentform property and set the textbox from the controls index property.
    VB Code:
    1. If rbBlue.Checked = True Then
    2.     'Assumes that control index 2 is your textbox.
    3.     Me.Owner.Controls.Item(2).ForeColor = System.Drawing.Color.Blue
    4. End If
    You would be better using Reflection instead of the Control's index but this is the simpler way.

    You can also create a Form property for passing the value back to form1 to set the forecolor of the textbox.


    Oh and also if your using the Owner you should show the second form and set the owner in the .ShowDialog argument.

    VB Code:
    1. Form2.ShowDialog(Me)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Thank you for your input. I believe I understand what you are saying and it fits in with my thinking that the problem is that I'm creating a new instance of Form1 instead of modifying the attributes of the existing Form1. However, I do have a follow up question. In your code you suggest I modify Form1 properties using the following:

    Me.Owner.Controls.Item(2).ForeColor = System.Drawing.Color.Blue

    However, remember that this code is in Form2 and I am trying to modify the properties of a TexBox on Form1. I would think, in this case, that I can't use [Me.] as that is a reference to the current form (i.e Form2) and not Form1.

    How would I need reformat the line to reference Form1's TextBox from code within Form2?

    Much Obliged.

    Rob C

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Yes, Me is Form2 but the Owner property identifies Form1 as when you set the owner argument when .ShowDialog(Me) from Form1 and Me being form1.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Re: Modifying Form1.textbox1 Atttributes from Form2

    I understand. I've made the changes and the color selection option works fine now. However, it seems I can not use this method to change the borderstyle of a TextBox as borderstyle is not a member of Systems.Windows.Forms.Control. This is minor as the whole point of the excersise was to gain a BASIC understanding of how to change the attributes of a control on one form from another and I understand that now.

    Your assitance is greatly appreciated.

    Thanks,

    Rob C

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Modifying Form1.textbox1 Atttributes from Form2

    You may want to try creating a public form object variable so you can access its controls and properties if your working with multiple forms. Its always good to know many ways to do the same thing as each has its place and benefit.
    VB Code:
    1. 'In a module
    2. Public Module Module1
    3.  
    4. Public goForm1 As Form1
    5.  
    6. Public Sub Main
    7.     goForm1 = New Form1
    8.     Application.Run(goForm1)
    9. End Sub
    10.  
    11. End Module
    Then from Form2 you only need to access form1 from its object variable.
    VB Code:
    1. 'Form2
    2. Private Sub Button1_Click(ByVal sender as object, ....) Handles Button1.Click
    3.     goForm1.TextBox1.Forecolor = system.drawing.colors.blue
    4.     goForm1.TextBox1.Text = "Set from Form2"
    5. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Excellent. I will give that a try as well. If you have a moment, any chance you could point me in the right direction for learning about using "reflection" as well? You peaked my interest earlier but I've had little luck getting a grip on the concept.

    I can't tell you how helpful you've been and I hope I haven't been a pain.

    Thanks Again,

    Rob C

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Reflection is alot more complex then using properties or public variables. I am not too good at it but others are. Try a search for Reflection in the vb.net forum.

    No worries as this is what the Foums are all about.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Resolved Re: Modifying Form1.textbox1 Atttributes from Form2

    Thanks.

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

    Re: Modifying Form1.textbox1 Atttributes from Form2

    Don't forget to resolve your thread from the Thread Tools menu. Only the icon/title from the first post shows up in the thread listing and the menu item edits those.
    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

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    32

    Re: [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2

    Sorry bout that. Still finding my feet in the forum. I now realise that I also inserted VB code incorrectly. An error wich I will not repeat in any future posts.

    Thank You

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2

    Its never a problem as we realize it takes some getting used to.

    We do have a Test Forum where you can make test posts to learn how to use any of the forums tools.

    Always glad to help.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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

    Re: [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2

    Quote Originally Posted by RobC_UK
    Sorry bout that. Still finding my feet in the forum. I now realise that I also inserted VB code incorrectly. An error wich I will not repeat in any future posts.

    Thank You
    No need to apologise. I was just steering you in the right direction, not having a go.
    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