How to programmatically change properties of many controls on a form

T

Tom

I am writing a Visual basic .Net database application.
There are many forms that first let you select and look at
a DB record and then when you click a "modify" button you
are allowed to change data in the text boxes. Then
an "update" button saves the data back to the database.

What is the best way to change several properties on most
of the textbox controls on the form when the "modify"
button is clicked? (e.g. make them editible (and hence not
grayed out.) Should the click event for modify loop
through all the controls on the form doing it manually or
is there a way to just have the modify button change a
public variable and have each text box use it to change
itself? (Note that I have already made my own version of
a textbox so it is easy to add a bit of extra code to it.)

As I have many forms, I am interested in doing this
the "right" way to minimize maintenance and spaghetti code.

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
Looping through the controls is one way, the danger of using the Form's
control collection is text boxes that you do not want to change.

I would consider creating a new Component that I could register each of my
text boxes with. The Component would have a property (field) that you
change. When you change the field the Component would either raise an event
or change each text box.

The advantage of raising an event is that its easier to introduce new
controls that "follow" the property.

Seeing as you already have your own version of the TextBox, it would be easy
enough for the text box to have a property of this component, so that the
TextBox itself could listen for the event & change itself accordingly...

This Component may derive from one of the CurrencyMangers or be a
CurrencyManager, depending on what CurrencyManager supplies and what this
class needs.

Hope this helps
Jay
 
G

Guest

I'm embarrassed to say that I have been just running through the controls collection to do this (though I have used panels to manage the job more efficiently). Your way sounds A LOT better. Do you have a reference for an example for me to use to understand what you're talking about?

Thanks,
 
J

Jay B. Harlow [MVP - Outlook]

Pat,
What I am talking about in general is OOP (Abstraction, Encapsulation,
Polymorphism, Inheritance).

What I am talking about specifically is to create a Component, which is a
class that inherits from System.ComponentModel.Component. Inheriting from
Component allows you to use your component with the designer, simplifiing
design. If design time support is not important you could start with a
normal Class.

For details on creating Components start here:
http://msdn.microsoft.com/library/d.../en-us/vbcon/html/vbconComponentAuthoring.asp

A start for your component would be:

Public Class Component1
Inherits System.ComponentModel.Component

#Region " Component Designer generated code "

Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region

Public Event StatusChanged As EventHandler

Private m_status As Boolean

Protected Overridable Sub OnStatusChanged(ByVal e As EventArgs)
RaiseEvent StatusChanged(Me, e)
End Sub

Public Property Status() As Boolean
Get
Return m_status
End Get
Set(ByVal value As Boolean)
m_status = value
OnStatusChanged(EventArgs.Empty)
End Set
End Property

End Class


Then within your TextBox controls.

Public Class TextBoxEx

Private WithEvents m_component1 As Component1

Private Sub Component1_StatusChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles m_component1.StatusChanged

End Sub

Public Property Component1() As Component1
Get
Return m_component1
End Get
Set(ByVal value As Component1)
m_component1 = value
End Set
End Property

End Class


Within the form designer you can add a TextBoxEx to your Form and a
Component1 to your form, you will see the Component1 that you dropped on the
form when you select the drop down.

Check out the following articles to make Component1 even more designer
friendly!

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/usingpropgrid.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vsnetpropbrow.asp

Hope this helps
Jay


pmcguire said:
I'm embarrassed to say that I have been just running through the controls
collection to do this (though I have used panels to manage the job more
efficiently). Your way sounds A LOT better. Do you have a reference for an
example for me to use to understand what you're talking about?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top