Me.text et richtextbox_TextChanged

Résolu
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014 - 17 sept. 2011 à 15:31
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014 - 18 sept. 2011 à 15:58
Bien le bonjour, il y a déjà quelques jours que je recherche comment ajouter "*" à Me.Text lorsque le texte change, j'ai déjà essayer :

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Me.Text = Me.Text + "*"
    End Sub


Mais le problème est que je veut ajouter cette étoile une seul fois.

Merci d'avance de votre aide !!!


Gabilach...

29 réponses

Utilisateur anonyme
17 sept. 2011 à 17:27
Salut Gabi,

Pourquoi ne pas faire plus simple:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.Text &= "*"
TextBox1.Text = Replace(TextBox1.Text, "**", "*")
TextBox1.Select(TextBox1.Text.Length - 1, 1)
End Sub


Bon.. testé en vb2010...

A+


CF2i - Guadeloupe
Ingénierie Informatique
3
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 18:00
Désoler du dernier message j'est stagner sur la page quelques heures et j'ai oublié de la rechargé xD, le code de Acive est à peut près correcte même si il c'est tromper de contrôle le code est parfait !!!

Me.Text &= "*"
        Me.Text = Replace(Me.Text, "**", "*")
        Me.Select(Me.Text.Length - 1, 1)


Donc grand merci et bonne programmations à tous

Gabilach...
3
Utilisateur anonyme
17 sept. 2011 à 18:06
Re:

As tu remarqué que je ne l'ai pas fait dans le "RichTextBox1_TextChanged" mais dans le "RichTextBox1_KeyUp"?

Essaye:

Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
RichTextBox1.Text = RichTextBox1.Text & "*"
RichTextBox1.Text = Replace(RichTextBox1.Text, "**", "*")
RichTextBox1.Select((RichTextBox1.Text.Length - 1), 1)
End Sub

Ca marche chez moi et je crois que ça devrait fonctionner en vb2005...




CF2i - Guadeloupe
Ingénierie Informatique
3
Utilisateur anonyme
18 sept. 2011 à 14:53
Moi j'ai ultimate aussi mais 2010 mais je pense pas que ce soit ça...

Essaye encore ça:

Public Class Form1
       Public TexteModifié As Boolean = False

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        'Si jamais on modifie le texte:
        TexteModifié = True
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If TexteModifié = True Then
            Dim Mess As MsgBoxResult = MsgBox("Voulez-vous enregistrer?", MsgBoxStyle.YesNo, "Confirmation")
            If Mess = MsgBoxResult.Yes Then
                e.Cancel = True
                'Tu enregistres ton fichier .....
            Else
                End
            End If
        End If
    End Sub
end class


Et dis moi l'erreur qu'il met (s'il y en a une)


CF2i - Guadeloupe
Ingénierie Informatique
3

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

Posez votre question
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
17 sept. 2011 à 16:07
Bonjour,
Si le ***caractère à droite*** est différent de "*", alors ...

***comment connaitre ce dernier caractère à droite
____________________
Réponse exacte ? => "REPONSE ACCEPTEE" pour faciliter les recherches d'autres forumeurs.
Pas d'aide en ligne installée ? ==> ne comptez pas sur moi pour simplement vous dire ce qu'elle contient
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
17 sept. 2011 à 17:16
Bonjour,

En .NET, on évite ce genre de fonction (question d'habitude et d'écriture, mais ça fonctionne quand même).
Regardes plutôt :
Chaine.SubString(Chaine.Length-LongueurALire)
Chaine est ta chaine de caractères.

---------------------------------------------------------------------
[list=ordered][*]Pour poser correctement une question et optimiser vos chances d'obtenir des réponses, pensez à lire le règlement CS et aussi ce lien[*]Quand vous postez un code, merci d'utiliser la coloration syntaxique (3ième icône en partant de la droite : )
[*]Si votre problème est résolu (et uniquement si c'est le cas), pensez à mettre "Réponse acceptée" sur le ou les messages qui vous ont aidés./list
---
Mon site
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 17:55
Merci de l'aide mais je n'arrive pas à l'utiliser je ne voie pas bien aurait tu un exemple tel qu'il soit à me donnait silteplait ?

Merci d'avance

Gabilach...
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
17 sept. 2011 à 18:04
Bonjour,

Dans l'onglet "Références" des propriétés du projet, retire l'import automatique de Microcoft.VisualBasic et remplace les fonctions VB6 Like par les fonctions .NET.

Par exemple :
TextBox1.Text = Replace(TextBox1.Text, "**", "*")
Devient :
TextBox1.Text = TextBox1.Text.Replace("**", "*")

Penses aussi à donner des noms explicites à tes contrôles, ça te facilitera le développement, par exemple, TextBox1 est moins parlant que TxtRecheche.

---------------------------------------------------------------------
[list=ordered][*]Pour poser correctement une question et optimiser vos chances d'obtenir des réponses, pensez à lire le règlement CS et aussi ce lien[*]Quand vous postez un code, merci d'utiliser la coloration syntaxique (3ième icône en partant de la droite : )
[*]Si votre problème est résolu (et uniquement si c'est le cas), pensez à mettre "Réponse acceptée" sur le ou les messages qui vous ont aidés./list
---
Mon site
0
Utilisateur anonyme
17 sept. 2011 à 18:10
Il a surement raison NHenry avec:

TextBox1.Text = TextBox1.Text.Replace("**", "*")


CF2i - Guadeloupe
Ingénierie Informatique
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 18:21
Vos codes me suffisent bien Je vous remercie

Bonne programmation et encore merci

Gabilach...
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 18:33
Sinon j'aurait bien une question, je voudrait que lorsque ma form se ferme si l'étoile est présent dans Me.text, on me pose une question avec un msgbox, j'avais déjà sa mais sa ne marche pas toujours bien :

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Activate()
        If Not RichTextBox1.Modified Then Exit Sub
        Dim Save As DialogResult = MessageBox.Show("Quitter sans sauvegarder les changements ?", Text, MessageBoxButtons.YesNo)
        If Save = DialogResult.No Then
            Dim SaveDialog As New SaveFileDialog()
            With SaveDialog
                .Filter = "Texte|*.txt|Tous|*.*"
                .DefaultExt = ".txt"
                .FileName = ""

                Dim ResultDialog As DialogResult = .ShowDialog()

                If ResultDialog = DialogResult.OK Then
                    If .FileName <> "" Then
                        Try
                            RichTextBox1.SaveFile(.FileName)
                            NotifyIcon1.Visible = False
                        Catch
                            MessageBox.Show("Le fichier ne peut pas être enregistré !!!", Text, MessageBoxButtons.OK)
                        End Try
                    End If
                End If
            End With
        End If
    End Sub


Merci bien

Gabilach...
0
Utilisateur anonyme
17 sept. 2011 à 18:48
Re nouvelle question...

C'est pas ça qui te pose problème?

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim Mess As MsgBoxResult = MsgBox("Voulez-vous quitter?", MsgBoxStyle.YesNo, "Confirmation")
If Mess = MsgBoxResult.No Then
e.Cancel = True
Else
End
End If
End Sub




CF2i - Guadeloupe
Ingénierie Informatique
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 20:10
Enfaite pour te dire c'est que j'arrive pas à faire que quand je modifie le contenu de mon richtextbox, je voudrait que si j'ai enregistrer avec mon bouton1 sa quitte sans rien demander, et que quand je n'enregistre pas sa me demande si je veut "vraiment quittez dans sauvegarder les changements ?" Voila et je ne sais pas comment mis prendre.

Merci bien

Gabilach...
0
Utilisateur anonyme
17 sept. 2011 à 20:41
Pourquoi tu ne fais pas comme ça:

Public Class Form1
'Au démarrage tu lis le contenu de ton richtextbox
Public TexteModifié As String = RichTextBox1.Text

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'si le richtextbox est différent du texte que t'as lu au démarrage:
If RichTextBox1.Text <> TexteModifié Then
Dim Mess As MsgBoxResult = MsgBox("Voulez-vous quitter?", MsgBoxStyle.YesNo, "Confirmation")
If Mess = MsgBoxResult.No Then
e.Cancel = True
'Tu enregistres ton fichier .....
End If
'et tu peux faire End après l'avoir enregistré
'ou s'il y a pas de modifs du texte il ferme tout simplement
End
End If
End Sub

Tu vois?



CF2i - Guadeloupe
Ingénierie Informatique
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 21:30
2 erreurs au démarrage je ne comprend pas j'ai mis exactement ton code

Gabilach...
0
Utilisateur anonyme
17 sept. 2011 à 21:56
Le "Public TexteModifié As String = RichTextBox1.Text" doit être juste après le "Public Class Form1"

Je ne vois pas l'erreur sauf si ton rich textbox est crée par ton code lui même
(du genre new.richtextbox et controls.add(xx))

Sinon dis moi quels erreurs...

CF2i - Guadeloupe
Ingénierie Informatique
0
Utilisateur anonyme
17 sept. 2011 à 21:59
ahh... ne fait pas un copier coller du tout, je suis en vb2010...

copie juste :
If RichTextBox1.Text <> TexteModifié Then
Dim Mess As MsgBoxResult = MsgBox("Voulez-vous quitter?", MsgBoxStyle.YesNo, "Confirmation")
If Mess = MsgBoxResult.No Then
e.Cancel = True
'Tu enregistres ton fichier .....
End If
'et tu peux faire End après l'avoir enregistré
'ou s'il y a pas de modifs du texte il ferme tout simplement
End
End If

et tu le mets dans ton formclosing


CF2i - Guadeloupe
Ingénierie Informatique
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
17 sept. 2011 à 22:02
J'est bien mis ton code au bonne endroit et j'ai cette erreur :



Et sinon j'utilise un richtextbox modifiée pour l'impression :

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing

Namespace RichTextBoxPrintCtrl
    Public Class RichTextBox1
        Inherits RichTextBox
        ' Convert the unit that is used by the .NET framework (1/100 inch) 
        ' and the unit that is used by Win32 API calls (twips 1/1440 inch)
        Private Const AnInch As Double = 14.4

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure RECT
            Public Left As Integer
            Public Top As Integer
            Public Right As Integer
            Public Bottom As Integer
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure CHARRANGE
            Public cpMin As Integer          ' First character of range (0 for start of doc)
            Public cpMax As Integer          ' Last character of range (-1 for end of doc)
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure FORMATRANGE
            Public hdc As IntPtr             ' Actual DC to draw on
            Public hdcTarget As IntPtr       ' Target DC for determining text formatting
            Public rc As RECT                ' Region of the DC to draw to (in twips)
            Public rcPage As RECT            ' Region of the whole DC (page size) (in twips)
            Public chrg As CHARRANGE         ' Range of text to draw (see above declaration)
        End Structure
        Property Findshadows(ByVal Words As String, ByVal p2 As Integer) As Integer
            Get
                Return _find
            End Get
            Set(ByVal value As Integer)
                _find = value
            End Set
        End Property

        Private Const WM_USER As Integer = &H400
        Private Const EM_FORMATRANGE As Integer = WM_USER + 57
        Private _find As Integer



        Property SelBold As Boolean

        Property SelLength As Integer

        Property SelStart As Integer

        Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr

        ' Render the contents of the RichTextBox for printing
        '	Return the last character printed + 1 (printing start from this point for next page)
        Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer

            ' Mark starting and ending character 
            Dim cRange As CHARRANGE
            cRange.cpMin = charFrom
            cRange.cpMax = charTo

            ' Calculate the area to render and print
            Dim rectToPrint As RECT
            rectToPrint.Top = e.MarginBounds.Top * AnInch
            rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
            rectToPrint.Left = e.MarginBounds.Left * AnInch
            rectToPrint.Right = e.MarginBounds.Right * AnInch

            ' Calculate the size of the page
            Dim rectPage As RECT
            rectPage.Top = e.PageBounds.Top * AnInch
            rectPage.Bottom = e.PageBounds.Bottom * AnInch
            rectPage.Left = e.PageBounds.Left * AnInch
            rectPage.Right = e.PageBounds.Right * AnInch

            Dim hdc As IntPtr = e.Graphics.GetHdc()

            Dim fmtRange As FORMATRANGE
            fmtRange.chrg = cRange                 ' Indicate character from to character to 
            fmtRange.hdc = hdc                     ' Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc               ' Point at printer hDC
            fmtRange.rc = rectToPrint              ' Indicate the area on page to print
            fmtRange.rcPage = rectPage             ' Indicate whole size of page

            Dim res As IntPtr = IntPtr.Zero

            Dim wparam As IntPtr = IntPtr.Zero
            wparam = New IntPtr(1)

            ' Move the pointer to the FORMATRANGE structure in memory
            Dim lparam As IntPtr = IntPtr.Zero
            lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
            Marshal.StructureToPtr(fmtRange, lparam, False)

            ' Send the rendered data for printing 
            res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)

            ' Free the block of memory allocated
            Marshal.FreeCoTaskMem(lparam)

            ' Release the device context handle obtained by a previous call
            e.Graphics.ReleaseHdc(hdc)

            ' Return last + 1 character printer
            Return res.ToInt32()
        End Function

        Private Sub InitializeComponent()
            Me.SuspendLayout()
            Me.ResumeLayout(False)

        End Sub
    End Class
End Namespace


Gabilach...
0
Utilisateur anonyme
17 sept. 2011 à 22:25
Y a pas grand chose dans le code...
met un "'" derrière "e.Cancel = True "
('e.Cancel = True )

ça devrait pas donner d'erreur...



CF2i - Guadeloupe
Ingénierie Informatique
0
Gabilach Messages postés 200 Date d'inscription mercredi 2 mars 2011 Statut Membre Dernière intervention 28 septembre 2014
18 sept. 2011 à 12:23
J'ai malheuresement toujours l'erreur

Gabilach...
0
Rejoignez-nous