Events for Custom Associated Control

已答覆 Events for Custom Associated Control

  • 2012年8月3日 上午 07:22
     
     

    Hello Friends,
    I am here with another problem

    I have created a custom textbox which is inheritated from textbox
    i declared a new property called AssociatedLabel
    In gotfocus Event I change the color (BackColor and Forecolor) of the associatedLabel
    as well as in the lostfocus

    e.g. TextBox1 is associated with label1
    TextBox2 is associated with label2
    Textbox3 is also associated with Label2

    Now I want to write some code in some Events for the associatedLabel

    How can I access those events

    Any help will be appreciated.

所有回覆

  • 2012年8月3日 上午 07:37
     
     

    First of all what does trigger the event?


    Success
    Cor

  • 2012年8月3日 上午 07:46
     
     

    Hi

    Thanks for reply

    If AssociatedLabel is clicked then textbox(with which the label is associated) should be focused

  • 2012年8月3日 上午 08:01
     
     

    Hello Vishal,

                      Here you have to catch label event and then operate to textbox.

    So flow is from lable ---> Textbox  and not from Textbox--> lable.

    So better handle events of the lable and at that time change focus from your associated label to textbox.

      By the way why are u passing whole control as associated control do u have some more functionality in it (in textbox ) for that control ?


    Want to add MVP with my name.

  • 2012年8月3日 上午 08:14
     
     

    So better handle events of the lable and at that time change focus from your associated label to textbox.

      By the way why are u passing whole control as associated control do u have some more functionality in it (in textbox ) for that control ?


    Want to add MVP with my name.

    Ya I want to handle the events for associatedLabel in the TextBox class that i have created But How?

    And Yes I have many other functionalities in it


    • 已編輯 Vishal1419 2012年8月3日 上午 08:18
    • 已編輯 Vishal1419 2012年8月3日 下午 03:44
    •  
  • 2012年8月3日 下午 03:49
     
     
     By the way why are u passing whole control as associated control

    If I cant do it with label then its OK as it is not so Important

    But I have another property AssociatedListBox also and I have to Handle the events of listbox

  • 2012年8月3日 下午 04:05
     
     

    In the Set method of the AssociatedLabel property, use AddHandler to attach event handlers to the label's events, e.g.

          AddHandler value.click, addressof OnLabelClick


    Armin

  • 2012年8月4日 上午 05:51
     
      包含代碼

    In the Set method of the AssociatedLabel property, use AddHandler to attach event handlers to the label's events, e.g.

          AddHandler value.click, addressof OnLabelClick


    Armin

    I tried this on a listbox

    tis is what I have tried

    AddHandler value.KeyPress, AddressOf Key_Press
    
    Private Sub Key_Press(ByVal sender As TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim x As TextBox = DirectCast(sender, TextBox)
            Call lstBox_KeyPress(x, Associated_LstBox, e)
    End Sub

    I get the following runtime error

    Unable to cast object of type 'System.Windows.Forms.ListBox' to type 'System.Windows.Forms.TextBox'.

    I have also tried it without directCast but same result


    • 已編輯 Vishal1419 2012年8月4日 上午 05:54
    • 已編輯 Vishal1419 2012年8月4日 上午 07:38
    • 已編輯 Vishal1419 2012年8月4日 上午 10:12
    •  
  • 2012年8月4日 上午 09:30
     
      包含代碼

    If AssociatedLabel is clicked then textbox(with which the label is associated) should be focused

    That would need the label to have an associated textbox.

    You could do that by setting the textbox as the Tag value of the label, and then adding code in the label click event to acces the Tag property and set the focus accordingly.

    The Property Set for the AssociatedLabel property of the custom text box could include code to se thte Tag property of the label to the text box where the property is being set. But you still need to add code to the label click event - I don't know of a way that the custom text box can control the event processing of the label.

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MyTextBox1.AssociatedLabel = Label1
        End Sub
    
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
            Dim T As TextBox = CType(sender.tag, TextBox)
            T.Focus()
        End Sub
    End Class
    
    Public Class myTextBox
        Inherits TextBox
        Private myLabel As Label
    
        Public Property AssociatedLabel() As Label
    
            Get
                Return myLabel
            End Get
            Set(ByVal value As Label)
                myLabel = value
                If Not myLabel Is Nothing Then
                    myLabel.Tag = Me
                End If
            End Set
        End Property
    
    End Class

  • 2012年8月4日 上午 10:10
     
     

    That would need the label to have an associated textbox.

    I have tried that before few days, At first sight it looks OK

    But then I got Problems as there are more textboxes associated with a single label in some forms

    Thanks for the kind reply

  • 2012年8月4日 上午 11:32
     
     已答覆 包含代碼

    In the Set method of the AssociatedLabel property, use AddHandler to attach event handlers to the label's events, e.g.

          AddHandler value.click, addressof OnLabelClick


    Armin

    I tried this on a listbox

    tis is what I have tried

    AddHandler value.KeyPress, AddressOf Key_Press
    
    Private Sub Key_Press(ByVal sender As TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim x As TextBox = DirectCast(sender, TextBox)
            Call lstBox_KeyPress(x, Associated_LstBox, e)
    End Sub

    I get the following runtime error

    Unable to cast object of type 'System.Windows.Forms.ListBox' to type 'System.Windows.Forms.TextBox'.

    I have also tried it without directCast but same result


    Why do you cast to Textbox as the associated control is a Label? The property is named AssociatedLabel, not AssociatedTextbox. The textbox is the class in which you have the code.

    In addition, why do you handle the KeyPress event as a label can not have the focus and consequently does not receive key events?

    And how is it possible to assign a Listbox to the AssociatedLabel property as the type of the AssociatedLabel property is Label?

    The code how I expect it to be...:

    Class MyTextBox
       Inherits TextBox
    
       Private _AssociatedLabel As Label
    
       Public Property AssociatedLabel() As Label
          Get
             Return _AssociatedLabel
          End Get
          Set(ByVal value As Label)
    
             _AssociatedLabel = value
    
             AddHandler _AssociatedLabel.Click, AddressOf OnLabelClick
    
          End Set
       End Property
       Private Sub OnLabelClick(ByVal sender As Object, ByVal e As EventArgs)
    
          Focus()
    
       End Sub
    End Class

    (reduced example without checks)


    Armin

    • 已標示為解答 Vishal1419 2012年8月4日 下午 01:03
    •  
  • 2012年8月4日 上午 11:40
     
      包含代碼

    The code how I expect it to be...:
    Class MyTextBox
       Inherits TextBox
    
       Private _AssociatedLabel As Label
    
       Public Property AssociatedLabel() As Label
          Get
             Return _AssociatedLabel
          End Get
          Set(ByVal value As Label)
    
             _AssociatedLabel = value
    
             AddHandler _AssociatedLabel.Click, AddressOf OnLabelClick
    
          End Set
       End Property
       Private Sub OnLabelClick(ByVal sender As Object, ByVal e As EventArgs)
    
          Focus()
    
       End Sub
    End Class

    (reduced example without checks)


    Armin

    I have done exactly that and got this runtime error

    I have not tried it with label but I tried with ListBox

    Unable to cast object of type 'System.Windows.Forms.ListBox' to type 'System.Windows.Forms.TextBox'.

    This error is on the Form1.Designer.VB

    On the first line (Public Class Form1)

  • 2012年8月4日 下午 12:10
     
     

    I have done exactly that and got this runtime error

    I have not tried it with label but I tried with ListBox

    Unable to cast object of type 'System.Windows.Forms.ListBox' to type 'System.Windows.Forms.TextBox'.

    This error is on the Form1.Designer.VB

    On the first line (Public Class Form1)

    If it is a runtime error, look at the exception details, mainly the stacktrace. Look at the top stack frame to see the location of the exception. Do the same for the InnerException property.

    Without knowing the failing line it's impossible to help.


    Armin

  • 2012年8月4日 下午 12:30
     
     

    I just tried with the label and it works fine

    I dont know why it gives me this error

    Anyway Here I paste the details of the stackTrace because I dont understand what is it trying to tell

       at AdvancedTextBox.MyTxtBox._Lambda$__1(Object a0, KeyPressEventArgs a1) in E:\Collections\VB.NET\Custom Controls\AdvancedTextBox\AdvancedTextBox\MyTxtBox.vb:line 216    at System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)    at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)    at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)    at System.Windows.Forms.Control.WmKeyChar(Message& m)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ListBox.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(ApplicationContext context)    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)    at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()

  • 2012年8月4日 下午 12:48
     
     

    Please post line 216 of file MyTxtBox.vb

    Also post your code of the AssociatedLabel property.


    Armin

  • 2012年8月4日 下午 01:03
     
     

    Ya, I got it

    I passed wrong arguments in the sub

    I passed sender as Textbox instead of sender as object

    Thank you for helping

  • 2012年8月4日 下午 01:07
     
     

    But then I got Problems as there are more textboxes associated with a single label in some forms

    That would work only only if the code executing in the label click did not need to refer to any particular textbox.  But you have indicated that "If AssociatedLabel is clicked then textbox(with which the label is associated) should be focused".  That cannot work if you have more than one textbox associated with a single label.

  • 2012年8月4日 下午 01:12
     
     

    Ya, I got it

    I passed wrong arguments in the sub

    I passed sender as Textbox instead of sender as object

    Thank you for helping

    I have no clue what you are doing. You don't have to call an event handler manually. Changing to type object even increases the possibility of passing a wrong type.

    Nice it works for you, but I'm really interested in which code you have now.


    Armin