-
Sep 1st, 2014, 06:56 AM
#1
Thread Starter
Addicted Member
Dynamic Creation of Multiple Events for Dynamically Created Control [RESOLVED]
Hi again everyone.
I'm having a really annoying problem here... I've created a textbox dinamically and I'm trying to add more than one event to it.
As of now I have the TextChanged event working well and I'm trying to add the KeyPress event, but I don't understand what the hell is happening, because the event isn't even fired on debug...
Summary: I have a feature on my app, where I can change between themes, pretty much like when you change Windows' Themes on Desktop Customization Properties.
I have currently only the Default VB.Net theme and a custom theme, named Reactor Theme. This pretty much means, that everytime I change themes I need to remove the controls that were associated to the previous theme and create them again, but with the new theme, hence why I need to create controls and their respective events dynamically.
This is the following code I have to the dynamic control creation and his associated event handlers:
vb.net Code:
Me.txtBirthDate = New WindowsApplication4.ReactorTextBox()
Me.Controls.Remove(txtBirthDate)
Me.txtBirthDate = Nothing
Me.txtBirthDate = New WindowsApplication4.ReactorTextBox()
Me.txtBirthDate.Location = New System.Drawing.Point(127, 38)
Me.txtBirthDate.Name = "txtBirthDate"
Me.txtBirthDate.Size = New System.Drawing.Size(99, 20)
Me.txtBirthDate.TabIndex = 14
AddHandler CType(txtBirthDate, WindowsApplication4.ReactorTextBox).TextChanged, AddressOf txtBirthDate_TextChanged
AddHandler CType(txtBirthDate, WindowsApplication4.ReactorTextBox).KeyPress, AddressOf txtBirthDate_KeyPress
Me.ReactorTheme1.Controls.Add(Me.txtBirthDate)
This is the code I have for the handlers themselves:
vb.net Code:
Private Sub txtBirthDate_TextChanged(sender As Object, e As EventArgs)
Try
If txtName.Text <> String.Empty And txtBirthDate.Text <> String.Empty And txtNationality.Text <> String.Empty _
And cbGender.SelectedIndex > -1 And txtAddress.Text <> String.Empty And txtZipCode.Text <> String.Empty _
And txtPhoneNumber.Text <> String.Empty And txtEmail.Text <> String.Empty And cbDepartment.SelectedIndex > -1 _
And cbAccount.SelectedIndex > -1 And cbEmployeeType.SelectedIndex > -1 Then
btnClear.Enabled = True
btnRegister.Enabled = True
Else
btnClear.Enabled = False
btnRegister.Enabled = False
End If
Catch ex As Exception
End Try
Try
If txtBirthDate.Text.Length >= 3 Then
Dim s As String = txtBirthDate.Text.Replace("-", "")
For x As Integer = 2 To s.Length Step 2
s = s.Insert(x + ((x \ 2) - 1), "-")
If x >= 4 Then
Exit For
End If
Next
s = s.TrimEnd("-"c)
txtBirthDate.Text = s
txtBirthDate.SelectionStart = s.Length
End If
Catch ex As Exception
End Try
Try
If CType(txtBirthDate, ReactorTextBox).Text.Length > 10 Then
CType(txtBirthDate, ReactorTextBox).Text = CType(txtBirthDate, ReactorTextBox).Text.Remove(CType(txtBirthDate, ReactorTextBox).Text.Length - 1, 1)
End If
Catch ex As Exception
End Try
End Sub
Private Sub txtBirthDate_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
MsgBox("FOOBAR")
End Sub
This bit of code:
vb.net Code:
If CType(txtBirthDate, ReactorTextBox).Text.Length > 10 Then
CType(txtBirthDate, ReactorTextBox).Text = CType(txtBirthDate, ReactorTextBox).Text.Remove(CType(txtBirthDate, ReactorTextBox).Text.Length - 1, 1)
End If
is what I want to take out, because it's unnecessary, when I have a freaking KeyPress event where I only need to point out, that everytime there's 10 characters on the box, I simply forbid the user from typing anything else like so:
vb.net Code:
If txtBirthDate.TextLength >= 10 Then
e.Handled = True
End If
Where's the problem? Well... the code that holds the KeyPress Event is where the problem pretty much is. When I debug the application, with a BreakPoint on the KeyPress event, this event is ignored?!?!
I know for certain I have the code for the Handlers well designed, I just can't figure out what the hell is missing and there's nothing to point me out a solution, not even a hint.
Anyone sees at all what am I missing here?
Last edited by Simbiose; Sep 2nd, 2014 at 09:05 AM.
-
Sep 1st, 2014, 10:06 AM
#2
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
I noticed that you're using both Me.txtBirthDate and txtBirthDate interchangeably in the method where you're wiring up the event handlers. Your AddHandler statements are referencing txtBirthDate and if you have such a variable declared locally then its actually going to be wiring up events for that one which you disposed of earlier and not the new one you've created which you've used Me.txtBirthDate to reference.
So the real question is: Do you have a local variable named txtBirthDate in that method ?
-
Sep 1st, 2014, 10:27 AM
#3
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Originally Posted by Niya
I noticed that you're using both Me.txtBirthDate and txtBirthDate interchangeably in the method where you're wiring up the event handlers. Your AddHandler statements are referencing txtBirthDate and if you have such a variable declared locally then its actually going to be wiring up events for that one which you disposed of earlier and not the new one you've created which you've used Me.txtBirthDate to reference.
So the real question is: Do you have a local variable named txtBirthDate in that method ?
Thanks for calling that into my atention, it's things like these that easilly missed after so many lines of code x.x
Nope, "txtBirthDate" is actually a textbox object, which I defined such a name for, so this variable is acessible from anywhere as long as I know how to call it.
However, I always use the "Me" identifier in order to better help me where I'm standing at the moment, because I have several forms that depend on each other.
Still, even though I added those missing identifiers, the problem isn't coming from there :s
Edit* There's also another thing I've noticed. I read on a few forums that people could only get the first of many multiple handlers to be consumed, which led me to think that's what was happening to me, but since that not even the debugger was caring for that handler, it was confusing me.
To make sure, I commented the TextChanged Event and left only the KeyPress Event to be checked and to my surprise the event was still ignored?!
I did already check the KeyPreview property, in case anyone's wondering lol and it is set to True, I have no idea what is going on...
Last edited by Simbiose; Sep 1st, 2014 at 10:32 AM.
-
Sep 1st, 2014, 10:32 AM
#4
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
One thing I'd point out is that the code may not work even if the events were working right. It'll work fine if the user is typing into the textbox, but copy/paste may not trigger the events anyways. That's something to look at, but clearly isn't related to the issue.
The textbox in question is a user control, evidently. What are you doing with the keypress in the user control (as opposed to the application that is making use of the user control)?
My usual boring signature: Nothing
-
Sep 1st, 2014, 10:42 AM
#5
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Originally Posted by Shaggy Hiker
One thing I'd point out is that the code may not work even if the events were working right. It'll work fine if the user is typing into the textbox, but copy/paste may not trigger the events anyways. That's something to look at, but clearly isn't related to the issue.
The textbox in question is a user control, evidently. What are you doing with the keypress in the user control (as opposed to the application that is making use of the user control)?
Since it's a textbox designed for birthdate purposes only, I've planned that textboxes like these will have a maximum of 10 characters and the following format:
##-##-####
I've programmed the TextChanged Event, so that when the user presses the 3rd and 5th number dashes will be written automatically, look below:
## (User types 3rd number) => ##-#
##-## (User types 5th number) => ##-##-#
When the textbox reaches 10 characters, the KeyPress Event will and ONLY will handle any key that is currently being pressed. Nothing else will be done by the event, except sending a message to the application informing that the press of the key was handled.
-
Sep 1st, 2014, 10:57 AM
#6
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Ok, an auto-formatting textbox. But what happens on a copy/paste? You get the Textchanged event, but not the KeyPress Event...I think.
Still, that's beside the point. You may want to post the code modified after the changes you made earlier. I think I know what changes you say you made, but it's a bit distracting to look back at that initial snippet and see the odd stuff with the Me. versus not Me. variables. There's some stuff there that could cause trouble, but it should cause trouble for BOTH events, not just one. It sounds like the TextChanged event is firing as you expect, but not the KeyPress event.
By the way, is any of that code found in the Load event for the form?
My usual boring signature: Nothing
-
Sep 1st, 2014, 11:57 AM
#7
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Originally Posted by Shaggy Hiker
Ok, an auto-formatting textbox. But what happens on a copy/paste? You get the Textchanged event, but not the KeyPress Event...I think.
Still, that's beside the point. You may want to post the code modified after the changes you made earlier. I think I know what changes you say you made, but it's a bit distracting to look back at that initial snippet and see the odd stuff with the Me. versus not Me. variables. There's some stuff there that could cause trouble, but it should cause trouble for BOTH events, not just one. It sounds like the TextChanged event is firing as you expect, but not the KeyPress event.
By the way, is any of that code found in the Load event for the form?
Let's get things clearer, at least I hope so lol.
If the user Copy & Pastes I had already thought about it, but still not worrying about it, as I'm going through everything carefully, but to ease your mind here's what I'll do. I'm gonna add a boolean flag which will check if a user copies any text to the clipboard. If the flag is triggered (i.e. the user copies hence the flag is set to true) then the customization on the TextChanged Event will be disregarded and when the author Pastes the data into the textbox the flag will be reset. I've done this a million times for many things and I'm pretty sure it'll work for this as well.
Now, about the KeyPress Event that isn't firing, I'll show all the code that plays a part on it. I hope it's not too confusing, here goes:
My Form Load Event code
vb.net Code:
Private Sub fEmployeeRegistry_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try If My.Settings.ThemeModeChosen = 0 Then Me.txtBirthDate = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'txtBirthDate ' Me.txtBirthDate.Location = New System.Drawing.Point(127, 38) Me.txtBirthDate.Name = "txtBirthDate" Me.txtBirthDate.Size = New System.Drawing.Size(99, 20) Me.txtBirthDate.TabIndex = 14 Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(512, 254) Me.Controls.Add(Me.txtBirthDate) Me.ResumeLayout(False) Me.PerformLayout() ElseIf My.Settings.ThemeModeChosen = 1 Then ElseIf My.Settings.ThemeModeChosen = 2 Then Me.txtBirthDate = New WindowsApplication4.ReactorTextBox() Me.SuspendLayout() ' 'txtBirthDate ' Me.Controls.Remove(Me.txtBirthDate) Me.txtBirthDate = Nothing Me.txtBirthDate = New WindowsApplication4.ReactorTextBox() Me.txtBirthDate.Location = New System.Drawing.Point(127, 38) Me.txtBirthDate.Name = "txtBirthDate" Me.txtBirthDate.Size = New System.Drawing.Size(99, 20) Me.txtBirthDate.TabIndex = 14 Me.Controls.Remove(Me.ReactorTheme1) Me.ReactorTheme1 = Nothing Me.ReactorTheme1 = New WindowsApplication4.ReactorTheme() AddHandler CType(Me.txtBirthDate, WindowsApplication4.ReactorTextBox).TextChanged, AddressOf txtBirthDate_TextChanged AddHandler CType(Me.txtBirthDate, WindowsApplication4.ReactorTextBox).KeyPress, AddressOf txtBirthDate_KeyPress Me.ReactorTheme1.Controls.Add(Me.txtBirthDate) Me.ReactorTheme1.Dock = System.Windows.Forms.DockStyle.Fill Me.ReactorTheme1.Font = New System.Drawing.Font("Verdana", 6.75!) Me.ReactorTheme1.Location = New System.Drawing.Point(0, 0) Me.ReactorTheme1.Name = "fEmployeeRegistry" Me.ReactorTheme1.Text = "fEmployeeRegistry" Me.Size = New System.Drawing.Size(512, 254) Me.ReactorTheme1.Size = Me.Size Me.Controls.Add(Me.ReactorTheme1) Me.ResumeLayout(False) Me.PerformLayout() End If txtBirthDate.MaxLength = 10 txtZipCode.MaxLength = 8 Catch ex As Exception End Try
Note: I just left the code assigned to the txtBirthDate Textbox control. I have many more controls on the Load Event, but no point showing them here for this case.
My TextChanged Event for the txtBirthDate Textbox control
vb.net Code:
Private Sub txtBirthDate_TextChanged(sender As Object, e As EventArgs) Try If txtName.Text <> String.Empty And txtBirthDate.Text <> String.Empty And txtNationality.Text <> String.Empty _ And cbGender.SelectedIndex > -1 And txtAddress.Text <> String.Empty And txtZipCode.Text <> String.Empty _ And txtPhoneNumber.Text <> String.Empty And txtEmail.Text <> String.Empty And cbDepartment.SelectedIndex > -1 _ And cbAccount.SelectedIndex > -1 And cbEmployeeType.SelectedIndex > -1 Then btnClear.Enabled = True btnRegister.Enabled = True Else btnClear.Enabled = False btnRegister.Enabled = False End If Catch ex As Exception End Try Try If txtBirthDate.Text.Length >= 3 Then Dim s As String = txtBirthDate.Text.Replace("-", "") For x As Integer = 2 To s.Length Step 2 s = s.Insert(x + ((x \ 2) - 1), "-") If x >= 4 Then Exit For End If Next s = s.TrimEnd("-"c) txtBirthDate.Text = s txtBirthDate.SelectionStart = s.Length End If Catch ex As Exception End Try Try 'If CType(Me.txtBirthDate, ReactorTextBox).Text.Length > 10 Then ' CType(Me.txtBirthDate, ReactorTextBox).Text = CType(Me.txtBirthDate, ReactorTextBox).Text.Remove(CType(Me.txtBirthDate, ReactorTextBox).Text.Length - 1, 1) 'End If Catch ex As Exception End Try
My KeyPress Event for txtBirthDate Textbox control
vb.net Code:
Private Sub txtBirthDate_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) MsgBox("") End Sub
-
Sep 1st, 2014, 12:47 PM
#8
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
So, it IS in the Load event. Is that Catch block truly empty? That could be your problem. As you may or may not know, on an increasing number of OS (64-bit, only, I believe), an exception thrown in the Load event will simply be swallowed without any issue...other than that the Load event will appear to exit at that point. By catching exceptions, you'd be getting around that, except that by having an empty catch block you would be swallowing the exceptions. Therefore, if an exception occured on the AddHandler line you'd never know. There wouldn't be any result other than that the event handler wouldn't be hooked up. Still, about the only exception that would be possible would be if the event handler signature was wrong, but it doesn't look wrong.
Still, if you really do have an empty Catch block in the Load event...don't do that. Put something in there, even if it is just a messagebox showing the exception. You may be getting an exception that you aren't seeing, and the message associated with the exception would be VERY informative, in this case.
My usual boring signature: Nothing
-
Sep 1st, 2014, 03:42 PM
#9
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Wait a second, when your ThemeModeChosen is 0 you don't wire up any event handlers, yet you're still creating a TextBox. Why did you do that ?
-
Sep 1st, 2014, 03:48 PM
#10
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
For that matter, the event handlers are only wired up for ThemeModeChosen = 2, as the ThemeModeChosen = 1 is empty.
My usual boring signature: Nothing
-
Sep 2nd, 2014, 03:30 AM
#11
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Thank you for the replies.
On every code block I always start with a Try Catch and one of my objectives at the beggining on my project was to write the exceptions only after I was done with every basic function. However, as my project grew and more ideas were coming to my mind, the problems started to get more and more complex and I had to turn into writting up the Catch blocks in order to solve issues I was having. So, this led to having several Catch blocks without any code and this was on of them lol....
Once again thanks for pointing me that out. Still, this leaves me a question.
Even though the Catch block is empty and if there was any error on the AddHandler statement, shouldn't the debugger jump from that very statement to the Catch block?
I've seen that happen many times on Catch blocks that don't have any code and I was able to check the exception, by hovering the mouse over the found error.
Anyway, I'm going to write up the code to catch any exceptions and see what that'll lead me to.
Regarding the ThemeModeChosen variable, when it's value is 0, I haven't set up any handlers, because I'm not testing anything for when the variable has that value, yet.
So, basically, I'm just debugging the handlers for the code block where the variable ThemeModeChosen has its value set to 2.
This variable is set at an application global level, more specifically on the Settings tab of the application properties. When the user launches the application, the application itself will check which theme is stored on its setting, through this very variable, so in this case, when I launch the application, I always have the variable set to 2, so that I can debug the handlers.
Edit* Well, I've set the catch block code and no exception was triggered. It appears that the handle is being added, but then it's ignored...
Edit** Huhh.... I just moved on into another form and decided to try out theme changing. On the textboxes of that form, the TextChanged Events are triggered, but not the KeyPress Events.
Then I decided to check the KeyPress Event, but on a ComboBox control that I also have on that form and the event fires! Soooo... hummm?! What is going on here?!
The code I used on that form is exactly the same.... I'm going insane...
Last edited by Simbiose; Sep 2nd, 2014 at 05:52 AM.
-
Sep 2nd, 2014, 06:55 AM
#12
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
At this point I'd say that the problem is in that ReactorTextbox class. Is that a third party control ? Or is it something you made yourself ?
-
Sep 2nd, 2014, 07:06 AM
#13
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
Originally Posted by Niya
At this point I'd say that the problem is in that ReactorTextbox class. Is that a third party control ? Or is it something you made yourself ?
Well... I was afraid you'd say that and I have been checking that ReactorTheme class in its whole...
It's a third party class, holds several controls and I have been adjusting the class code to my needs. Apparently I'm missing something on the class itself that blocks or ignores the usage of event like KeyPress.
Here's the code relevant to the TextBox Control
vb.net Code:
Public Class ReactorTextBox : Inherits Control Dim WithEvents txtbox As New TextBox #Region " Control Help - Properties & Flicker Control " Private _passmask As Boolean = False Public Property UsePasswordMask() As Boolean Get Return _passmask End Get Set(ByVal v As Boolean) _passmask = v Invalidate() End Set End Property Private _maxchars As Integer = 32767 Public Shadows Property MaxLength() As Integer Get Return _maxchars End Get Set(ByVal v As Integer) _maxchars = v Invalidate() End Set End Property Private _align As HorizontalAlignment Public Shadows Property TextAlign() As HorizontalAlignment Get Return _align End Get Set(ByVal v As HorizontalAlignment) _align = v Invalidate() End Set End Property Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs) End Sub 'Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) ' MyBase.OnTextChanged(e) ' 'Invalidate() 'End Sub Protected Overrides Sub OnBackColorChanged(ByVal e As System.EventArgs) MyBase.OnBackColorChanged(e) txtbox.BackColor = BackColor Invalidate() End Sub Protected Overrides Sub OnForeColorChanged(ByVal e As System.EventArgs) MyBase.OnForeColorChanged(e) txtbox.ForeColor = ForeColor Invalidate() End Sub Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs) MyBase.OnFontChanged(e) txtbox.Font = Font End Sub Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs) MyBase.OnGotFocus(e) txtbox.Focus() End Sub Private _selectedPoint As Integer = 0 Public Shadows Property SelectionStart() As Integer Get Return _selectedPoint End Get Set(ByVal v As Integer) _selectedPoint = v End Set End Property Private _selectedLength As Integer = 0 Public Shadows Property SelectionLength() As Integer Get Return _selectedLength End Get Set(ByVal v As Integer) _selectedLength = v End Set End Property Sub TextChngTxtBox() Handles txtbox.TextChanged Text = txtbox.Text txtbox.Select(txtbox.TextLength, 0) End Sub Sub TextChng() Handles MyBase.TextChanged txtbox.Text = Text txtbox.Select(txtbox.TextLength, 0) End Sub Sub NewTextBox() With txtbox .Multiline = False .BackColor = BackColor .ForeColor = ForeColor .Text = String.Empty .TextAlign = HorizontalAlignment.Center .BorderStyle = BorderStyle.None .Location = New Point(5, 5) .Font = New Font("Verdana", 7.25) .Size = New Size(Width - 10, Height - 11) .UseSystemPasswordChar = UsePasswordMask .MaxLength = MaxLength End With End Sub #End Region Sub New() MyBase.New() NewTextBox() Controls.Add(txtbox) Text = "" BackColor = Color.FromArgb(37, 37, 37) ForeColor = Color.White Size = New Size(135, 35) DoubleBuffered = True End Sub Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim G As Graphics = e.Graphics MyBase.OnPaint(e) Height = txtbox.Height + 11 With txtbox .Width = Width - 10 .TextAlign = TextAlign .UseSystemPasswordChar = UsePasswordMask End With G.Clear(BackColor) G.FillRectangle(New SolidBrush(Color.FromArgb(37, 37, 37)), New Rectangle(1, 1, Width - 2, Height - 2)) G.DrawRectangle((New Pen(New SolidBrush(Color.Black))), New Rectangle(1, 1, Width - 3, Height - 3)) G.DrawRectangle((New Pen(New SolidBrush(Color.FromArgb(70, 70, 70)))), New Rectangle(0, 0, Width - 1, Height - 1)) G.DrawLine(New Pen(New SolidBrush(Color.FromArgb(45, 45, 45))), 0, 0, Width, 0) G.DrawLine(New Pen(New SolidBrush(Color.FromArgb(45, 45, 45))), 0, 0, 0, Height) G.DrawLine(New Pen(New SolidBrush(Color.FromArgb(45, 45, 45))), Width - 1, 0, Width - 1, Height) G.DrawLine(New Pen(New SolidBrush(Color.FromArgb(31, 31, 31))), 2, 2, Width - 3, 2) End Sub End Class
I still haven't had any luck so far, still only started checking if the problem was coming from the class itself since yesterday ...
Edit* Well, from what I'm understanding I think I have to instantiate the KeyPress event manually.
I debugged the class code and everytime a textbox is instantiated on the form that holds that very same textbox, the variable txtbox holds the same control that was instantiated and then that variable is also instantiated, but on the class itself, so what happens here is, since the object was created on the form load event, but has no events attached to it and the object is controlled by the class it appears that I have to create the events within the class.
For instance, we have the TextChngTxtBox() and TextChng() handle the TextChanged event, where the former handles every textbox that is instantiated on the whole project and the latter handles the object that is called on at a certain point (sub, function, etc) on runtime.
I'm not sure this is where the problem lies, but the only way to make sure is for me to create the KeyPress event manually on the class and check if this is where the problem lies, but I have no clue on how to do that.
Last edited by Simbiose; Sep 2nd, 2014 at 08:21 AM.
-
Sep 2nd, 2014, 09:03 AM
#14
Thread Starter
Addicted Member
Re: Dynamic Creation of Multiple Events for Dynamically Created Control
YES! I finally managed to solve the damn problem!
I added the KeyPress Event on the ReactorTheme Class itself, like so:
vb.net Code:
Sub KeyPresTxtBox(sender As Object, e As KeyPressEventArgs) Handles txtbox.KeyPress If NumberOfHifens = 1 Then If txtbox.TextLength >= 8 Then If Not e.KeyChar = Chr(Keys.Back) Then e.Handled = True End If End If ElseIf NumberOfHifens = 2 Then If txtbox.TextLength >= 10 Then If Not e.KeyChar = Chr(Keys.Back) Then e.Handled = True End If End If End If End Sub
And I had to add this little bit of code on the TextChng() procedure of the ReactorTheme class:
vb.net Code:
NumberOfHifens = 0 For i As Integer = 0 To txtbox.Text.Length - 1 If txtbox.Text(i) = "-" Then NumberOfHifens += 1 End If Next
Although I solved the problem, I'm still unaware why the KeyPress Event is only triggered on the class, while the TextChanged Event is triggered in both the form load Event and the class. I'd be really appreciated if someone could explain this to me, because after I've completed my project, I'm gonna lay out every detail, explaining how I did what I did. I'm planning on releasing a website soon explaining the projects I build, so this would be a good remark.
Edit* I've just realized that the code I had before, was applied to every textbox, because I can't access the KeyPress of a specified Textbox control, precisely because the debugger ignores it (so in a way the problem's still there... and I seriously hope it won't cause further problems in the future...). So I had to write my code on the Event that applies to every textbox Control and in order to differentiate which textboxes I want to have certain formats, all I needed to do was to insert a couple of restrictions on the TextChanged Event at the ThemeReactor class itself, as you can see in the code posted above.
Last edited by Simbiose; Sep 3rd, 2014 at 04:12 AM.
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
|