Controls disabled or Readonly

M

Michael Jackson

On my .NET 2003 form(s), I'm restricting changing of existing records by
setting the Enabled property to False on the bound controls. However, I
notice that the textbox and combobox is painted differently when disabled.
Plus, the grayed out text is hard to read. I've also tried using readonly,
but don't like that option either. I prefer the controls keep the enabled
look if possible.

Anyone have a technique for ensuring existing data is not changed and/or
allowing entry only when a new record is being entered, short of e.Handled
on the keypress event.

Thanks,

Michael
 
B

Beth Massi [Architect MVP]

Hi Michael,

We do something similar. We "disable" databound controls when the position
in the currency manager goes to -1 (no record). What you can do is add a
property to your textbox baseclass to control the look you want. You could
create an interface and then implement it in the controls you want to
disable. Something like:

Imports System.ComponentModel
Imports System.ComponentModel.Design

Public Interface IDataDisableable
Property DataEnabled() As Boolean
Event DataEnabledChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
End Interface

Public Class BaseTextBox
Inherits System.Windows.Forms.TextBox
Implements IDataDisableable

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() call
InitializeProperties()
End Sub

'Control 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 Control 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()
'
'BaseTextBox
'
End Sub

#End Region

Private m_dataEnabled As Boolean = True

Public Event DataEnabledChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Implements IDataDisableable.DataEnabledChanged

Protected Overridable Sub OnDataEnabledChanged()
RaiseEvent DataEnabledChanged(Me, EventArgs.Empty)
End Sub

Public Property DataEnabled() As Boolean Implements
IDataDisableable.DataEnabled
Get
Return m_dataEnabled
End Get
Set(ByVal Value As Boolean)
If m_dataEnabled <> Value Then
If Not (Value) Then
Me.ReadOnly = True
Me.TabStop = False
Me.BackColor = SystemColors.Control
Else
Me.ReadOnly = False
Me.TabStop = True
Me.BackColor = SystemColors.Window
End If

m_dataEnabled = Value
OnDataEnabledChanged()
End If
End Set
End Property

End Class

Then on your form you would handle the PositionChanged event of the currency
manager realted to the binding you're interested in. If the current row
view's IsNew property returned False, you could set all your databound
control's DataEnabled property to False. Something like:

Private WithEvents CM As CurrencyManager

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.BaseTextBox1.DataBindings.Add("Text", Me.DataSet1, "Table1.Field1")

'-- Make sure you grab the right currency manager by specifying the correct
binding member (path).
CM = DirectCast(Me.BindingContext(Me.DataSet1, "Table1"), CurrencyManager)
End Sub

Private Sub CM_PositionChanged(ByVal sender As Object, ByVal e As EventArgs)
Handles CM.PositionChanged
'-- Now when the position changes, you can enable/disable the controls.
If CM.Position > -1 Then
If DirectCast(CM.Current, DataRowView).IsNew Then
EnableDataControls(CM)
Else
DisableDataControls(CM)
End If
Else
DisableDataControls(CM)
End If

End Sub

Private Sub EnableDataControls(ByVal cm As CurrencyManager)
For Each b As Binding In cm.Bindings
If TypeOf b.Control Is IDataDisableable Then
DirectCast(b.Control, IDataDisableable).DataEnabled = True
End If
Next
End Sub

Private Sub DisableDataControls(ByVal cm As CurrencyManager)
For Each b As Binding In cm.Bindings
If TypeOf b.Control Is IDataDisableable Then
DirectCast(b.Control, IDataDisableable).DataEnabled = False
End If
Next
End Sub

Hope this gets you on your way....
-B
 

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