At one point I came across an interesting programming problem. Windows has 4 similar, but different functions for Finding a Window in it's API. Having the below code, would it be possible to combine the two Select Case statements into one function and assign which Findwindow function to use on a variable so the correct one could be called at a latter time without another Select Case structure?
Before, needing two Select Case structures because you're doing two different events:
Code:Private Sub txtClassName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClassName.TextChanged, txtWindowCaption.TextChanged Select Case True Case txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As Integer, WindowName As Integer) will be called." Case txtWindowCaption.Text.Length.Equals(0) AndAlso Not txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As String, WindowName As Integer) will be called." Case txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As Integer, WindowName As String) will be called." Case Else lblFunctionCalled.Text = "FindWindow (ClassName As String, WindowName As String) will be called." End Select End Sub Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click Dim hWnd As IntPtr Select Case True Case txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) hWnd = Win32API.FindWindowAny(0, 0) Case txtWindowCaption.Text.Length.Equals(0) AndAlso Not txtClassName.Text.Length.Equals(0) hWnd = Win32API.FindWindowNullWindowCaption(txtClassName.Text, 0) Case Not txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) hWnd = Win32API.FindWindowNullClassName(0, txtWindowCaption.Text) Case Else hWnd = Win32API.FindWindow(txtClassName.Text, txtWindowCaption.Text) End Select End Sub
After, assigning our different functions to a lambda and calling it. Look how much cleaner the code is!:
Code:Private lambda As Func(Of String, String, IntPtr) Private Sub txtClassName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClassName.TextChanged, txtWindowCaption.TextChanged Select Case True Case txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As Integer, WindowName As Integer) will be called." lambda = Function(a As String, b As String) Win32API.FindWindowAny(0, 0) Case txtWindowCaption.Text.Length.Equals(0) AndAlso Not txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As String, WindowName As Integer) will be called." lambda = Function(a As String, b As String) Win32API.FindWindowNullWindowCaption(a, 0) Case Not txtWindowCaption.Text.Length.Equals(0) AndAlso txtClassName.Text.Length.Equals(0) lblFunctionCalled.Text = "FindWindow (ClassName As Integer, WindowName As String) will be called." lambda = Function(a As String, b As String) Win32API.FindWindowNullClassName(0, b) Case Else lblFunctionCalled.Text = "FindWindow (ClassName As String, WindowName As String) will be called." lambda = Function(a As String, b As String) Win32API.FindWindow(a, b) End Select End Sub Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click Dim hWnd As IntPtr = lambda(txtClassName.Text, txtWindowCaption.Text) End Sub




the people who helped you and mark your forum RESOLVED when you're done! 
Reply With Quote