?
Solved

Is ther an alternative to activecontrol

Posted on 2013-01-04
3
Medium Priority
?
234 Views
Last Modified: 2013-01-07
I am working with a form and I need to know what the active control is.  I am using AddHandler.  So far I am using something like this

AddHandler txtBox.GotFocus, AddressOf TextBox_GotFocus

then this
   Private Sub TextBox_GotFocus()
        _form.ActiveControl.BackColor = Highlight
    End Sub

I wrote a sample version outside of the main application and things went well.  The controls on the form are standard windows syncfusion, and some usercontrols.  All of these sit on an image panel divided up on a tabsheet.

I realise this is a bit of a soup in terms of form content, but that is what I have to work with.  I supect I might be able to use an alternative approach to ActiveControl but I am not aware of it.

As a foot note the _form is a by reference to the form because this code is in a class, the intent is to create a generic branding class to enable brand format to be applied over the whole application.  Also this is a winforms development.
0
Comment
Question by:Alyanto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Author Comment

by:Alyanto
ID: 38744037
The code to date

Imports System.Windows.Forms
Imports System.Drawing

Public Class cGenFormFormatter
    Private _form As Form
    Private ReadOnly Property Highlight As Color
        Get
            Return Color.FromArgb(0, 43, 85)
        End Get
    End Property
    Private Property ctrlBackColor As Color
    Private Property MouseOver As Color
    Private Property aControl As Control

    Public Sub New(ByRef Frm As Form)
        Dim icn As System.Drawing.Icon = My.Resources.form_default
        Frm.Icon = icn



        For Each ctrl As Control In Frm.Controls
            Debug.Print(ctrl.GetType().ToString)
            WhatControl(ctrl)

        Next
    End Sub

    Public Sub WhatControl(Ctrl As Control)
        Select Case Ctrl.GetType().BaseType.ToString
            Case "System.Windows.Forms.UserControl"
                UserCtrl(Ctrl)
            Case Else
                Select Case Ctrl.GetType().ToString
                    Case "Syncfusion.Windows.Forms.Tools.GradientPanel"
                        GradientPanel(Ctrl)
                    Case "Syncfusion.Windows.Forms.Tools.TabControlAdv"
                        TabControlAdv(Ctrl)
                    Case "Syncfusion.Windows.Forms.Tools.TextBoxExt"
                        syncTextBox(Ctrl)
                    Case "System.Windows.Forms.TextBox"
                        txtBox(Ctrl)
                    Case Else

                        Debug.Print(Ctrl.GetType().ToString)
                End Select
        End Select

    End Sub

    Public Sub GradientPanel(ByRef pnl As Syncfusion.Windows.Forms.Tools.GradientPanel)
        For Each ctrl As Control In pnl.Controls
            WhatControl(ctrl)
        Next
    End Sub

    Public Sub TabControlAdv(ByRef tabs As Syncfusion.Windows.Forms.Tools.TabControlAdv)
        Try
            For Each tb As Syncfusion.Windows.Forms.Tools.TabPageAdv In tabs.TabPages
                TabPageAdv(tb)
            Next
        Catch ex As Exception
            Debug.Print(ex.Message)

        End Try

    End Sub

    Public Sub TabPageAdv(ByRef tab As Syncfusion.Windows.Forms.Tools.TabPageAdv)
        For Each ctrl As Control In tab.Controls
            WhatControl(ctrl)
        Next
    End Sub

    Private Sub UserCtrl(ByRef UCtrl As UserControl)
        For Each ctrl As Control In UCtrl.Controls
            WhatControl(ctrl)
        Next
    End Sub

    Private Sub syncTextBox(txtBox As Syncfusion.Windows.Forms.Tools.TextBoxExt)
        ctrlBackColor = txtBox.BackColor
        AddHandler txtBox.GotFocus, AddressOf TextBox_GotFocus
        AddHandler txtBox.LostFocus, AddressOf TextBox_LostFocus
    End Sub

    Private Sub TextBox_GotFocus()
        _form.ActiveControl.BackColor = Highlight
    End Sub

    Private Sub TextBox_LostFocus()
        _form.ActiveControl.BackColor = ctrlBackColor
    End Sub

    Private Sub txtBox(txtBox As System.Windows.Forms.TextBox)
        ctrlBackColor = txtBox.BackColor
        AddHandler txtBox.GotFocus, AddressOf TextBox_GotFocus
        AddHandler txtBox.LostFocus, AddressOf TextBox_LostFocus
    End Sub

 End Class
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38744868
I think this is the best approach for this task as far as I can see. For branding, what you can do is have a static method or a class method which takes a form and applies formatting to all its controls. Then call this method when creating a form.
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 38750460
Timely as ever
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question