Détecter la touche ENTER sur un usercontrol INPUTBOX

Adn56 Messages postés 1172 Date d'inscription jeudi 24 mai 2007 Statut Membre Dernière intervention 28 septembre 2013 - 29 janv. 2010 à 18:41
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 - 30 janv. 2010 à 18:09
Bonsoir à tous,
Voici mon soucis : J'ai récupéré et modifié la source de Marco Bellinaso, mais voila pas possible d'activer le dialog.result.ok si l'user appui sur la touche ENTER lors de la saisie de la valeur (obligé de cliquer sur OK, c'est saoulant non ?).
J'éspére être explicite, car visiblement y'a un truc que j'ai pas pigé

Pour tester la fonction : copier ce code dans une classe d'un nouveau projet et appellez la class via la form du projet :

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim coul_fond As Color = Color.Black
        Dim coul_text As Color = Color.White
        Dim code As String = InputDialog.affiche("Saisir votre code", "bienvenue", "****", coul_fond, coul_text)
        MessageBox.Show(code)
    End Sub


avec le code ci dessous pour la class InputDialog :

' Create an InputBox like the one of VB6. You can define the dialog's title,
'  question and default value.
' Example: MessageBox.Show(InputDialog.InputBox("Type your name:", "Test",
'  "Marco"))
' code de Marco Bellinaso

Public Class InputDialog
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents txtValue As System.Windows.Forms.TextBox
    Friend WithEvents lblPrompt As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnOK = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.txtValue = New System.Windows.Forms.TextBox
        Me.lblPrompt = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'btnOK
        '
        Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.btnOK.Location = New System.Drawing.Point(11, 76)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(72, 24)
        Me.btnOK.TabIndex = 1
        Me.btnOK.Text = "&OK"
        '
        'btnCancel
        '
        Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Location = New System.Drawing.Point(91, 76)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(72, 24)
        Me.btnCancel.TabIndex = 2
        Me.btnCancel.Text = "&Annuler"
        '
        'txtValue
        '
        Me.txtValue.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtValue.Location = New System.Drawing.Point(12, 42)
        Me.txtValue.Name = "txtValue"
        Me.txtValue.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
        Me.txtValue.Size = New System.Drawing.Size(151, 20)
        Me.txtValue.TabIndex = 0
        Me.txtValue.UseSystemPasswordChar = True
        '
        'lblPrompt
        '
        Me.lblPrompt.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblPrompt.Location = New System.Drawing.Point(12, 7)
        Me.lblPrompt.Name = "lblPrompt"
        Me.lblPrompt.Size = New System.Drawing.Size(151, 32)
        Me.lblPrompt.TabIndex = 4
        '
        'InputDialog
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(175, 113)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.txtValue)
        Me.Controls.Add(Me.lblPrompt)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "InputDialog"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Shared valide As Boolean = False 'modif @dn !marche poa! je veux qu'un enter sur la saisie du code valide la form

    Public Property Prompt() As String
        Get
            Return lblPrompt.Text
        End Get
        Set(ByVal Value As String)
            lblPrompt.Text = Value
        End Set
    End Property

    Public Property Value() As String
        Get
            Return txtValue.Text.Trim()
        End Get
        Set(ByVal Value As String)
            txtValue.Text = Value.Trim()
            ' preselect the text, and give the focus to this control
            txtValue.SelectAll()
            txtValue.Focus()
        End Set
    End Property

    ' create an InputDialog window, and return the typed text, et modif @dn des couleurs et format de border de la form ;=)
    Public Shared Function affiche(ByVal prompt As String, _
        ByVal title As String, ByVal defaultVal As String, _
        ByVal couleur_fond As Color, ByVal couleur_texte As Color) As String
        Dim dlg As New InputDialog
        dlg.Text = title
        dlg.Prompt = prompt
        dlg.Value = defaultVal
        dlg.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        dlg.BackColor = couleur_fond
        dlg.ForeColor = couleur_texte

        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK _
            Or valide Then ' dlg.valide marche poa
            Return dlg.Value
        Else
            Return defaultVal
        End If
    End Function
    ' modif @dn mais marche pas -_-"

    Private Sub txtValue_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtValue.KeyDown
        If e.KeyCode Keys.Enter Then valide True
    End Sub

End Class


Je suis sûr que ce bout de code qui remplace la dialog de VB6 sera trés utile aux débutant comme moi, donc merci de votre aide.
Cordialement @dn

5 réponses

PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
29 janv. 2010 à 21:26
salut,

c'est le code d'origine ça?
qu'est-ce que tu nous a fait....

pourquoi il y a un Dim dlg As New InputDialog dans l'input?..

tu dois faire tout ça sur ME

ensuite c'est 50 fois plus simple de traiter l'évènement CLICK du bouton au lieu de laisser le DEFAULT (bouton) agir, du coup une procédure séparée, que tu appelles aussi en cas de touche enter

++
[hr]
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
29 janv. 2010 à 21:57
pis c'est pas un usercontrol en plus

une manière :

un NEW plus pratique
    Public Sub New(ByVal cBackColor As Color, ByVal cForeColor As Color)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.BackColor = cBackColor
        Me.ForeColor = cForeColor
    End Sub


une code plus... évènementiel
    Private mValue As String

    Public Overridable Overloads Function Show(ByVal sPrompt As String, ByVal sTitle As String, Optional ByVal sDefaultValue As String = "") As String
        lblPrompt.Text = sPrompt
        Me.Text = sTitle
        With txtValue
            .Text = sDefaultValue
            .SelectAll()
            .Focus()
        End With
        MyBase.ShowDialog()
        Return mValue
    End Function


    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        mValue = String.Empty
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SetReturn()
    End Sub
    Private Sub txtValue_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtValue.KeyDown
        If e.KeyCode = Keys.Enter Then SetReturn()
    End Sub
    Private Sub SetReturn()
        mValue = txtValue.Text
        Me.Close()
    End Sub


et l'appel :
    Dim Input As New InputDialog(Color.Black, Color.White)
    Dim sRet As String = Input.Show("Saisir votre code", "bienvenue", "****")
    Input = Nothing
    MessageBox.Show(sRet)



++
0
Adn56 Messages postés 1172 Date d'inscription jeudi 24 mai 2007 Statut Membre Dernière intervention 28 septembre 2013 1
30 janv. 2010 à 17:16
arf, ben tu vois je suis aussi balaise pour écrire des trucs à la con quepour trouver du code à la con ^^
Non ce code n'est pas de moi, en fait avant je faisiat comme (+/-) disons comme ma F_operation dans le compte.
Puis je me suis rendu compte que l'on ne pouvez pas modifier les couleurs des commondialog (input, color, open/save...)
Donc je me suis mis en quéte de classe (usercontrol) pour avoir sous le coude une dll de dialogbox paramétrable.
C'est la que je suis tombé sur :
http://www.devx.com/vb2themax/Tip/19625

Comme quoi sur le net ya pas que du bon

Bon je reprend mon anciénne méthode pour l'inputbox.

Si quelqu'un sait comment faire pour avoir des colordialog et open/save dont on peut changer la couleur de fond cela me plairait.
Merci pour le code et @++ pour de nouvelle aventure :)
0
Adn56 Messages postés 1172 Date d'inscription jeudi 24 mai 2007 Statut Membre Dernière intervention 28 septembre 2013 1
30 janv. 2010 à 18:01
Et voila, j'ai mixer ta façon et la mienne et cela donne cela dans une form

Private Sub F_Saisi_code_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me 'configuration de la form
            ' type de form
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
            'couleur de fond
            .BackColor = F_principal.Couleur_forms
            'couleur des textes
            .ForeColor = F_principal.Couleur_textes
            'défini la taille de la form 
            .Width = 145
            .Height = 110
            'démarrage au centre de l'application
            .StartPosition = FormStartPosition.CenterParent
        End With

        txtvalue.SelectAll()
        txtvalue.Focus()
    End Sub

    Private mValue As String = String.Empty

    Public Overridable Overloads Function Show(ByVal Titre As String) As String
        lblPrompt.Text = Titre

        With txtvalue
            ' utilise le system de password
            .PasswordChar = CChar("*")
            .UseSystemPasswordChar = True
            .Text = "******"
            ' selectionne la zone de saisi
            .SelectAll()
            .Focus()
        End With

        MyBase.ShowDialog()
        Return mValue
    End Function


    Private Sub bp_quit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bp_quit.Click
        Me.Close()
    End Sub

    Private Sub bp_ok_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bp_ok.Click
        valide()
    End Sub
    Private Sub txtValue_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtvalue.KeyDown
        If e.KeyCode = Keys.Enter Then valide()
    End Sub
    Private Sub valide()
        mValue = txtvalue.Text
        Me.Close()
    End Sub
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
30 janv. 2010 à 18:09
bien ;)

un détail :
VB.NET supporte le CHAR directement
pas besoin de convertir

.PasswordChar = CChar("*")
devient alors
.PasswordChar = "*"c


n'oublie pas de valider
++
0
Rejoignez-nous