Home All Groups Group Topic Archive Search About
Author
28 Mar 2006 1:58 AM
Rob
Question : I want to create a read only TextBox that does not
respond to any input from the keyboard, therefore I came up
with the following derived class :

Option Strict On

Imports System.Windows.Forms.TextBox

Public Class DerivedTextBox
    Inherits TextBox

    Protected Overridable Sub KeyEventHandler( _
    ByVal sender As Object, _
    ByVal e As KeyEventHandler)

    End Sub

End Class


I then place this TextBox on my Form1 using the following :

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

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

        'Add any initialization after the InitializeComponent() call
        Me.TextBox1.Show()
        '
        'TextBox1
        '
        Me.TextBox1.AutoSize = False
        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(230, 150)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.TabStop = False
        Me.TextBox1.Text = ""
        Me.TextBox1.Visible = True
    End Sub

    'Form 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
    Friend WithEvents TextBox1 As DerivedTextBox

However the TextBox remains invisible. How can I get
TextBox1 to appear on Form1 ?

Author
28 Mar 2006 4:12 AM
Ken Tucker [MVP]
Hi,

        I dont see where you added it to the form's controls.

Ken
------------------
"Rob" <rls_***@worldnet.att.net> wrote in message
news:u0cgbsgUGHA.4436@TK2MSFTNGP10.phx.gbl...
Question : I want to create a read only TextBox that does not
respond to any input from the keyboard, therefore I came up
with the following derived class :

Option Strict On

Imports System.Windows.Forms.TextBox

Public Class DerivedTextBox
    Inherits TextBox

    Protected Overridable Sub KeyEventHandler( _
    ByVal sender As Object, _
    ByVal e As KeyEventHandler)

    End Sub

End Class


I then place this TextBox on my Form1 using the following :

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

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

        'Add any initialization after the InitializeComponent() call
        Me.TextBox1.Show()
        '
        'TextBox1
        '
        Me.TextBox1.AutoSize = False
        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(230, 150)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.TabStop = False
        Me.TextBox1.Text = ""
        Me.TextBox1.Visible = True
    End Sub

    'Form 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
    Friend WithEvents TextBox1 As DerivedTextBox

However the TextBox remains invisible. How can I get
TextBox1 to appear on Form1 ?
Author
28 Mar 2006 4:30 AM
Rob
Your right ! ! ! , I needed to make the following addition :
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1, Me.TextBox1})

Unfortunately, I'm still back to square one, my TextBox1 still
responds to keyboard events. Is there a way to rewrite the
Class DerivedTextBox to prevent keyboard response ?


Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:%235TUr3hUGHA.5372@TK2MSFTNGP09.phx.gbl...
> Hi,
>
>         I dont see where you added it to the form's controls.
>
> Ken
> ------------------
> "Rob" <rls_***@worldnet.att.net> wrote in message
> news:u0cgbsgUGHA.4436@TK2MSFTNGP10.phx.gbl...
> Question : I want to create a read only TextBox that does not
> respond to any input from the keyboard, therefore I came up
> with the following derived class :
>
> Option Strict On
>
> Imports System.Windows.Forms.TextBox
>
> Public Class DerivedTextBox
>     Inherits TextBox
>
>     Protected Overridable Sub KeyEventHandler( _
>     ByVal sender As Object, _
>     ByVal e As KeyEventHandler)
>
>     End Sub
>
> End Class
>
>
> I then place this TextBox on my Form1 using the following :
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>     Public Sub New()
>         MyBase.New()
>
>         'This call is required by the Windows Form Designer.
>         InitializeComponent()
>
>         'Add any initialization after the InitializeComponent() call
>         Me.TextBox1.Show()
>         '
>         'TextBox1
>         '
>         Me.TextBox1.AutoSize = False
>         Me.TextBox1.Location = New System.Drawing.Point(8, 8)
>         Me.TextBox1.Multiline = True
>         Me.TextBox1.Name = "TextBox1"
>         Me.TextBox1.ReadOnly = True
>         Me.TextBox1.Size = New System.Drawing.Size(230, 150)
>         Me.TextBox1.TabIndex = 1
>         Me.TextBox1.TabStop = False
>         Me.TextBox1.Text = ""
>         Me.TextBox1.Visible = True
>     End Sub
>
>     'Form 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
>     Friend WithEvents TextBox1 As DerivedTextBox
>
> However the TextBox remains invisible. How can I get
> TextBox1 to appear on Form1 ?
>
>
Author
28 Mar 2006 5:30 AM
Cor Ligthert [MVP]
Rob,

Why are you doing it this difficult. A label with a white background looks in my opinion the same.

Cor
  "Rob" <rls_***@worldnet.att.net> schreef in bericht news:u1TCgBiUGHA.5108@TK2MSFTNGP09.phx.gbl...
  Your right ! ! ! , I needed to make the following addition :
  Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1, Me.TextBox1})

  Unfortunately, I'm still back to square one, my TextBox1 still
  responds to keyboard events. Is there a way to rewrite the
  Class DerivedTextBox to prevent keyboard response ?


Show quoteHide quote
  "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:%235TUr3hUGHA.5372@TK2MSFTNGP09.phx.gbl...
  > Hi,
  >
  >         I dont see where you added it to the form's controls.
  >
  > Ken
  > ------------------
  > "Rob" <rls_***@worldnet.att.net> wrote in message
  > news:u0cgbsgUGHA.4436@TK2MSFTNGP10.phx.gbl...
  > Question : I want to create a read only TextBox that does not
  > respond to any input from the keyboard, therefore I came up
  > with the following derived class :
  >
  > Option Strict On
  >
  > Imports System.Windows.Forms.TextBox
  >
  > Public Class DerivedTextBox
  >     Inherits TextBox
  >
  >     Protected Overridable Sub KeyEventHandler( _
  >     ByVal sender As Object, _
  >     ByVal e As KeyEventHandler)
  >
  >     End Sub
  >
  > End Class
  >
  >
  > I then place this TextBox on my Form1 using the following :
  >
  > Public Class Form1
  >     Inherits System.Windows.Forms.Form
  >
  > #Region " Windows Form Designer generated code "
  >
  >     Public Sub New()
  >         MyBase.New()
  >
  >         'This call is required by the Windows Form Designer.
  >         InitializeComponent()
  >
  >         'Add any initialization after the InitializeComponent() call
  >         Me.TextBox1.Show()
  >         '
  >         'TextBox1
  >         '
  >         Me.TextBox1.AutoSize = False
  >         Me.TextBox1.Location = New System.Drawing.Point(8, 8)
  >         Me.TextBox1.Multiline = True
  >         Me.TextBox1.Name = "TextBox1"
  >         Me.TextBox1.ReadOnly = True
  >         Me.TextBox1.Size = New System.Drawing.Size(230, 150)
  >         Me.TextBox1.TabIndex = 1
  >         Me.TextBox1.TabStop = False
  >         Me.TextBox1.Text = ""
  >         Me.TextBox1.Visible = True
  >     End Sub
  >
  >     'Form 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
  >     Friend WithEvents TextBox1 As DerivedTextBox
  >
  > However the TextBox remains invisible. How can I get
  > TextBox1 to appear on Form1 ?
  >
  >
Author
28 Mar 2006 11:05 AM
Martin
Well, I can understand that you would want to make this dependent on a
property, to simulate the 'Locked' state we used to have in VB6. That
allowed the control to take focus, and to navigate inside the textbox with
the arrow keys, but did not allow the user to make any changes.
I am looking myself for such a 'state' too, so I can set a property
dependent on the users' rights. Such a state would be ideal for users who
only have 'Read' rights.
Currently I use the ReadOnly property for this, but that is so ugly. I looks
like the control is disabled.

Martin


"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:u8an4hiUGHA.1572@tk2msftngp13.phx.gbl...
Rob,

Why are you doing it this difficult. A label with a white background looks
in my opinion the same.

Cor
"Rob" <rls_***@worldnet.att.net> schreef in bericht
news:u1TCgBiUGHA.5108@TK2MSFTNGP09.phx.gbl...
Your right ! ! ! , I needed to make the following addition :
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1,
Me.Button1, Me.TextBox1})

Unfortunately, I'm still back to square one, my TextBox1 still
responds to keyboard events. Is there a way to rewrite the
Class DerivedTextBox to prevent keyboard response ?


Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%235TUr3hUGHA.5372@TK2MSFTNGP09.phx.gbl...
> Hi,
>
>         I dont see where you added it to the form's controls.
>
> Ken
> ------------------
> "Rob" <rls_***@worldnet.att.net> wrote in message
> news:u0cgbsgUGHA.4436@TK2MSFTNGP10.phx.gbl...
> Question : I want to create a read only TextBox that does not
> respond to any input from the keyboard, therefore I came up
> with the following derived class :
>
> Option Strict On
>
> Imports System.Windows.Forms.TextBox
>
> Public Class DerivedTextBox
>     Inherits TextBox
>
>     Protected Overridable Sub KeyEventHandler( _
>     ByVal sender As Object, _
>     ByVal e As KeyEventHandler)
>
>     End Sub
>
> End Class
>
>
> I then place this TextBox on my Form1 using the following :
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>     Public Sub New()
>         MyBase.New()
>
>         'This call is required by the Windows Form Designer.
>         InitializeComponent()
>
>         'Add any initialization after the InitializeComponent() call
>         Me.TextBox1.Show()
>         '
>         'TextBox1
>         '
>         Me.TextBox1.AutoSize = False
>         Me.TextBox1.Location = New System.Drawing.Point(8, 8)
>         Me.TextBox1.Multiline = True
>         Me.TextBox1.Name = "TextBox1"
>         Me.TextBox1.ReadOnly = True
>         Me.TextBox1.Size = New System.Drawing.Size(230, 150)
>         Me.TextBox1.TabIndex = 1
>         Me.TextBox1.TabStop = False
>         Me.TextBox1.Text = ""
>         Me.TextBox1.Visible = True
>     End Sub
>
>     'Form 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
>     Friend WithEvents TextBox1 As DerivedTextBox
>
> However the TextBox remains invisible. How can I get
> TextBox1 to appear on Form1 ?
>
>
Author
28 Mar 2006 11:17 AM
Cerebrus
Hi,

Martin wrote :

>> Currently I use the ReadOnly property for this, but that is so ugly. I looks
>> like the control is disabled.

Change the backcolor of the textbox back to normal, after you set it's
ReadOnly property to True. The normal behaviour is meant so that the
user gets a *visual* indication of the Readonly status. Else, users
would definitely try to type in the control a few times before
realizing that input is disabled, unless you give some other
indication.

Regards,

Cerebrus.
Author
28 Mar 2006 11:31 AM
Martin
Yup, that does the trick, so I will override the Readonly property in my
subclass to keep the original background color. That would also be the way
to go for Rob I think.

Thanks for your contribution

Martin

Show quoteHide quote
"Cerebrus" <zorg***@sify.com> wrote in message
news:1143544648.968707.116820@z34g2000cwc.googlegroups.com...
> Hi,
>
> Martin wrote :
>
>>> Currently I use the ReadOnly property for this, but that is so ugly. I
>>> looks
>>> like the control is disabled.
>
> Change the backcolor of the textbox back to normal, after you set it's
> ReadOnly property to True. The normal behaviour is meant so that the
> user gets a *visual* indication of the Readonly status. Else, users
> would definitely try to type in the control a few times before
> realizing that input is disabled, unless you give some other
> indication.
>
> Regards,
>
> Cerebrus.
>
Author
28 Mar 2006 11:36 AM
Martin
That makes me wonder... Why didn't MS put a ReadOnly property on other
controls, such as the combobox, checkbox and radiobutton? Now everybody who
needs that (and/or used it in VB6) has to re-invent the wheel all over
again.

Show quoteHide quote
"Cerebrus" <zorg***@sify.com> wrote in message
news:1143544648.968707.116820@z34g2000cwc.googlegroups.com...
> Hi,
>
> Martin wrote :
>
>>> Currently I use the ReadOnly property for this, but that is so ugly. I
>>> looks
>>> like the control is disabled.
>
> Change the backcolor of the textbox back to normal, after you set it's
> ReadOnly property to True. The normal behaviour is meant so that the
> user gets a *visual* indication of the Readonly status. Else, users
> would definitely try to type in the control a few times before
> realizing that input is disabled, unless you give some other
> indication.
>
> Regards,
>
> Cerebrus.
>
Author
28 Mar 2006 11:28 AM
Ken Tucker [MVP]
Hi,

        In the keypress event you need e.handled=true

Ken
---------------
"Rob" <rls_***@worldnet.att.net> wrote in message
news:u1TCgBiUGHA.5108@TK2MSFTNGP09.phx.gbl...
Your right ! ! ! , I needed to make the following addition :
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1,
Me.Button1, Me.TextBox1})

Unfortunately, I'm still back to square one, my TextBox1 still
responds to keyboard events. Is there a way to rewrite the
Class DerivedTextBox to prevent keyboard response ?


Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%235TUr3hUGHA.5372@TK2MSFTNGP09.phx.gbl...
> Hi,
>
>         I dont see where you added it to the form's controls.
>
> Ken
> ------------------
> "Rob" <rls_***@worldnet.att.net> wrote in message
> news:u0cgbsgUGHA.4436@TK2MSFTNGP10.phx.gbl...
> Question : I want to create a read only TextBox that does not
> respond to any input from the keyboard, therefore I came up
> with the following derived class :
>
> Option Strict On
>
> Imports System.Windows.Forms.TextBox
>
> Public Class DerivedTextBox
>     Inherits TextBox
>
>     Protected Overridable Sub KeyEventHandler( _
>     ByVal sender As Object, _
>     ByVal e As KeyEventHandler)
>
>     End Sub
>
> End Class
>
>
> I then place this TextBox on my Form1 using the following :
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>     Public Sub New()
>         MyBase.New()
>
>         'This call is required by the Windows Form Designer.
>         InitializeComponent()
>
>         'Add any initialization after the InitializeComponent() call
>         Me.TextBox1.Show()
>         '
>         'TextBox1
>         '
>         Me.TextBox1.AutoSize = False
>         Me.TextBox1.Location = New System.Drawing.Point(8, 8)
>         Me.TextBox1.Multiline = True
>         Me.TextBox1.Name = "TextBox1"
>         Me.TextBox1.ReadOnly = True
>         Me.TextBox1.Size = New System.Drawing.Size(230, 150)
>         Me.TextBox1.TabIndex = 1
>         Me.TextBox1.TabStop = False
>         Me.TextBox1.Text = ""
>         Me.TextBox1.Visible = True
>     End Sub
>
>     'Form 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
>     Friend WithEvents TextBox1 As DerivedTextBox
>
> However the TextBox remains invisible. How can I get
> TextBox1 to appear on Form1 ?
>
>