Is it possible to make Textbox behave like label control

  • 14 years ago

    Hi

    I think by making the textbox disabling the focus to the textbox it possible to get this change, But HOW SHOULD I DO IT?

    any help

  • 14 years ago
    In what way/ways do you want it to behave like a label?  Do you just want it to not recieve focus through either tabbing or clicking?  Also, I have to ask but why not just use a label.  You can disable a textbox which will prevent it from ever recieving focus.  However, this will also grey it out.  I was able to figure out a workaround that would allow you to disable the textbox but not grey it out.  I'll post some code if you are interested.
  • 14 years ago

    Hi twofaced

    Like you said I wanted the textbox not to receive a focus through tabbing or clicking. I simply want it to display a value for sometime and change this on some conditions to modify its value.  yes making its enable property false will make it grey otherwise it is fine.

    I thought to create one label and one textbox control placed one on the other, so that they could function as i want by changing the visible property of them. To desiplay i use the label and to edit or modify i use the textbox. But i feel this method is not good if i have many such kind of combinations.

    So i am waiting for your idea.

  • 14 years ago
    From the sounds of it you want a textbox to normally act like a label but under some conditions be edited by the user.  This solution should work great for you.  What I did was create a class that inherits the TextBox class.  In every way it's just a textbox except for one, it doesn't go grey when disabled.
    Public Class TextBox_NoDisabledLook
        Inherits TextBox
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            'The message ID that tells the textbox to grey out when disabled
            Const WM_ENABLE As Integer = &HA
    
            'Exit if the message is WM_ENABLE, thus preventing the greyed out look
            If m.Msg = WM_ENABLE Then Exit Sub
            MyBase.WndProc(m)
        End Sub
    End Class

    To add this control to your application just create a new class and replace the code with the code above. The control should appear in the tools list under "MyControls Components" which should be at the top. You may need to run your program once before the control appears in the list (That's what I always need to do anyway). At this point you can drag and drop the control on your form just like any other.

    Now you can disable the textbox which will prevent it from getting focus but it will still have an enabled look. Just enable the textbox whenever you want to allow the user to edit it. I hope that helps with your problem.

  • 14 years ago

    Thanks it really helps. But after i coped and pasted the class, i couldn't able to use your way of adding the control. I did it by adding the class dll manually by going through ' add/remove item ' box from the tools menu. Then i got the control in the toolbox. After this i draged and droped it and made a test, and it worked as you said.

     Bye the way i am using VB.NET 2003. Does this have an effect to do your way of adding the control?

  • 14 years ago

    I've only used vb.net 2005 so I couldn't say if there are any differences between the two but it's entirely possible.  Glad you got it working.

  • 14 years ago
    The easy and the best way is that,

    1. Open a new project - > Standard Exe.
    2. Place a text box in the form window.
    3. If needed change the text box text property to the text that u need.
    4. Then change the appearance of the textbox to Flat from the properties window(If not present then right click the textbox and choose properties).
    5. Then change the backcolor property to Scroll bar or Menu bar... Now the text box changes to grey with the border line.
    6. Now change the border style property to None. And finally make the enabled property to False.

    Now ur text box looks like the label box......










Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray