|
-
Jul 29th, 2009, 04:43 AM
#1
Thread Starter
Lively Member
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. ?
-
Jul 29th, 2009, 04:55 AM
#2
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.
-
Jul 29th, 2009, 05:07 AM
#3
Thread Starter
Lively Member
Re: write a property for textbox class .
 Originally Posted by jmcilhinney
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 .
 Originally Posted by jmcilhinney
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.
-
Jul 29th, 2009, 05:55 AM
#4
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
-
Jul 29th, 2009, 06:13 AM
#5
New Member
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
-
Jul 30th, 2009, 02:28 AM
#6
Thread Starter
Lively Member
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.
-
Jul 30th, 2009, 03:17 AM
#7
Re: write a property for textbox class .
They must be TextBoxEx and not TextBox
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 30th, 2009, 03:54 AM
#8
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:
Private TextBox1 As System.Windows.Forms.TextBox
becomes:
vb.net Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|