Textbox ReadOnly or Disabled....

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Not sure whether this is one of those things you cannot get around, but here goes :rolleyes:

When a user submits data from my data entry form, all fields become disabled (enabled=false). However, a couple of my textboxes are filled in with an amount of data, and so have vertical scrollbars.

When a textbox is disabled, the scrollbar doesn't work. It does when the textbox is readonly.

This to me isn't a problem, but the users can't see why they can click into that textbox but not enter any text (give them a colouring in book and crayons any day...:p )

So my question is, is there;

a) A way to make a disabled textbox have an enabled scrollbar OR
b) Have a read-only textbox, but not be "clickable"

Regards,
Luke
 
What I mean is I don't want the user to be able to click into the textbox and them then think they can type - I want the textbox to be like it's disabled, you can't click into it - but I want them to be able to use the scrollbar if it's required.

It's those annoying users. If they weren't there then there'd be no problems with my apps....and I probably wouldn't have a job :D
 
ya mean...

27.7 How do I prevent a control from receiving focus when it receives a mouseclick?


You can set the control's Enabled property to false. This will prevent clicking but also gives the disabled look.

There is a ControlStyles.Selectable flag that determines whether the control can get focus or not. But it does not appear to affect some controls such as TextBox. But you can prevent the TextBox from getting focus by overriding its WndProc method and ignoring the mouse down.
public class MyTextBox : TextBox
{
const int WM_LBUTTONDOWN = 0x0201;

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_LBUTTONDOWN)
return;
base.WndProc(ref m);
}
}



then yes!! But this will be the first time I've ever done some overriding, so how do I go about that?

Regards,
Luke
 
OK, I have now put the class together and have;

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] MyTextBox
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] TextBox
[/SIZE][SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] WM_LBUTTONDOWN [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0x0201
[/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Overloads[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] WndProc([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] m.Msg = WM_LBUTTONDOWN [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].WndProc(m)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=black]
[/COLOR][/SIZE]

So, how do I override the textbox in question? Or will it automatically override all textboxes in my App (not bothered if it does)

..These are the fundamentals of programming I haven't come across yet, so bear with me as I'm again a Noob :D

 
i just made a project with a multiline textbox and set it's readonly property to true and the scrollbars work fine, here's the code:
VB.NET:
TextBox1.Text = "1" & ControlChars.NewLine & "2" & ControlChars.NewLine & "3" & ControlChars.NewLine & "4" & ControlChars.NewLine & "5"
TextBox1.ScrollBars = ScrollBars.Vertical
TextBox1.ReadOnly = True
 
JuggaloBrotha said:
i just made a project with a multiline textbox and set it's readonly property to true and the scrollbars work fine, here's the code:
VB.NET:
TextBox1.Text = "1" & ControlChars.NewLine & "2" & ControlChars.NewLine & "3" & ControlChars.NewLine & "4" & ControlChars.NewLine & "5"
TextBox1.ScrollBars = ScrollBars.Vertical
TextBox1.ReadOnly = True

yes but he doesn't want the user be able to select the text. And with the code above you can still select it. Right?
V-B_New-B> try this
VB.NET:
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overloads [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] WndProc([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Message)
[/SIZE][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] (m.Msg = &H202) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]   [COLOR=darkgreen] '{...}[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff] End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
or you can declare the variable WM_LBUTTONDOWN as integer and use it for Comparasion.
VB.NET:
[SIZE=2][COLOR=#0000ff]Const[SIZE=2][COLOR=#000000] WM_LBUTTONDOWN [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2][COLOR=#000000] = &H202[/COLOR][/SIZE][/COLOR][/SIZE]

Regards ;)
 
Hey,

Yes you are correct I don't want any of the text to be selected - I want the textbox to act as though it is disabled. However I do want the vertical scrollbar to still be used.

Kulrom, please bear in mind I'm new to this overriding and overloading side of programming :)

I've tried your code, but what replaces '(...) ?? I tried using

VB.NET:
[LEFT][SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overloads [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] WndProc([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Message)
[/SIZE][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] (m.Msg = &H202) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  [COLOR=#0000ff]Return
[SIZE=2]End[/SIZE][/COLOR][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].WndProc(m)
[/SIZE][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[COLOR=#0000ff]
[/COLOR]

but that didn't work :(

Thanks!
Luke[/LEFT]
 
from the sounds of it, you should make your own control for this

i would suggest using a label on a panel with a vertical scrollbar
 
HI Check this i have did it in VB.Net..

'Put this in a class...
VB.NET:
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox
'const int WM_LBUTTONDOWN = 0x0201; 
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
'If m.Msg = WM_LBUTTONDOWN Then Exit Sub
If m.WParam.ToString = 4917572 Then Exit Sub
MyBase.WndProc(m)
End Sub
End Class


Add a form to your application and add a Button, name it Button1

paste this code..

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mt As New MyTextBox
mt.Left = 0
mt.Top = 0
mt.Size =
New Size(50, 100)
mt.Multiline =
True
mt.Show()
Me.Controls.Add(mt)

mt.Text = "asd asdjh asdlh kjshf shdfljsah dhslkdfh ksahdf hsadl flkshdflkh sad hflksfdlkhslkdf lksdfl hdhfals dhhasdl lkjsdhflkhsdlkfhlksdhf lhsadf hdhf lsah jasdhlkhsdfh fd asd asdjh asdlh kjshf shdfljsah dhslkdfh ksahdf hsadl flkshdflkh sad hflksfdlkhslkdf lksdfl hdhfals dhhasdl lkjsdhflkhsdlkfhlksdhf lhsadf hdhf lsah jasdhlkhsdfh fd "

mt.ScrollBars = ScrollBars.Vertical
mt.BackColor = Color.Gray

End Sub

and by the way How we can convert this code to VB.Net, Anybody knows??
'const int WM_LBUTTONDOWN = 0x0201;

Bye
 

Latest posts

Back
Top