Welcome Guest!
 VB.NET Helper
 Previous Message All Messages Next Message 
VB .NET Helper Newsletter  Rod Stephens
 Apr 27, 2009 13:01 PDT 

Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***

Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========

    VB.NET Contents:
1. New HowTo: Simulate a robot arm with three rotating joints in Visual
Basic .NET
2. New HowTo: Make a DataGridView control use an array of objects for a
data source in Visual Basic .NET
3. New HowTo: Make tooltips remain visible for a very long time in
Visual Basic .NET
4. New HowTo: Create a DataTable with row cell errors, and display it in
a DataGridView in Visual Basic .NET
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Simulate a robot arm with three rotating joints in Visual
Basic .NET
http://www.vb-helper.com/howto_net_robot_arm.html
http://www.vb-helper.com/HowTo/howto_net_robot_arm.zip

See a picture at http://www.vb-helper.com/howto_net_robot_arm.jpg.

The basic idea is to consider the stages of the arm one after another
and use transformations to move from one to the next.

Suppose the three arm segments have lengths L1, L2, and L3 and the
angles of rotation at the three joints are A1, A2, and A3.

The first joint rotates the arm by angle A1 at a fixed shoulder. The
second joint is translated distance L1 away in the direction of the
first arm segment.

The second joint rotates the arm by angle A2 at the "elbow." The third
joint is translated distance L2 away in the direction of the second arm
segment.

The third joint rotates the arm by angle A3 at the "wrist." The end of
the arm is translated distance L3 away in the direction of the third arm
segment.

    -----

To draw the arm, start by drawing the shoulder at the origin. This
example adds a translation first to center the shoulder in the drawing
area.

Next the code applies the rotation A1 at the shoulder and draws a
rectangle representing the first arm segment. There are is one tricky
issue here.

The rotation is <B>prepended</B> to the previous translation so the
rotation occurs before the translation that centers the shoulder.
Imagine the arm in its own world coordinate space with the shoulder at
the origin. The program draws a horizontal rectangle representing the
first arm segment. The transformations then rotate it to the correct
angle and translate it so the shoulder is centered. This puts the first
arm segment in the correct position.

Next the code must draw the elbow joint. To do this, it <B>prepends</B>
a translation of distance L1 in the X direction and 0 in the Y
direction. It then draws the elbow joint at the origin.

Again imagine the arm in world coordinate space and suppose you draw the
elbow at the origin. The new translation moves the joint to (L1, 0). The
rotation of angle A1 rotates the joint to match the rotation of the
first arm segment. Finally the shoulder's translation moves the whole
thing so the base of the first arm segment is centered in the drawing
area.

The code continues like this as many times as necessary to draw each arm
segment and joint. In eachv step, it prepends the new translations and
rotations.

    -----

The following code shows how the program draws the arm. The code is
quite simple; it's understanding the code that's tricky.

The TrackBar controls tbarJoint1, tbarJoint2, and tbarJoint3 let you
adjust the joint angles at run time.

Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
    Const ARM_LENGTH_1 As Integer = 75
    Const ARM_LENGTH_2 As Integer = 50
    Const ARM_LENGTH_3 As Integer = 50

    e.Graphics.Clear(Me.BackColor)

    Dim cx As Double = picCanvas.ClientSize.Width / 2
    Dim cy As Double = picCanvas.ClientSize.Height / 2

    ' Make sure we start from scratch.
    e.Graphics.ResetTransform()

    ' For each stage in the arm, draw and then *prepend* the
    ' new transformation to represent the next arm in the sequence.

    ' Translate to center of form.
    e.Graphics.TranslateTransform(cx, cy)

    ' Draw the shoulder centered at the origin.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the shoulder.
    ' (Negative to make the angle increase counter-clockwise).
    e.Graphics.RotateTransform(-tbarJoint1.Value,
Drawing2D.MatrixOrder.Prepend)

    ' Draw the first arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, ARM_LENGTH_1, 3)

    ' Translate to the end of the first arm.
    e.Graphics.TranslateTransform(ARM_LENGTH_1, 0,
Drawing2D.MatrixOrder.Prepend)

    ' Draw the elbow.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the elbow.
    e.Graphics.RotateTransform(-tbarJoint2.Value,
Drawing2D.MatrixOrder.Prepend)

    ' Draw the second arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, ARM_LENGTH_2, 3)

    ' Translate to the end of the second arm.
    e.Graphics.TranslateTransform(ARM_LENGTH_2, 0,
Drawing2D.MatrixOrder.Prepend)

    ' Draw the wrist.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the wrist.
    e.Graphics.RotateTransform(-tbarJoint3.Value,
Drawing2D.MatrixOrder.Prepend)

    ' Draw the third arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, ARM_LENGTH_3, 3)
End Sub
==========
2. New HowTo: Make a DataGridView control use an array of objects for a
data source in Visual Basic .NET
http://www.vb-helper.com/howto_net_datagrid_array.html
http://www.vb-helper.com/HowTo/howto_net_datagrid_array.zip

Simply build an array of objects and assign it to the DataGridView's
DataSource property. The control automatically displays the values of
each object's public properties.

The following code shows how the program assigns the control to display
an array of Person objects. The code also sets column headers for the
control. (By default the control uses the properties' names.)

' Make the data array.
Dim people(3) As Person
people(0) = New Person("Ann", "Able")
people(1) = New Person("Ben", "Baker")
people(2) = New Person("Cindy", "Carruthers")

' Make the control use the array as its data source.
dgvPeople.DataSource = people

' Make column headings.
dgvPeople.Columns(0).HeaderText = "Given Name"
dgvPeople.Columns(1).HeaderText = "Surname"

Note that a value must be a property not a public field to be visible.
The following code shows how the program defines the Person class's
FirstName property.

Private m_FirstName As String
Public Property FirstName() As String
    Get
        Return m_FirstName
    End Get
    Set(ByVal value As String)
        m_FirstName = value
    End Set
End Property
==========
3. New HowTo: Make tooltips remain visible for a very long time in
Visual Basic .NET
http://www.vb-helper.com/howto_net_permanent_tooltip.html
http://www.vb-helper.com/HowTo/howto_net_permanent_tooltip.zip

The ToolTip control's AutoPopDelay property determines how long a
tooltip remains visible. Unfortunately the tooltip seems to go away
after about 7 seconds regardless.

If you float the mouse over the top row of buttons, the program displays
a normal tooltip. AutoPopDelay is set to 3600000 (1 hour) but the
tooltip goes away after around 7 seconds.

The program uses the following code to display tooltips for the second
row of buttons. When the mouse enters a button, the code calls the
ToolTip's GetToolTip method to get the control's tooltip. It then calls
the Show method to display the tooltip for an hour. This seems to work.

Private Sub Button_ShowTip(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button4.MouseEnter, Button5.MouseEnter,
Button6.MouseEnter
    Dim btn As Button = DirectCast(sender, Button)
    Dim txt As String = ToolTip1.GetToolTip(btn)
    ToolTip1.Show(txt, btn, 3600000)
End Sub

If the user really needs to always see instructions about a control,
however, you might be better off displaying it in a Label or somewhere
other than a tooltip. When you float the mouse over one of the bottom
buttons, the following code displays the text in the button's Tag
property in a label. When the mouse leaves the button, the code clears
the label.

Private Sub Button_ShowLabel(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button7.MouseEnter, Button8.MouseEnter,
Button9.MouseEnter
    Dim btn As Button = DirectCast(sender, Button)
    Dim txt As String = btn.Tag
    If lblTip.Text <> txt Then lblTip.Text = txt
End Sub

Private Sub Button_ClearLabel(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button7.MouseLeave, Button8.MouseLeave,
Button9.MouseLeave
    If lblTip.Text <> "" Then lblTip.Text = ""
End Sub
==========
4. New HowTo: Create a DataTable with row cell errors, and display it in
a DataGridView in Visual Basic .NET
http://www.vb-helper.com/howto_net_datagridview_datatable_errors.html
http://www.vb-helper.com/HowTo/howto_net_datagridview_datatable_errors.zip


See a picture at
http://www.vb-helper.com/howto_net_datagridview_datatable_errors.jpg.

The program first makes a DataTable and fills it with student data.

It then loops through the DataTable's rows. If a row is missing the
student's first or last name, the code flags the row as in error in the
DataTable.

For each row, the code loops through the columns that hold test and quiz
scores. If a cell has a value less than 70, the program flags the cell
as in error in the DataTable.

The code finishes by looping through the rows and columns again,
coloring any cell with a value under 75.

The DataGridView automatically displays an error icon on any row or cell
flagged as in error. If the user hovers the mouse over the icon, a
tooltip appears describing the error.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    ' Make a DataTable.
    Dim students_table As New DataTable("Students")

    ' Add columns.
    students_table.Columns.Add("FirstName", GetType(String))
    students_table.Columns.Add("LastName", GetType(String))
    students_table.Columns.Add("Quiz Average", GetType(String))
    students_table.Columns.Add("Test Average", GetType(String))

    ' Make the combined FirstName/LastName unique.
    Dim first_last_columns() As DataColumn = { _
        students_table.Columns("FirstName"), _
        students_table.Columns("LastName") _
    }
    students_table.Constraints.Add( _
        New UniqueConstraint(first_last_columns))

    ' Make some student data.
    students_table.Rows.Add(New Object() {"Art", "Ant", 74, 77})
    students_table.Rows.Add(New Object() {"", "Bug", 80, 81})
    students_table.Rows.Add(New Object() {"Cid", "Cat", 97, 89})
    students_table.Rows.Add(New Object() {"Deb", "", 82, 76})
    students_table.Rows.Add(New Object() {"Ed", "Eager", 67, 60})
    students_table.Rows.Add(New Object() {"Fran", "Fix", 71, 69})
    students_table.Rows.Add(New Object() {"Gus", "Gantry", 88, 95})
    students_table.Rows.Add(New Object() {"Hil", "Harris", 63, 58})

    ' Set errors.
    For r As Integer = 0 To students_table.Rows.Count - 1
        ' If a student has no first or last name,
        ' flag the row as in error.
        If students_table.Rows(r).Item(0) = "" OrElse _
           students_table.Rows(r).Item(1) = "" _
        Then
            students_table.Rows(r).RowError = "Missing first or last
name"
        End If

        ' Loop over the quiz and test average columns.
        For c As Integer = 2 To 3
            If students_table.Rows(r).Item(c) < 70 Then
                ' Place a cell error in this cell.
                students_table.Rows(r).SetColumnError(c, "Score under
70!")
            End If
        Next c
    Next r

    ' Attach dgvStudents to the DataTable.
    dgvStudents.DataSource = students_table

    ' Shade scores below 75 pink.
    Dim low_score_style As New DataGridViewCellStyle()
    low_score_style.BackColor = Color.Pink
    low_score_style.ForeColor = Color.Red
    For r As Integer = 0 To students_table.Rows.Count - 1
        If students_table.Rows(r).Item("Quiz Average") < 75 Then
            dgvStudents.Item(2, r).Style = low_score_style
        End If
        If students_table.Rows(r).Item("Test Average") < 75 Then
            dgvStudents.Item(3, r).Style = low_score_style
        End If
    Next r
End Sub
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.