اگر بخوام توی ویژوال بیسیک 2005 یک کلمه رو درون یک text box جستجو کنم چه کدی باید بنویسم؟
Printable View
اگر بخوام توی ویژوال بیسیک 2005 یک کلمه رو درون یک text box جستجو کنم چه کدی باید بنویسم؟
پيدا کردن قبلیکد:Dim lngResult As Integer
Dim IntPos As Integer
Dim Options As System.Windows.Forms.RichTextBoxFinds = RichTextBoxFinds.None
Public FindPre As Boolean = False
Public Nextfind As Boolean = False
کد HTML:
پيدا کردن بدیکد:Public Sub FindMEPre(ByVal Where As RichTextBox, ByVal What As String, ByVal BeginStart As CheckBox, ByVal Wrap As CheckBox)
'Try
Dim OptionsPer As System.Windows.Forms.RichTextBoxFinds
OptionsPer = Options + RichTextBoxFinds.Reverse
Where.SelectionLength = 0
Select Case FindPre
'~~~~~~~~~~~~~'Next
Case False
Select Case BeginStart.CheckState 'آ يا بايد از اول شروع کنه يا نه
Case CheckState.Checked 'آره از اول شروع بشه
IntPos = Where.TextLength
Case CheckState.Unchecked 'خير،از جايی که هست شروع کنه
IntPos = Where.SelectionStart
End Select
'~~~~~~~~~~~~~'Find Next
Case True
IntPos = Where.SelectionStart
If IntPos = 0 Then GoTo Boro
End Select
'Search Me
lngResult = Where.Find(What, 0, IntPos, OptionsPer)
'Not Found
If lngResult = -1 Then
Boro:
Select Case Wrap.CheckState
Case CheckState.Checked 'اگه بايد بيا از سر شروع کنه
Where.SelectionStart = Where.TextLength
Where.SelectionLength = 0
FindPre = False
IntPos = Where.TextLength
Case CheckState.Unchecked 'اگه بايد پيغام نشون بده
MessageBox.Show("Cannot find string", "Search", MessageBoxButtons.OK)
FindPre = False
Exit Sub
End Select
Else 'Text Found
FindPre = True
End If
Where.ScrollToCaret()
'Catch ex As Exception
' IntPos = Where.TextLength
' pakkon("Try Again")
'End Try
End Sub
کد:
پيدا کردن همهکد:Public Sub Findnext(ByVal Where As RichTextBox, ByVal What As String, ByVal BeginStart As CheckBox, ByVal Wrap As CheckBox)
Try
Select Case Nextfind
'~~~~~~~~~~~~~'Next
Case False
Select Case BeginStart.CheckState 'آ يا بايد از اول شروع کنه يا نه
Case CheckState.Checked 'آره از اول شروع بشه
IntPos = 0
Case CheckState.Unchecked 'خير،از جايی که هست شروع کنه
IntPos = Where.SelectionStart
End Select
'~~~~~~~~~~~~~'Find Next
Case True
Select Case BeginStart.CheckState
Case CheckState.Checked
IntPos = lngResult + What.Length
Case CheckState.Unchecked 'خير،از جايی که هست شروع کنه
If Where.SelectionLength > 0 Then
IntPos = Where.SelectionStart + Where.SelectionLength
Else
IntPos = Where.SelectionStart
End If
If IntPos >= Where.Text.Length Then GoTo Boro
End Select
End Select
'Search Me
lngResult = Where.Find(What, IntPos, Options)
'Not Found
If lngResult = -1 Then
Select Case Wrap.CheckState
Case CheckState.Checked 'اگه بايد بيا از سر شروع کنه
Where.SelectionStart = 0
Where.SelectionLength = 0
Nextfind = False
IntPos = 0
Case CheckState.Unchecked 'اگه بايد پيغام نشون بده
Boro:
MessageBox.Show("Cannot find string", "Search", MessageBoxButtons.OK)
Nextfind = False
IntPos = 0
Exit Sub
End Select
Else 'Text Found
Nextfind = True
End If
Where.ScrollToCaret()
Catch ex As Exception
IntPos = 0
End Try
End Sub
کد:کد:Public Function FindAll(ByVal Where As RichTextBox, ByVal What As TextBox, ByVal Bar As ProgressBar)
Dim Founded As Integer = 0
Dim intPosition As Integer = 0
Dim OptionsAll As System.Windows.Forms.RichTextBoxFinds
OptionsAll = RichTextBoxFinds.NoHighlight + Options
intPosition = 0
Bar.Visible = True
Do
lngResult = Where.Find(What.Text, intPosition, OptionsAll)
If lngResult = -1 Then
Exit Do
Else
intPosition = lngResult + What.Text.Length
Founded += 1
If intPosition >= Where.Text.Length Then Exit Do
End If
Loop
Bar.Visible = False
Return Founded
End Function
اگر فقط ميخواي بدوني كه اون رشته مورد نظر وجود داره يا نه از دستور زير استفاده كن
کد:If TextBox1.Text.Contains("رشته مورد نظر") Then
EndIf