|
-
Nov 13th, 2005, 11:55 AM
#1
Thread Starter
Member
[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
-
Nov 13th, 2005, 12:29 PM
#2
Re: Modifying Form1.textbox1 Atttributes from Form2
Thats because your creating a second instance of Form1...
You need to access your form2's parentform property and set the textbox from the controls index property.
VB Code:
If rbBlue.Checked = True Then
'Assumes that control index 2 is your textbox.
Me.Owner.Controls.Item(2).ForeColor = System.Drawing.Color.Blue
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/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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 13th, 2005, 01:42 PM
#3
Thread Starter
Member
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
-
Nov 13th, 2005, 02:07 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 13th, 2005, 02:50 PM
#5
Thread Starter
Member
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
-
Nov 13th, 2005, 03:07 PM
#6
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:
'In a module
Public Module Module1
Public goForm1 As Form1
Public Sub Main
goForm1 = New Form1
Application.Run(goForm1)
End Sub
End Module
Then from Form2 you only need to access form1 from its object variable.
VB Code:
'Form2
Private Sub Button1_Click(ByVal sender as object, ....) Handles Button1.Click
goForm1.TextBox1.Forecolor = system.drawing.colors.blue
goForm1.TextBox1.Text = "Set from Form2"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 13th, 2005, 03:18 PM
#7
Thread Starter
Member
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
-
Nov 13th, 2005, 04:00 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 13th, 2005, 04:57 PM
#9
Thread Starter
Member
Re: Modifying Form1.textbox1 Atttributes from Form2
Thanks.
-
Nov 13th, 2005, 07:48 PM
#10
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.
-
Nov 13th, 2005, 08:30 PM
#11
Thread Starter
Member
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
-
Nov 13th, 2005, 08:34 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 13th, 2005, 08:44 PM
#13
Re: [RESOLVED] Modifying Form1.textbox1 Atttributes from Form2
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|