Link to home
Start Free TrialLog in
Avatar of G Scott
G ScottFlag for United States of America

asked on

Monitor Selection Form (Multiple Monitors)

So I am trying to create a form that will allow the user to select which monitor they want to open a form on. I loop through all the current monitors and draw a rectangle on the form representing the monitors and resolutions.  

User generated image
A couple things I just can't figure out are:

1)how to add a Label control in the middle of each of the rectangles for the monitor number (shown in drawing above) and

2)how to add a click event to these rectangles. So when the user clicks it then 'chooses' the monitor based on that click.

Here is how I am finding the monitors:

'******************************************************
        Dim currentmonitorcount As Integer = Screen.AllScreens.Length
        Dim currentmonitornames As String = Screen.AllScreens.ToString
        For i = 0 To currentmonitorcount - 1

            Dim mywidth As Integer
            Dim myheight As Integer

            mywidth = Screen.AllScreens(i).WorkingArea.X.ToString / 10
            myheight = Screen.AllScreens(i).WorkingArea.Y.ToString / 10
 
            MsgBox(Screen.AllScreens(i).WorkingArea.Width.ToString & " x " & Screen.AllScreens(i).WorkingArea.Height.ToString)

            mywidth = Screen.AllScreens(i).WorkingArea.Width / 10
            myheight = Screen.AllScreens(i).WorkingArea.Height / 10
            Dim myname As String
            myname = Screen.AllScreens(i).DeviceName.ToString

            DrawRectangle(i * 300, 400, mywidth, myheight, myname)


        Next

******************************************************

 Private Sub DrawRectangle(ByRef left1 As Integer, ByVal top1 As Integer, ByVal width1 As Integer, ByVal height1 As Integer, ByVal display As String)

        Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
        Dim rect1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
        ' Set the form as the parent of the ShapeContainer.
        canvas.Parent = Me
        ' Set the ShapeContainer as the parent of the RectangleShape.
        rect1.Parent = canvas
        ' Set the location and size of the rectangle.
        rect1.Left = left1 + 20
        rect1.Top = top1
        rect1.Width = width1
        rect1.Height = height1
        Dim a As Integer
        Dim b As Integer
        a = (rect1.Top - 65) / 2
        b = (rect1.Left - 15) / 2



 

    End Sub


***************************************************
 Public Sub drawlabel(ByVal thename As String, ByVal counter As Integer)
        Dim lblTask As New Label

        lblTask.Name = "Label" & counter

        lblTask.Size = New Size(150, 20)

        lblTask.Location = New Point(200, counter * 22)

        lblTask.Text = "txtDescription.text"

        Me.Controls.Add(lblTask)



    End Sub



Thanks for any help you can give me on this.  Not sure if this is practical, but now I just want to figure out how to do it.
Avatar of G Scott
G Scott
Flag of United States of America image

ASKER

Ok, I sort of figured out how to do the labels:

Public Sub drawlabel(ByVal thename As String, ByVal counter As Integer, ByVal point1 As Integer, ByVal point2 As Integer)
        Dim lblTask As New Label

        lblTask.Name = "Label" & counter

        lblTask.Size = New Size(100, 20)

        lblTask.Location = New Point(point1, point2)

        lblTask.Text = thename

        Me.Controls.Add(lblTask)



    End Sub

And then I call it from the DrawRectangle sub:

 Private Sub DrawRectangle(ByRef left1 As Integer, ByVal top1 As Integer, ByVal width1 As Integer, ByVal height1 As Integer, ByVal display As String, ByVal color As Color)

        Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
        Dim rect1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
        ' Set the form as the parent of the ShapeContainer.
        canvas.Parent = Me
        ' Set the ShapeContainer as the parent of the RectangleShape.
        rect1.Parent = canvas
        ' Set the location and size of the rectangle.
        rect1.Left = left1 + 20
        rect1.Top = top1
        rect1.Width = width1
        rect1.Height = height1
        rect1.BorderColor = color
        Dim a As Integer
        Dim b As Integer
        a = (rect1.Top + 1)
        b = (rect1.Left + 1)
        drawlabel(display, 1, b, a)




    End Sub

I would like to make it look like my drawing.  And I still need the AddHandler event. I just can't figure it out. It only makes ShapeContainers and not really Rectangles.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G Scott

ASKER

Ha, thanks Idle_Mind. How simple is that? Didn't even think of it. Thank you so much. I got it working quite well.
Avatar of G Scott

ASKER

Just in case someone is coming here to find something like this I will post my code.

Public Class Form1
    Private lblCount As New Label

    Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim currentmonitorcount As Integer = Screen.AllScreens.Length
        Dim currentmonitornames As String = Screen.AllScreens.ToString
        For i = 0 To currentmonitorcount - 1
            Dim mywidth As Integer
            Dim myheight As Integer
            mywidth = Screen.AllScreens(i).WorkingArea.X.ToString / 10
            myheight = Screen.AllScreens(i).WorkingArea.Y.ToString / 10
            mywidth = Screen.AllScreens(i).WorkingArea.Width / 10
            myheight = Screen.AllScreens(i).WorkingArea.Height / 10
            Dim myname As String
            myname = Screen.AllScreens(i).DeviceName.ToString
            If mywidth = 168 And myheight = 105 Then
                'DrawRectangle(i * 300, 400, mywidth, myheight, myname, Red)
                drawButton(i, 100, (i * 300) + 100, 200, mywidth, myheight, myname)
            Else
                'DrawRectangle(i * 300, 400, mywidth, myheight, myname, Black)
                drawButton(i, 1, (i * 300) + 100, 200, mywidth, myheight, myname)
            End If


        Next

    End Sub

    Shared Function ExtractNumbers(ByVal expr As String) As String
        Return String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(expr, "[^\d]"))
    End Function

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs)
        My.Forms.Form2.Show()
        Dim screen As Screen
        screen = screen.AllScreens(sender.name)
        Form2.StartPosition = FormStartPosition.Manual
        Form2.Location = screen.Bounds.Location + New Point(0, 0)
        Form2.Show()


    End Sub

    Public Sub drawButton(ByVal thename As String, ByVal counter As Integer, ByVal point1 As Integer, ByVal point2 As Integer, ByVal width2 As Integer, ByVal height2 As Integer, ByVal display As String)
        Dim lblTask As New Button
        lblTask.Name = thename
        lblTask.Size = New Size(width2, height2)
        lblTask.Location = New Point(point1, point2)
        lblTask.TextAlign = ContentAlignment.MiddleCenter
        Dim myfont As New Font("Sans Serif", 36, FontStyle.Regular)
        lblTask.Font = myfont
        lblTask.Text = ExtractNumbers(display)
        lblTask.BackColor = Chartreuse
        Me.Controls.Add(lblTask)
        AddHandler lblTask.MouseClick, AddressOf Button3_Click

    End Sub

End Class