OVH Cloud OVH Cloud

OK ou cancel??

2 réponses
Avatar
Jean-Philippe Bariteau
Bonjour, j'aimerais savoir comment différencier, lors d'une entrée dans un
input box, si l'utilisateur a entré une chaine vide et a pesé sur ok ou si
il a pesé sur cancel.

Merci



--

Jean-Philippe Bariteau
CRGL / Net Communications

2 réponses

Avatar
Bismark Prods
Bonjour,

"Jean-Philippe Bariteau" a écrit dans le
message de news:
Bonjour, j'aimerais savoir comment différencier, lors d'une entrée dans un
input box, si l'utilisateur a entré une chaine vide et a pesé sur ok ou si
il a pesé sur cancel.



Dans ce cas il semble plus approprié de créer une boite de dialogue
vous-même et de capturer les informations avec DialogResult sur la sortie de
la fonction Form.ShowDialog


Merci



--

Jean-Philippe Bariteau
CRGL / Net Communications





Bismark
Avatar
Zoury
Salut Jean-Phillipe! :O)

Il n'y a aucune façon de le déterminer implicitement.. la méthode la plus
approrié resterait à l'aide d'un hook..

voici un exemple (désolé pour les longues lignes..):
'***
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Text
Imports System.Reflection

Public Class InputBoxEx

Private Const WH_CALLWNDPROC As Int32 = &H4
Private Const WM_SYSCOMMAND As Int32 = &H112
Private Const WM_MOUSEACTIVATE As Int32 = &H21
Private Const SC_CLOSE As Int32 = &HF060&

<StructLayout(LayoutKind.Sequential)> _
Private Structure CWPSTRUCT
Public lParam As Int32
Public wParam As Int32
Public message As Int32
Public hWnd As IntPtr
End Structure

Private Delegate Function MessageProcDelegate _
( _
ByVal nCode As Int32, _
ByVal wParam As IntPtr, _
ByRef lParam As CWPSTRUCT _
) As Int32

<DllImport("user32", SetLastError:=True)> _
Private Shared Function SetWindowsHookEx _
( _
ByVal idHook As Int32, _
ByVal lpfn As MessageProcDelegate, _
ByVal hInstance As IntPtr, _
ByVal threadId As Int32 _
) As Int32
'
End Function

<DllImport("user32", SetLastError:=True)> _
Private Shared Function UnhookWindowsHookEx _
( _
ByVal idHook As Int32 _
) As Boolean
'
End Function

<DllImport("user32", SetLastError:=True)> _
Private Shared Function CallNextHookEx _
( _
ByVal idHook As Int32, _
ByVal nCode As Int32, _
ByVal wParam As IntPtr, _
ByRef lParam As CWPSTRUCT _
) As Int32
'
End Function

<DllImport("user32", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText _
( _
ByVal hwnd As IntPtr, _
ByVal lpString As StringBuilder, _
ByVal cch As Int32 _
) As Int32
'
End Function

Private Shared m_hHook As Int32 = 0
Private Shared m_bCanceled As Boolean
Private Shared m_owner As IWin32Window
Private Shared m_dlHookProc As MessageProcDelegate = New
MessageProcDelegate(AddressOf InputBoxEx.CallWndProc)

Public Shared Function Show _
( _
ByVal Prompt As String, _
Optional ByRef Title As String = "", _
Optional ByRef DefaultResponse As String = "", _
Optional ByRef XPos As Int32 = 0, _
Optional ByRef YPos As Int32 = 0, _
Optional ByRef Owner As IWin32Window = Nothing _
) As String
m_owner = Owner
Hook()
Dim sRet As String = InputBox(Prompt, Title, DefaultResponse, XPos,
YPos)
UnHook()
If (m_bCanceled) Then sRet = Nothing
Return sRet
End Function

Private Shared Sub Hook()
m_bCanceled = False
m_hHook = SetWindowsHookEx(WH_CALLWNDPROC, m_dlHookProc,
IntPtr.Zero, AppDomain.GetCurrentThreadId())
End Sub

Private Shared Sub UnHook()
If (m_hHook > 0) Then
UnhookWindowsHookEx(m_hHook)
End If
End Sub

Private Shared Function CallWndProc(ByVal code As Int32, ByVal wParam As
IntPtr, ByRef lParam As CWPSTRUCT) As Int32

Select Case lParam.message
Case WM_SYSCOMMAND
If (lParam.wParam = SC_CLOSE) Then
m_bCanceled = True
End If
Case WM_MOUSEACTIVATE
Try
Dim sb As New StringBuilder(255)
GetWindowText(lParam.hWnd, sb, sb.Capacity)
If (sb.ToString() = "Annuler") Then
m_bCanceled = True
End If
Finally
End Try
End Select

Return CallNextHookEx(m_hHook, code, wParam, lParam)

End Function

End Class
'***

exemple d'utilisation :
'***
' Form1
Option Explicit On

Public Class Form1
Inherits System.Windows.Forms.Form

' windows code form blablabla...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s As String = InputBoxEx.Show("Renvoit la valeur inscrite dans
le TextBox si on appuie sur Ok. Sinon on renvoit NULL..", "InputBoxEx Démo",
, , , Me)
If (s Is Nothing) Then
Trace.WriteLine("l'opération à été annuler")
Else
Trace.WriteLine("l'opération à renvoyé : " & s)
End If
End Sub
End Class
'***

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic classique
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/
"Jean-Philippe Bariteau" a écrit dans le
message de news:
Bonjour, j'aimerais savoir comment différencier, lors d'une entrée dans un
input box, si l'utilisateur a entré une chaine vide et a pesé sur ok ou si
il a pesé sur cancel.

Merci



--

Jean-Philippe Bariteau
CRGL / Net Communications