2012年12月4日 星期二

StreamReader 寫 textbox 中文亂碼

 Dim sr1 As New StreamReader("C:\test.txt", System.Text.Encoding.Default)

只要用這個語法就正常了。

2012年12月2日 星期日

DataGridView Double Click 欄位填值


 Private Sub DataGridView1_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Select Case e.ColumnIndex
            Case 0
               TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value
            Case 3
                TextBox1.Text = DataGridView1.CurrentRow.Cells(3).Value
        End Select
    End Sub


Case 0 代表第一欄,

CurrentRow 就是 Double Click 的那一列,

Cells(0).Value  第一個欄位的值。

DataGridView 資料清空的語法

加底下這行就搞定了。

DataGridView1.DataSource = Nothing

2012年12月1日 星期六

DataGridView 指定首列顯示

加入底下這行,指定 Row_num 即可讓特定列移至首列。

 DataGridView1.FirstDisplayedScrollingRowIndex = Row_num

DataGridView 指定欄位排序

加入底下這行,修改要排序的欄位,例如第三欄 Columns(3)

從小到大或從大到小,修改為 Ascending 或 Descending

DataGridView1.Sort(DataGridView1.Columns(3), System.ComponentModel.ListSortDirection.Descending)

Richtextbox 搜尋字串並標示背景顏色

    Dim indexOfSearchText As Integer = 0
    Dim start As Integer = 0

    Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click
       
         Dim startindex As Integer

start_search:

        If TextBox1.Text.Length > 0 Then
            startindex = FindMyText(TextBox1.Text.Trim(), start, RichTextBox1.Text.Length)
        Else
            MsgBox("沒有關鍵字")
            GoTo mark_end
        End If

        ' If string was found in the RichTextBox, highlight it
        If startindex >= 0 Then
            ' Set the highlight color
            RichTextBox1.SelectionBackColor = Color.GreenYellow

            ' Find the end index. End Index = number of characters in textbox
            Dim endindex As Integer = TextBox1.Text.Length
            ' Highlight the search string
            RichTextBox1.Select(startindex, endindex)
            ' mark the start position after the position of
            ' last search string
            start = startindex + endindex
            GoTo start_search
        End If
mark_end:
    End Sub

    Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
        ' Set the return value to -1 by default.
        Dim retVal As Integer = -1

        ' A valid starting index should be specified.
        ' if indexOfSearchText = -1, the end of search
        If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
            ' A valid ending index
            If searchEnd > searchStart OrElse searchEnd = -1 Then
                ' Find the position of search string in RichTextBox
                indexOfSearchText = RichTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
                ' Determine whether the text was found in richTextBox1.
                If indexOfSearchText <> -1 Then
                    ' Return the index to the specified search text.
                    retVal = indexOfSearchText
                End If
            End If
        End If
        Return retVal
    End Function


改編自以下網址,感謝原作者。
http://www.dotnetcurry.com/ShowArticle.aspx?ID=146

DateTimePicker 自動預設當天日期

只要在 Form_Load 加入底下這一行,

在載入 Form 時就會自動顯示當天日期。

DateTimePicker1.Value = DateTime.Now