none
請問類似SQL 2005 的連線 狀態條如何作?? RRS feed

  • 問題

  •  

    各位達人好,

     

    請問類似SQL 2005 的連線 狀態條如何作??

    就是按下連線後,有一橫條會動,直到連線成功,想要用在AP中的連接資料庫動作,才不會以為當機了。

     

    謝謝您!!

    2008年6月6日 上午 05:46

解答

  • 那個是用不同的執行緒做的。

    .NET 2.0 可以使用 BackgroundWorker 來處理。

     

    橫條的移動是透過 Timer 加上繪圖 API 做出來的。

     

    大概像這樣:

     

    Code Snippet

     

    private void m_timer_Elapsed(object sender, ElapsedEventArgs e)
    {
       Graphics g = this.CreateGraphics();

     

       if (this.m_drawRect.X > this.Width)
        this.m_drawRect.X = -1 * this.m_drawRect.Width;

       int moveLength = this.m_drawRect.X;
       moveLength += 15; // run speed.
       this.m_drawRect.X = moveLength;

       // create new location's rectangle.
       SolidBrush sb = new SolidBrush(Color.White);

       Rectangle rectBorder = this.DisplayRectangle;
       Rectangle rectL = new Rectangle(this.m_drawRect.X,
        this.m_drawRect.Y, this.m_drawRect.Width / 2, this.m_drawRect.Height);
       Rectangle rectR = new Rectangle(this.m_drawRect.X + this.m_drawRect.Width / 2,
        this.m_drawRect.Y, this.m_drawRect.Width / 2, this.m_drawRect.Height);
       LinearGradientBrush lgbL = new LinearGradientBrush(rectL, Color.FromArgb(255, 255, 255),
        Color.FromArgb(255, 142, 33), LinearGradientMode.Horizontal);
       LinearGradientBrush lgbR = new LinearGradientBrush(rectR, Color.FromArgb(255, 142, 33),
        Color.FromArgb(255, 255, 255), LinearGradientMode.Horizontal);

       g.FillRectangle(sb, this.DisplayRectangle);
                g.FillRectangle(lgbL, rectL);
       g.FillRectangle(lgbR, rectR);

     

       rectBorder.Width -= 1;
       rectBorder.Height -= 1;

     

       sb.Dispose();
       lgbL.Dispose();
       lgbR.Dispose();

     

     

    2008年6月6日 上午 05:57
    版主
  • hI,

     

    您可以處理Connection物件的事件, 在CurrentState的內容值為Connecting時顯示Style屬性為Marquee的ProgressBar, 例如:

     

    Private Sub Connection_StateChanged(ByVal sender As Object, ByVal e As StateChangeEventArgs)
            Select Case e.CurrentState
                Case ConnectionState.Connecting

                    ProgressBar1.Style=ProgressBarStyle.Marquee
                Case Else

                    ProgressBar1.Style=ProgressBarStyle.Blocks
            End Select
    End Sub

     

    就可以在程式進行連線時自動顯示一個會動的橫條, 有需要的話還可以改變ProgressBar物件的ForeColor, 使其顯示出更好的效果

     

    tihs

    2008年6月7日 上午 11:49

所有回覆

  • 那個是用不同的執行緒做的。

    .NET 2.0 可以使用 BackgroundWorker 來處理。

     

    橫條的移動是透過 Timer 加上繪圖 API 做出來的。

     

    大概像這樣:

     

    Code Snippet

     

    private void m_timer_Elapsed(object sender, ElapsedEventArgs e)
    {
       Graphics g = this.CreateGraphics();

     

       if (this.m_drawRect.X > this.Width)
        this.m_drawRect.X = -1 * this.m_drawRect.Width;

       int moveLength = this.m_drawRect.X;
       moveLength += 15; // run speed.
       this.m_drawRect.X = moveLength;

       // create new location's rectangle.
       SolidBrush sb = new SolidBrush(Color.White);

       Rectangle rectBorder = this.DisplayRectangle;
       Rectangle rectL = new Rectangle(this.m_drawRect.X,
        this.m_drawRect.Y, this.m_drawRect.Width / 2, this.m_drawRect.Height);
       Rectangle rectR = new Rectangle(this.m_drawRect.X + this.m_drawRect.Width / 2,
        this.m_drawRect.Y, this.m_drawRect.Width / 2, this.m_drawRect.Height);
       LinearGradientBrush lgbL = new LinearGradientBrush(rectL, Color.FromArgb(255, 255, 255),
        Color.FromArgb(255, 142, 33), LinearGradientMode.Horizontal);
       LinearGradientBrush lgbR = new LinearGradientBrush(rectR, Color.FromArgb(255, 142, 33),
        Color.FromArgb(255, 255, 255), LinearGradientMode.Horizontal);

       g.FillRectangle(sb, this.DisplayRectangle);
                g.FillRectangle(lgbL, rectL);
       g.FillRectangle(lgbR, rectR);

     

       rectBorder.Width -= 1;
       rectBorder.Height -= 1;

     

       sb.Dispose();
       lgbL.Dispose();
       lgbR.Dispose();

     

     

    2008年6月6日 上午 05:57
    版主
  • hI,

     

    您可以處理Connection物件的事件, 在CurrentState的內容值為Connecting時顯示Style屬性為Marquee的ProgressBar, 例如:

     

    Private Sub Connection_StateChanged(ByVal sender As Object, ByVal e As StateChangeEventArgs)
            Select Case e.CurrentState
                Case ConnectionState.Connecting

                    ProgressBar1.Style=ProgressBarStyle.Marquee
                Case Else

                    ProgressBar1.Style=ProgressBarStyle.Blocks
            End Select
    End Sub

     

    就可以在程式進行連線時自動顯示一個會動的橫條, 有需要的話還可以改變ProgressBar物件的ForeColor, 使其顯示出更好的效果

     

    tihs

    2008年6月7日 上午 11:49
  • 謝謝兩位 達人的回答!!

     

    那請問connection 物件是指像SqlConnection 的東西嗎?

    以下是我的程式片斷,按下按鈕,在連上資料庫前,ProgressBar7都沒變,還請指導,謝謝您!!

    Private WithEvents m_oDbConnection As SqlConnection

    Private Sub m_oDbConnection_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles m_oDbConnection.StateChange

            'RaiseEvent StateChange(sender, e)
      

            Select Case e.CurrentState
                Case ConnectionState.Executing

                    ProgressBar7.Style = ProgressBarStyle.Marquee
                Case ConnectionState.Connecting

                    ProgressBar7.Style = ProgressBarStyle.Marquee
                Case Else

                    ProgressBar7.Style = ProgressBarStyle.Blocks
            End Select

        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLEXPConn.Click

          

            m_oDbConnection = New SqlConnection(strcon)
            m_oDbConnection.Open()

     

    謝謝您!!

     

    2008年6月9日 上午 10:36
  • HI,

     

    同步作業的話會造成ProgressBar無法顯示, 您可以這樣做:

     

    1) 在類別宣告:

        Private strConn As String = "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;Pooling=False"
        Private WithEvents conn As SqlConnection

        Private t As Thread

     

    2) 處理Button的Click事件

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ProgressBar1.Style = ProgressBarStyle.Marquee

            t = New Thread(AddressOf DoQuery)
            t.Start()
        End Sub

     

    3) 製作DoQuery方法

       Private Sub DoQuery()
            conn = New SqlConnection(strConn)
            Dim strSQL As String = "Select * from Orders"
            Dim cmd As New SqlCommand(strSQL, conn)
            cmd.CommandType = CommandType.Text

            conn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader

            While dr.Read()

            End While
            Thread.Sleep(2000)
            dr.Close()
            cmd.Dispose()
            conn.Close()
            conn.Dispose()
        End Sub

     

    4) 處理Connection物件的StateChange事件

       Private Sub conn_StateChange(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles   

        conn.StateChange
            Select Case e.CurrentState
                Case ConnectionState.Closed
                    ProgressBar1.Style = ProgressBarStyle.Blocks
            End Select
        End Sub

     

    跑馬燈式的ProgressBar就會顯示出來, 直到資料庫查詢結束

     

    tihs

    2008年6月10日 上午 05:59