2016-01-20 83 views
0

我在VB中是新的,請指導我下面事項:vb.net按鍵事件(Visual Studio 2010中)

有不同的解決方案(例如,2種不同的形式:SolutionA \ Form1並SolutionB \窗體2)。

這兩個表單都有多個文本框。

首先我們在SolutionA \ Form1的文本框中輸入數據。然後,通過使用KeyPress事件,光標移動到SolutionB \ Form2中的文本框。

之後,通過在SolutionB \窗體2使用按鍵事件,光標移回在SolutionA \ Form1的另一個文本框。

是否可以點這種方式?如果可能的話,請讓我知道如何編碼。我正在使用Visual Studio 2010.謝謝。


我用名爲txtPart的文本框創建了一個測試表單(表單1)。

一旦我點擊「Enter」按鈕,我希望txtPart的數據傳輸到記事本中。 如何從這裏調用NotepadSendAndReceive類?

Public Class Form1 

    Private Sub txtPart_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPart.KeyPress 

     Try 
      If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then 
       ' How to call NotepadSendAndReceive class here? 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     End Try 
    End Sub 

End Class 

請幫忙。謝謝。

============================================== ==================================

我創建了一個測試表單(表單1)與一個名爲txtPart的文本框。

一旦我點擊「Enter」按鈕,我希望txtPart的數據傳輸到記事本中。我如何從這裏調用NotepadSendAndReceive類? 公共類Form1中

Private Sub txtPart_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPart.KeyPress 

    Try 
     If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then 
      ' How to call NotepadSendAndReceive class here? 
     End If 
    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try 
End Sub 

末級

請請幫助。謝謝。

+0

你的KeyPress事件中是否有'Form2.TextBox1.Focus()'? – GrindelOh

+0

不,因爲我不確定如何將Focus()形成爲不同的解決方案。但是,如果以兩種形式使用相同的解決方案,我總是以這種方式使用。謝謝。 – orenmango

回答

0

這是通過使用NamedPipeServer(高複雜性)或 以其他方式使用WINAPI可能(我會推薦這)。

看一看這樣的:

Public Class NotepadSendAndReceive 
     Public Const VK_RETURN As UInteger = &HD 
     Public Const WM_SETFOCUS As UInteger = &H7 
     Public Const WM_KILLFOCUS As UInteger = &H8 
     Public Const WM_CHAR As UInteger = &H102 
     Public Const WM_SETTEXT As UInteger = &HC 
     Public Const WM_KEYDOWN As UInteger = &H100 
     Public Const WM_KEYUP As UInteger = &H101 
     Public Const cap As Integer = 1048576 
     Public Const WM_CLICK As UInteger = &HF5 
     Public Const WM_GETTEXT As UInteger = &HD 
     Public Const WM_GETTEXTLENGTH As UInteger = &HE 

     <DllImport("User32.Dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowTextLength(hWnd As IntPtr) As Integer 
     End Function 
     <DllImport("User32.Dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer 
     End Function 
     <DllImport("User32.Dll")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As String) As IntPtr 
     End Function 
     <DllImport("User32.Dll")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer 
     End Function 
     <DllImport("User32.Dll")> Public Shared Function PostMessage(hWnd As Integer, msg As UInteger, wParam As UInteger, lParam As Integer) As IntPtr 
     End Function 
     <DllImport("User32.Dll")> Public Shared Function PostMessage(hWnd As Integer, msg As UInteger, wParam As UInteger, lParam As UInteger) As IntPtr 
     End Function 
     <DllImport("User32.Dll")> Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr 
     End Function 
     <DllImport("User32.Dll")> Private Shared Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As IntPtr) As IntPtr 
     End Function 

     Private Shared Function GetStringOfEditor() As String 
      Return GetText(GetProcNumber("Editor")) 
     End Function 

     Public Shared Sub SendStringToEditor(T As String) 
      SendMessage(GetProcNumber("Editor"), &HC, CType(cap, IntPtr), T) 
     End Sub 

     Public Shared Sub SendEnter(Hwndd As IntPtr) 
      SendMessage(Hwndd, WM_SETFOCUS, CType(cap, IntPtr), String.Empty) 
      Threading.Thread.Sleep(10) 
      PostMessage(Hwndd.ToInt32(), WM_KEYDOWN, &HD, &H1C0001) 
      PostMessage(Hwndd.ToInt32(), WM_CHAR, &HD, &H1C0001) 
      Threading.Thread.Sleep(10) 
      PostMessage(Hwndd.ToInt32(), WM_KEYUP, &HD, &HC01C0001UI) 
      SendMessage(Hwndd, WM_KILLFOCUS, CType(cap, IntPtr), String.Empty) 
     End Sub 

     Private Shared Function GetProcNumber(pTitle As String) As IntPtr 
      Dim Pzs As New List(Of String) 
      For Each p As Process In Process.GetProcesses 
       If p.MainWindowTitle.Contains(pTitle) Then Pzs.Add(p.MainWindowTitle) 
      Next 
      If Pzs.Count > 0 Then Return FindWindowEx(FindWindow("Notepad", Pzs(0)), IntPtr.Zero, "Edit", IntPtr.Zero) 
     End Function 

     Private Shared Function GetText(hWnd As IntPtr) As String 
      Dim nLen As Integer = CInt(SendMessage(hWnd, WM_GETTEXTLENGTH, IntPtr.Zero, String.Empty)) 
      Dim pText As New StringBuilder(nLen + 1) 
      SendMessage(hWnd, WM_GETTEXT, nLen + 1, pText) 
      Return pText.ToString() 
     End Function 

    End Class 

調用示例(記事本必須打開)

NotepadSendAndReceive.SendStringToEditor("This is a test") 

你只需要自定義此一點點,你就必須 找出您的文本框的當前句柄。通過使用「FindWindowEx」

If Pzs.Count > 0 Then Return FindWindowEx(FindWindow("Notepad", Pzs(0)), IntPtr.Zero, "Edit", IntPtr.Zero) 

該方案旨在爲「記事本」,然後在「編輯」元素的子把手當前手柄:

看看。

然後你有正確的當前句柄爲您的記事本編輯區域,你可以例如發送文本到它。

在你的情況下,它將是「Form2」,然後你必須知道你的文本框是如何命名的(這可以通過使用WinSpy ++找到)。

此致敬禮。

+0

謝謝Sir SiriSch!我將研究這種方法並嘗試編碼。我在許多論壇搜索,但從來沒有發現過這個想法。再次感謝 :) – orenmango