Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim texte As String = "Maitre corbeau sur un arbre perché tenait dans son bec un fromage" & _ Environment.NewLine & _ "Maitre renard par l'odeur alléchée lui tint à peu près ce langage" RichTextBox1.AppendText(texte) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fenetre As New frmCherche() fenetre.ShowDialog() RichTextBox1.Find(fenetre.Mot) fenetre.Close() Me.BringToFront() RichTextBox1.Focus() End Sub Public Class frmCherche Inherits Windows.Forms.Form Dim txtCherche As New TextBox With {.Left 20, .Top 10, .Width = 200} Dim btnOK As New Button With {.Left 40, .Top 40, .Text = "OK"} Dim btnAnnuler As New Button With {.Left 120, .Top 40, .Text = "Annuler"} Dim _Mot As String Sub New() With Me .Controls.Add(txtCherche) .Controls.Add(btnOK) .Controls.Add(btnAnnuler) .Height = 120 .Width = 250 .Text = "Rechercher" .StartPosition = FormStartPosition.CenterParent .MaximizeBox = False .MinimizeBox = False .AcceptButton = btnOK End With AddHandler btnOK.Click, AddressOf btn_OK AddHandler btnAnnuler.Click, AddressOf btn_Annuler End Sub Sub New(ByVal Mot As String) txtCherche.Text = Mot Me.Controls.Add(txtCherche) End Sub Private Sub btn_OK(ByVal sender As Object, ByVal e As EventArgs) _Mot = txtCherche.Text Me.Hide() End Sub Private Sub btn_Annuler(ByVal sender As Object, ByVal e As EventArgs) _Mot = "" Me.Hide() End Sub ReadOnly Property Mot() As String Get Return _Mot End Get End Property End Class End Class
Private ou As Integer Private Sub cherche_premier_Click() ou = 1 trouve End Sub Private Sub cherche_suivant_Click() trouve End Sub Private Sub trouve() pos = InStr(ou, RichTextBox1.Text, Text1.Text) If pos > 0 Then RichTextBox1.SelStart = pos - 1 RichTextBox1.SelLength = Len(Text1.Text) RichTextBox1.SetFocus ou = ou + Len(Text1.Text) + pos End If End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionImports 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
Et je me demande vraiment comment mettre un champs de recherche sur la même form que mon richtextbox si je programme un genre de WordPad ?
InitializeComponent()a chaque Sub New() mais ce n'est pas très grave, sa marche nikel avec la fenetre frmrecherche, mais j'ai une question, j'ai remplacer la form par un toolstrip se trouvant dans ma form1 :
Dim _Mot As String Sub New() InitializeComponent() AddHandler ToolStripButton1.Click, AddressOf btn_OK End Sub Sub New(ByVal Mot As String) InitializeComponent() ToolStripTextBox1.Text = Mot End Sub Private Sub btn_OK(ByVal sender As Object, ByVal e As EventArgs) _Mot = ToolStripTextBox1.Text Me.Hide() End Sub ReadOnly Property Mot() As String Get Return _Mot End Get End Property Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click RichTextBox1.Find(Me.Mot) RichTextBox1.Focus() End Sub
dim mot as string = Toolstriptextbox1.text Richtextbox1.find(mot)
Dim _Mot As String Sub New() InitializeComponent() AddHandler ToolStripButton1.Click, AddressOf btn_OK End Sub Sub New(ByVal Mot As String) InitializeComponent() ToolStripTextBox1.Text = Mot End Sub Private Sub btn_OK(ByVal sender As Object, ByVal e As EventArgs) _Mot = ToolStripTextBox1.Text Me.Hide() End Sub ReadOnly Property Mot() As String Get Return _Mot End Get End Property Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim mot As String = ToolStripTextBox1.Text RichTextBox1.Find(Me.Mot) RichTextBox1.Focus() End Sub
Si maintenant tu décides de remplacer ce formulaire dynamique par un toolstriptextbox sur ton formulaire principal, tu comprendra bien que tout le code est à revoir.