18-09-16, 02:12 AM
(آخر تعديل لهذه المشاركة : 18-09-16, 02:14 AM {2} بواسطة Doctor GME.)
أخى سعود
حاولت التعديل فيه ولم أنجح إلا فى خاصية find once
أما الباقى بحاجة إلا مساعدة على تعديل الكود
أخى العزيز كما قلت للأخ سعود فى الردود السابقة
برنامجى به textbox ولا أستطيع تغييره ل richtextbox لأن الكود أصبح كبير جداً
ولا يمكننى تغييره فالأسهل بكثير هو تعديل كود البحث ليتماشى مع textbox
حاولت التعديل فيه ولم أنجح إلا فى خاصية find once
أما الباقى بحاجة إلا مساعدة على تعديل الكود
كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'button called (Find Once)
SearchText(Form1.TextBox1.Text)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'button called (Find Next)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'button called (Find Previous)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'button called (Find All)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Public Overloads Function SearchText(ByVal textToFind As String) As Integer
'Contains the return value of the search.
'if it returns -1, then a match was not found.
textToFind = TextBox1.Text
Dim nomatchCase As Boolean = False
Dim highlightText As Boolean = True
Dim startPosition As Integer = 0
Dim endPosition As Integer = 0
Dim i As Integer
If endPosition < 1 Then
If Not nomatchCase Then
textToFind = textToFind.ToLower
Dim temp As String = Form1.TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)
Else
i = Form1.TextBox1.Text.IndexOf(textToFind, startPosition, Me.Text.Length)
End If
Else
If nomatchCase = False Then
textToFind = textToFind.ToLower
Dim temp As String = Form1.TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, endPosition)
Else
i = Form1.TextBox1.Text.IndexOf(textToFind, startPosition, endPosition)
End If
End If
If i > -1 Then
If highlightText Then
Form1.TextBox1.Focus()
Form1.TextBox1.SelectionStart = i
Form1.TextBox1.SelectionLength = textToFind.Length
End If
End If
'
'Returns the position the text was found at
'otherwise it will report -1, which means that the search string was not found.
Return i
End Function
End Class(15-09-16, 02:17 AM)silverlight كتب : أتمني إن الكود التالي سوف يعطيك فكرة جيدة عن كيفية البحث عن كلمة في RichTextBox
افتح مشروع و أضف الي الفورم RichTextBox و باتون ثم اكتب الكود بالشكل التالي
الكود سوف يجد لك الكلمة التي تبحث عنها
وهنا انا قمت بالبحث عن كلمة vb4arab
PHP كود :
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String = "vb4arab demo, The fox knows to jump but how, how he jump?, and how he learned to jumb bla bla bla bla, vb4arab demo"
RichTextBox1.Text = s
End Sub
Private Function TryFindString(stringTofind As String, highlightColor As Color, matchCase As Boolean, matchWholeWord As Boolean) As Boolean
Me.RichTextBox1.SelectAll()
Me.RichTextBox1.SelectionBackColor = Color.White
Dim startIndex As Integer = 0
Dim searchOption As RichTextBoxFinds = Nothing
If matchCase AndAlso matchWholeWord Then
searchOption = RichTextBoxFinds.MatchCase Or RichTextBoxFinds.WholeWord
ElseIf matchCase Then
searchOption = RichTextBoxFinds.MatchCase
ElseIf matchWholeWord Then
searchOption = RichTextBoxFinds.WholeWord
Else
searchOption = RichTextBoxFinds.None
End If
Dim found As Boolean = False
Dim index As Integer = -1
While (Me.FindIndex(Me.RichTextBox1.Find(stringTofind, startIndex, searchOption), index)) > -1
found = True
Me.RichTextBox1.SelectionBackColor = highlightColor
startIndex = index + 1
End While
Return found
End Function
Private Function FindIndex(value As Integer, ByRef index As Integer) As Integer
index = value
Return value
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim search As String = "vb4arab"
Dim found As Boolean = TryFindString(search, SystemColors.MenuHighlight, True, True)
If Not found Then
Dim errorString As String = String.Format("Can't find the word ""{0}"", try another word", search)
MessageBox.Show(errorString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
End Class
أخى العزيز كما قلت للأخ سعود فى الردود السابقة
برنامجى به textbox ولا أستطيع تغييره ل richtextbox لأن الكود أصبح كبير جداً
ولا يمكننى تغييره فالأسهل بكثير هو تعديل كود البحث ليتماشى مع textbox
