[RESOLVED] How to make textbox 'gray-out'?-VBForums
Results 1 to 20 of 20

Thread: [RESOLVED] How to make textbox 'gray-out'?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Resolved [RESOLVED] How to make textbox 'gray-out'?

    How to make a text box, not able to let users write anything on it?

    Where the box will appears to be grey-out...?

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: How to make textbox 'gray-out'?

    TextBox1.Enabled = False

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to make textbox 'gray-out'?

    hmm. but mine still enables user to write it in. Not able to gray-out.
    I'm not sure if my other codings affect it too but I just post it in here

    vb Code:
    1. Private Sub cboCost_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCost.SelectedIndexChanged
    2.         cboCost.SelectedIndex = 0
    3.         cboCost.Enabled = False
    4.  
    5.     End Sub
    6.  
    7.     Private Sub cboSource_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSource.SelectedIndexChanged
    8.  
    9.         If cboSource.Enabled = False Then
    10.             cboSource.SelectedIndex = 0
    11.         ElseIf cboSource.Enabled = True Then
    12.             cboSource.Enabled = chkboxCost.Checked
    13.         End If
    14.  
    15.     End Sub
    16.  
    17.     Private Sub chkboxCost_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkboxCost.CheckedChanged
    18.         If cboSource.Enabled = False Then
    19.             cboSource.Enabled = chkboxCost.Checked
    20.         End If
    21.        
    22.     End Sub
    23.  
    24.     Private Sub txtCMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCMaterial.TextChanged
    25.         txtCMaterial.Enabled = False
    26.  
    27.     End Sub

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    I assume its the txtCMaterial control that you are trying to grey out? If so, why are you doing it in the TextChanged event? That event will fire when someone begins to type into the box... I am guessing you want it greyed out before then. If thats the case then just do it in the form load event or something like that, so that the box is already greyed out before the user can attempt to type in it. You could even do it in the designer (assuming it wants to be greyed out for the lifetime of your app)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by melvados
    hmm. but mine still enables user to write it in. Not able to gray-out.
    I'm not sure if my other codings affect it too but I just post it in here

    vb Code:
    1. Private Sub cboCost_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCost.SelectedIndexChanged
    2.         cboCost.SelectedIndex = 0
    3.         cboCost.Enabled = False
    4.  
    5.     End Sub
    6.  
    7.     Private Sub cboSource_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSource.SelectedIndexChanged
    8.  
    9.         If cboSource.Enabled = False Then
    10.             cboSource.SelectedIndex = 0
    11.         ElseIf cboSource.Enabled = True Then
    12.             cboSource.Enabled = chkboxCost.Checked
    13.         End If
    14.  
    15.     End Sub
    16.  
    17.     Private Sub chkboxCost_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkboxCost.CheckedChanged
    18.         If cboSource.Enabled = False Then
    19.             cboSource.Enabled = chkboxCost.Checked
    20.         End If
    21.        
    22.     End Sub
    23.  
    24.     Private Sub txtCMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCMaterial.TextChanged
    25.         txtCMaterial.Enabled = False
    26.  
    27.     End Sub
    All I'm going to say is, good god this is an app maintenance nightmare right here

  6. #6
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by melvados
    How to make a text box, not able to let users write anything on it?

    Where the box will appears to be grey-out...?
    If you want to disable a textbox (or any other control for that matter) do it on the property page.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  7. #7
    Junior Member
    Join Date
    Jul 2008
    Posts
    31

    Re: How to make textbox 'gray-out'?

    textbox.readonly= true

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by endri12
    textbox.readonly= true
    that wont "grey-out" the control, which is what the OP asked for
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Junior Member
    Join Date
    Jul 2008
    Posts
    31

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by chris128
    that wont "grey-out" the control, which is what the OP asked for
    what are you talking about?
    the op asked to make textbox not able to write anything and grayed out , doesn't readonly property do that?

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by endri12
    what are you talking about?
    the op asked to make textbox not able to write anything and grayed out , doesn't readonly property do that?
    Yes it does, if he wants it grayed out he can set the readonly property to True then change the backcolor to gray.

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by JuggaloBrotha
    Yes it does, if he wants it grayed out he can set the readonly property to True then change the backcolor to gray.
    Yeah but just setting Enabled to false does the exact same thing but in one line... I dont know if your just messing around or if your being serious but its blatantly obvious (to me anyway) that the OP was just refering to how controls look when they are not enabled when he said greyed out. Why change ReadOnly and the background colour when you can just type TextBox.Enabled = False :S
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    Re: How to make textbox 'gray-out'?

    Actually, there's a significant difference.... if the control is not enabled, it cannot receive focus.... the text cant's be highlighted (I think) and you cannot scroll the text box. Why would that matter? If I want a text box that has text in it that is larger than can be displayed, or may be needed else where (think copy-paste) .... I may not want the user to edit the info so, but I want then to be able to see the data, setting ReadOnly gives me that... but it doesn't give any indication that it is readonly.... so I have to provide some other visual cue that the data can't be edited.... enter the grey out.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: How to make textbox 'gray-out'?

    I think you're looking for something like this:
    Code:
    TextBox1.ReadOnly = True
    ========================================

    Sorry, my bad.
    Last edited by Louix; Aug 1st, 2008 at 01:47 PM.

  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by techgnome
    Actually, there's a significant difference.... if the control is not enabled, it cannot receive focus.... the text cant's be highlighted (I think) and you cannot scroll the text box. Why would that matter? If I want a text box that has text in it that is larger than can be displayed, or may be needed else where (think copy-paste) .... I may not want the user to edit the info so, but I want then to be able to see the data, setting ReadOnly gives me that... but it doesn't give any indication that it is readonly.... so I have to provide some other visual cue that the data can't be edited.... enter the grey out.

    -tg
    I appreciate that but the OP didnt mention anything like that, he just said he wanted to grey-out the box which to me always means setting textbox.Enabled to false.
    Also, the OP posted some code that he was using to set Enabled to False and said that it did not work so saying that he should try setting ReadOnly to False is not going to work either... because Enabled = False is only not working because he is using it in the wrong place or something similar.

    Anyway I'm going to shut up now because the OP has probably forgotten about this thread by now or has fixed it himself.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    Re: How to make textbox 'gray-out'?

    1) My comment had more to do with the "" comment of yours.... I was simply noting that SOMETIMES there's a reason to use ReadOnly AND change the background.
    2) The OP is after more than jsut greying out the text -> "not able to let users write anything on it?" Therefore it is not jsut a visual problem, but also a functional issue. In this case, If the OP has no interest in copying teh text, or scrolling or highlighting any portion of the text, then .Enabled *should* be the appropriate solution. I think it didn't work for the OP because of WHERE he was attempting to set the enabled property - in the Textchanged event - which doesn't fire until AFTER the user has tried to edit the text. Needs to be done before that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    No, he said the textbox would be greyed out, not just the text. Otherwise I would not of suggested it and yeah I already mentioned about the fact that he was doing it in the wrong place
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    Re: How to make textbox 'gray-out'?

    Just goes to show... ask 6 developers a question, you'll get 12 different answers... it's all in the interpretation.... You read it one way, I another....

    Here's another wrench, why use a text box at all? Labels.... labels work jsut as well too...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    19,269

    Re: How to make textbox 'gray-out'?

    setting textboxes as readonly automatically changes the backcolor to systemcolors.control

  19. #19
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,598

    Re: How to make textbox 'gray-out'?

    Quote Originally Posted by .paul.
    setting textboxes as readonly automatically changes the backcolor to systemcolors.control
    so it does! never knew that
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  20. #20
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: [RESOLVED] How to make textbox 'gray-out'?

    Although this marked resolved, user should also look to whether the textbox is locked. If it isn't, setting to enabled = false won't turn grey.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.