Etant débutant en vb.net, j'essaie de synchroniser l'écriture entre une Textbox et une Richtextbox en utilisant le code suivant :
<code basic><code>
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(97) Then RichTextBox1.Text = RichTextBox1.Text & "a"
If e.KeyChar = Convert.ToChar(98) Then RichTextBox1.Text = RichTextBox1.Text & "b"
If e.KeyChar = Convert.ToChar(99) Then RichTextBox1.Text = RichTextBox1.Text & "c"
If e.KeyChar = Convert.ToChar(100) Then RichTextBox1.Text = RichTextBox1.Text & "d"
If e.KeyChar = Convert.ToChar(101) Then RichTextBox1.Text = RichTextBox1.Text & "e"
If e.KeyChar = Convert.ToChar(102) Then RichTextBox1.Text = RichTextBox1.Text & "f"
If e.KeyChar = Convert.ToChar(103) Then RichTextBox1.Text = RichTextBox1.Text & "g"
If e.KeyChar = Convert.ToChar(104) Then RichTextBox1.Text = RichTextBox1.Text & "h"
If e.KeyChar = Convert.ToChar(105) Then RichTextBox1.Text = RichTextBox1.Text & "i"
If e.KeyChar = Convert.ToChar(106) Then RichTextBox1.Text = RichTextBox1.Text & "j"
If e.KeyChar = Convert.ToChar(107) Then RichTextBox1.Text = RichTextBox1.Text & "k"
If e.KeyChar = Convert.ToChar(108) Then RichTextBox1.Text = RichTextBox1.Text & "l"
If e.KeyChar = Convert.ToChar(109) Then RichTextBox1.Text = RichTextBox1.Text & "m"
If e.KeyChar = Convert.ToChar(110) Then RichTextBox1.Text = RichTextBox1.Text & "n"
If e.KeyChar = Convert.ToChar(111) Then RichTextBox1.Text = RichTextBox1.Text & "o"
If e.KeyChar = Convert.ToChar(112) Then RichTextBox1.Text = RichTextBox1.Text & "p"
If e.KeyChar = Convert.ToChar(113) Then RichTextBox1.Text = RichTextBox1.Text & "q"
If e.KeyChar = Convert.ToChar(114) Then RichTextBox1.Text = RichTextBox1.Text & "r"
If e.KeyChar = Convert.ToChar(115) Then RichTextBox1.Text = RichTextBox1.Text & "s"
If e.KeyChar = Convert.ToChar(116) Then RichTextBox1.Text = RichTextBox1.Text & "t"
If e.KeyChar = Convert.ToChar(117) Then RichTextBox1.Text = RichTextBox1.Text & "u"
If e.KeyChar = Convert.ToChar(118) Then RichTextBox1.Text = RichTextBox1.Text & "v"
If e.KeyChar = Convert.ToChar(119) Then RichTextBox1.Text = RichTextBox1.Text & "w"
If e.KeyChar = Convert.ToChar(120) Then RichTextBox1.Text = RichTextBox1.Text & "x"
If e.KeyChar = Convert.ToChar(121) Then RichTextBox1.Text = RichTextBox1.Text & "y"
If e.KeyChar = Convert.ToChar(122) Then RichTextBox1.Text = RichTextBox1.Text & "z"
If e.KeyChar = Convert.ToChar(8) Then
RichTextBox1.Select()
If RichTextBox1.TextLength > 0 Then RichTextBox1.SelectionStart = RichTextBox1.TextLength
SendKeys.Send("{BKSP}")
End If
End Sub
<code basic>
</code></code>
La méthode que j'utilise fonctionne mais n'est sûrement pas optimisée et surtout elle est lourde!
NHenry
Messages postés15090Date d'inscriptionvendredi 14 mars 2003StatutModérateurDernière intervention 6 novembre 2023159 19 avril 2016 à 22:02
L'idéal serait de savoir dans quel but tu as besoin de ça, car tel que tu as fais, si le curseur de la textbox n'est pas à la fin, mais au milieu du texte, le résultat ne sera plus le bon.
De plis, si l'uitilisateur tape "!" par exemple, tu auras une désynchronisation.
Le but de mon projet est de pouvoir écrire dans ma Richtextbox à partir de ma Textbox qui elle utilise une écriture intuitive plus ou moins(celle-ci contient l'ensemble des mots français), le fichier s'appelle Dico.txt):
Option Explicit On Option Strict On Imports System.IO Imports System
Public Class Form1
Dim lst As New List(Of String) Dim MySource As New AutoCompleteStringCollection()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Chemin As String = My.Application.Info.DirectoryPath & "Dico.txt" Dim Mot() As String = File.ReadAllLines(Chemin) Dim NombreDeLigne = Mot.Length For L = 0 To NombreDeLigne - 1 lst.Add(Mot(L)) Next MySource.AddRange(lst.ToArray) TextBox1.AutoCompleteCustomSource = MySource TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource End Sub Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress If e.KeyChar = Convert.ToChar(97) Then TextBox1.Text = TextBox1.Text & "a" If e.KeyChar = Convert.ToChar(98) Then TextBox1.Text = TextBox1.Text & "b" If e.KeyChar = Convert.ToChar(99) Then TextBox1.Text = TextBox1.Text & "c" If e.KeyChar = Convert.ToChar(100) Then TextBox1.Text = TextBox1.Text & "d" If e.KeyChar = Convert.ToChar(101) Then TextBox1.Text = TextBox1.Text & "e" If e.KeyChar = Convert.ToChar(102) Then TextBox1.Text = TextBox1.Text & "f" If e.KeyChar = Convert.ToChar(103) Then TextBox1.Text = TextBox1.Text & "g" If e.KeyChar = Convert.ToChar(104) Then TextBox1.Text = TextBox1.Text & "h" If e.KeyChar = Convert.ToChar(105) Then TextBox1.Text = TextBox1.Text & "i" If e.KeyChar = Convert.ToChar(106) Then TextBox1.Text = TextBox1.Text & "j" If e.KeyChar = Convert.ToChar(107) Then TextBox1.Text = TextBox1.Text & "k" If e.KeyChar = Convert.ToChar(108) Then TextBox1.Text = TextBox1.Text & "l" If e.KeyChar = Convert.ToChar(109) Then TextBox1.Text = TextBox1.Text & "m" If e.KeyChar = Convert.ToChar(110) Then TextBox1.Text = TextBox1.Text & "n" If e.KeyChar = Convert.ToChar(111) Then TextBox1.Text = TextBox1.Text & "o" If e.KeyChar = Convert.ToChar(112) Then TextBox1.Text = TextBox1.Text & "p" If e.KeyChar = Convert.ToChar(113) Then TextBox1.Text = TextBox1.Text & "q" If e.KeyChar = Convert.ToChar(114) Then TextBox1.Text = TextBox1.Text & "r" If e.KeyChar = Convert.ToChar(115) Then TextBox1.Text = TextBox1.Text & "s" If e.KeyChar = Convert.ToChar(116) Then TextBox1.Text = TextBox1.Text & "t" If e.KeyChar = Convert.ToChar(117) Then TextBox1.Text = TextBox1.Text & "u" If e.KeyChar = Convert.ToChar(118) Then TextBox1.Text = TextBox1.Text & "v" If e.KeyChar = Convert.ToChar(119) Then TextBox1.Text = TextBox1.Text & "w" If e.KeyChar = Convert.ToChar(120) Then TextBox1.Text = TextBox1.Text & "x" If e.KeyChar = Convert.ToChar(121) Then TextBox1.Text = TextBox1.Text & "y" If e.KeyChar = Convert.ToChar(122) Then TextBox1.Text = TextBox1.Text & "z" End Sub Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Convert.ToChar(97) Then RichTextBox1.AppendText("a") If e.KeyChar = Convert.ToChar(98) Then RichTextBox1.AppendText("b") If e.KeyChar = Convert.ToChar(99) Then RichTextBox1.AppendText("c") If e.KeyChar = Convert.ToChar(100) Then RichTextBox1.AppendText("d") If e.KeyChar = Convert.ToChar(101) Then RichTextBox1.AppendText("e") If e.KeyChar = Convert.ToChar(102) Then RichTextBox1.AppendText("f") If e.KeyChar = Convert.ToChar(103) Then RichTextBox1.AppendText("g") If e.KeyChar = Convert.ToChar(104) Then RichTextBox1.AppendText("h") If e.KeyChar = Convert.ToChar(105) Then RichTextBox1.AppendText("i") If e.KeyChar = Convert.ToChar(106) Then RichTextBox1.AppendText("j") If e.KeyChar = Convert.ToChar(107) Then RichTextBox1.AppendText("k") If e.KeyChar = Convert.ToChar(108) Then RichTextBox1.AppendText("l") If e.KeyChar = Convert.ToChar(109) Then RichTextBox1.AppendText("m") If e.KeyChar = Convert.ToChar(110) Then RichTextBox1.AppendText("n") If e.KeyChar = Convert.ToChar(111) Then RichTextBox1.AppendText("o") If e.KeyChar = Convert.ToChar(112) Then RichTextBox1.AppendText("p") If e.KeyChar = Convert.ToChar(113) Then RichTextBox1.AppendText("q") If e.KeyChar = Convert.ToChar(114) Then RichTextBox1.AppendText("r") If e.KeyChar = Convert.ToChar(115) Then RichTextBox1.AppendText("s") If e.KeyChar = Convert.ToChar(116) Then RichTextBox1.AppendText("t") If e.KeyChar = Convert.ToChar(117) Then RichTextBox1.AppendText("u") If e.KeyChar = Convert.ToChar(118) Then RichTextBox1.AppendText("v") If e.KeyChar = Convert.ToChar(119) Then RichTextBox1.AppendText("w") If e.KeyChar = Convert.ToChar(120) Then RichTextBox1.AppendText("x") If e.KeyChar = Convert.ToChar(121) Then RichTextBox1.AppendText("y") If e.KeyChar = Convert.ToChar(122) Then RichTextBox1.AppendText("z") End Sub End Class
L'idéal bien sur serait de faire l'inverse, de plus je suis loin d'avoir répertorié l'ensemble des touches.
J'espère avoir été un peu plus clair dans mes explications et encore merci de m'accordé de votre temps
NHenry
Messages postés15090Date d'inscriptionvendredi 14 mars 2003StatutModérateurDernière intervention 6 novembre 2023159 Modifié par NHenry le 19/04/2016 à 22:19
D'accord, pourquoi ne pas faire (pour un sens, je te laisse chercher pour l'autre) :
(Tapé hors éditeur)
.. Sub Textbox_Change(...) 'Ou TextChange, je ne sais plus
RichTextBox1.Text=TextBox1.Text
RichTextBox1.SelStart=TextBox1.SelStart 'Ou SelectionStart, pareil, je l'utilise trop rarement
RichTextBox1.SelLength=TextBox1.SelLength
End Sub
J'interviens principalement en VB6 et VB.NET, avec un peu de C#, mais la modération m'amène souvent sur d'autre langages.
En VB.NET pensez à activer "Option Explicit" et "Option Strict"
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged RichTextBox1.Text = TextBox1.Text RichTextBox1.SelectionStart = TextBox1.SelectionStart RichTextBox1.SelectionLength = TextBox1.SelectionLength End Sub
19 avril 2016 à 22:13
Le but de mon projet est de pouvoir écrire dans ma Richtextbox à partir de ma Textbox qui elle utilise une écriture intuitive plus ou moins(celle-ci contient l'ensemble des mots français), le fichier s'appelle Dico.txt):
L'idéal bien sur serait de faire l'inverse, de plus je suis loin d'avoir répertorié l'ensemble des touches.
J'espère avoir été un peu plus clair dans mes explications et encore merci de m'accordé de votre temps