Results 1 to 8 of 8

Thread: write a property for textbox class .

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    66

    write a property for textbox class .

    I want to write a simple propety for textbox.

    I know a structure of property statement but i cant do exactly.

    example :

    I want create a property name selectedlength1. And i have two textboxs in my form and i want to assign :

    Textbox1.Selectedlength1 = 2
    Textbox1.Selectedlength1 = 3

    Then the length of string in textbox1 is selected is : 2 and 3 for textbox2.

    How can i do that. ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: write a property for textbox class .

    First up, I'm confused as to what you're actually trying to achieve, given that the TextBox class already has a SelectionLength property.

    Secondly, you can't add properties to existing classes from the outside. You would have to either define your own class that inherits TextBox and add a property to it, then use that class instead of the regular TextBox, or else you'd have to define an extension method that extends the TextBox class, which you can do if you're using .NET 3.5.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    66

    Re: write a property for textbox class .

    Quote Originally Posted by jmcilhinney View Post
    First up, I'm confused as to what you're actually trying to achieve, given that the TextBox class already has a SelectionLength property.
    .
    But i want to add condition example : if i click thirth character the textbox will select 2 characters.

    If i click to second character the textbox will select 3 characters .


    Quote Originally Posted by jmcilhinney View Post
    Secondly, you can't add properties to existing classes from the outside. You would have to either define your own class that inherits TextBox and add a property to it, then use that class instead of the regular TextBox, or else you'd have to define an extension method that extends the TextBox class, which you can do if you're using .NET 3.5.
    That s thing which i want. Please help me.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: write a property for textbox class .

    Code:
    Public Class TextBoxEx
        Inherits TextBox
    
        Private _autoSelectCharCount As Integer
    
        Public Property AutoSelectCharacterCount() As Integer
            Get
                Return Me._autoSelectCharCount
            End Get
            Set(ByVal value As Integer)
                If value < 0 Then
                    Throw New ArgumentException("Value must be non-negative.")
                End If
    
                Me._autoSelectCharCount = value
            End Set
        End Property
    
        Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
            MyBase.OnClick(e)
    
            Me.SelectionLength = Math.Min(Me._autoSelectCharCount, _
                                          Me.TextLength - Me.SelectionLength)
        End Sub
    
    End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    New Member
    Join Date
    Jul 2009
    Posts
    1

    Re: write a property for textbox class .

    It's nice to be here. I can learn a lot of things..

    taux emprunt
    immobilier
    - Taux emprunt immobilier.
    Comparez les offres d’emprunt immobilier, simulation emprunt
    immobilier, taux emprunt immobilier

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    66

    Re: write a property for textbox class .

    Sorry about stupid question. But i want to ask .
    I understood code you wrote. But i have two textbox in my form .How can i add _autoSelectCharCount property to them.

    Thanks.

  7. #7
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: write a property for textbox class .

    They must be TextBoxEx and not TextBox

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: write a property for textbox class .

    As mickey_pt said, you need to have TextBoxEx controls on your form rather than TextBox controls. Once you've added that class to your project and built it you'll find that it will be added to the top of the Toolbox automatically.

    That said, you don't necessarily have to delete your existing TextBoxes. You can edit the designer-generated code to make them TextBoxEx, but be careful to only change what you have to in that code because you can mess up your form badly. In the Solution Explorer, click the Show All Files button and then expand node for your form. Double-click the designer.vb node to open the designer code file. Find the declarations for the TextBoxes you want to "upgrade" and change their type to TextBoxEx, e.g.
    vb.net Code:
    1. Private TextBox1 As System.Windows.Forms.TextBox
    becomes:
    vb.net Code:
    1. Private TextBox1 As TextBoxEx
    Once that's done you'll find that there are now some errors further up the code, where the TextBoxes are created and assigned to those variables. Again, you need to change the type to TextBoxEx. That's it. You can now view your code in the designer and you'll find that your TextBox controls are now TextBoxEx controls and they have a new property in the Misc section.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width