VB.NET Displaying All Form on Different Monitor - vb.net

I'm trying to my 3 Forms to three of my Monitors but I can't seem to display the other 2 I can only display Form1 on my Second Monitor. Is it the wrong syntax in my code?
Dim numberofmonitors As Integer = Screen.AllScreens.Length
If numberofmonitors > 1 Then
Me.Bounds = Screen.AllScreens(1).Bounds
ElseIf numberofmonitors > 2 Then
Me.Bounds = Screen.AllScreens(1).Bounds
Form2.Bounds = Screen.AllScreens(2).Bounds
Form2.Show()
ElseIf numberofmonitors > 3 Then
Me.Bounds = Screen.AllScreens(1).Bounds
Form2.Bounds = Screen.AllScreens(2).Bounds
Form2.Show()
Form3.Bounds = Screen.AllScreens(3).Bounds
Form3.Show()
End If
And i also try this command
Dim myScreens() As Screen = Screen.AllScreens
If (myScreens.Length = 3) Then
'Position form 1 in the middle of screen 1
Me.Left = myScreens(0).WorkingArea.Width / 2 - Me.Width / 2
Me.Top = myScreens(0).WorkingArea.Height / 2 - Me.Height / 2
'Position the top left corner of form 2 in the middle of screen 2
Dim myForm2 As New Form2
myForm2.Show()
myForm2.Left = myScreens(0).Bounds.Width + myScreens(1).WorkingArea.Width / 2
myForm2.Top = myScreens(1).WorkingArea.Height / 2
Dim myForm3 As New Form2
myForm3.Show()
myForm3.Left = myScreens(0).Bounds.Width + myScreens(2).WorkingArea.Width / 3
myForm3.Top = myScreens(2).WorkingArea.Height / 3
End If
but the problem is this command is Form3 is not displaying to my 1rst monitor/left side monitor but then the Main form is displaying to my Middle Monitor and Form2 is displaying to my Right Monitor

Me.Bounds = Screen.AllScreens(0).Bounds
Me.StartPosition = FormStartPosition.Manual
Form2.Bounds = Screen.AllScreens(1).Bounds
Form2.Show()
Form2.StartPosition = FormStartPosition.Manual
Form3.Bounds = Screen.AllScreens(2).Bounds
Form3.StartPosition = FormStartPosition.Manual
Form3.Show()
So i did this command someone helped me in reedit thanks anyway guys

Related

Search Access Database in VB.Net Working Folder with Multiple Criteria

Project Context
I am working on a VB.Net project that combines a map creator with a database.It is built in line with these tutorials on YouTube (https://www.youtube.com/watch?v=XV2HFzbKOJI&t=195s).
The idea is to be able to click a square on the grid and have the appropriate database row be presented on the screen.
When the user clicks "Go" (see image at the bottom), this records the X and Y grid co-ordinates (see image top right). I accomplish this with the following Click Event:
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
'Associates click with colour for paintbrush
If MouseX = 1 And MouseY = 22 Then
Paintbrush = 1 'BUILDING
ElseIf MouseX = 5 And MouseY = 22 Then
Paintbrush = 2 'WATER
ElseIf MouseX = 8 And MouseY = 22 Then
Paintbrush = 3 'ROAD
ElseIf MouseX = 11 And MouseY = 22 Then
Paintbrush = 4 'FIELD
ElseIf MouseX = 14 And MouseY = 22 Then
Paintbrush = 5 'WOODLAND
ElseIf MouseX = 18 And MouseY = 22 Then
Paintbrush = 6
End If
If MouseY <= 20 Then
Select Case Paintbrush
Case 0
Case 1 ' Building
Map(mMapX, mMapY, 0) = 1
Case 2 ' Water
Map(mMapX, mMapY, 0) = 2
Case 3 ' Road
Map(mMapX, mMapY, 0) = 3
Case 4 ' Field
Map(mMapX, mMapY, 0) = 4
Case 5 ' Woodland
Map(mMapX, mMapY, 0) = 5
Case 6 'Go
CurrentLocationX = MouseX ' <<< This is the important part here
CurrentLocationY = MouseY
XLocation.Text = CurrentLocationX
YLocation.Text = CurrentLocationY
End Select
End If
End Sub
In the Access database (database="database"/table="twix"), which I have integrated into the project,I have X and Y Columns, one row for each square in the grid.
There is also a Primary Key column that has a unique number for each entry (which is the equivalent of the textbox top left in the 'Binding Navigator').
Problem
So my idea is that I can have it make a query (maybe SELECT. something?) based on the two variables stored in the textboxes at the top-right and spit out the appropriate row ID into the top-left textbox. I would then position this at the end of the click-event.
XLocation.Text = CurrentLocationX
YLocation.Text = CurrentLocationY
The end result is that the click command would make this query and update the "descriptions" of the map-grid location. I think I have all the necessary components here, I am just struggling to link the two systems (the grid and the database) up.
Question
I have looked around online, though I find the answers are all to case specific for me to salvage any code. What do I need to add to my ClickEvent to make the query to Database and refresh the records shown?
Imports System.Data.OleDb
Public Class Form1
'Search Variables
Dim con As New OleDbConnection
[...]
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Open Search Connection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\matth\OneDrive\Desktop\VisualBasic Stuff\GameFinal\Database.accdb"
con.Open()
[...]
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
'Go
CurrentLocationX = MouseX
CurrentLocationY = MouseY
XLocation.Text = CurrentLocationX
YLocation.Text = CurrentLocationY
SearchSQL()
Current.Focus()
SendKeys.Send("{ENTER}")
End Select
End If
End Sub
[...]
Private Sub SearchSQL()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT ID FROM Twix WHERE x =" & CurrentLocationX & " and y =" & CurrentLocationY, con)
da.Fill(dt)
Current.Text = dt.Rows(0).Item(0)
con.Close()
End Sub

Size of labels in a TableLayoutPanel

I've been stuck in this for two hours now. I googled for the answer a lot, and still can't figure out what's wrong with my program.
I have a TableLayoutPanel1 in a form2. This is the code in which I create labels, a list and then add all of those labels to the TableLayOutPanel1:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lbl0, lbl1, lbl11, lbl2, lbl22, lbl3, lbl33, lbl4, lbl44, lbl5, lbl55, lbl6, lbl66, lbl7, lbl77 As New Label
lbl0.Text = "ACCESORIOS"
lbl0.Font = New System.Drawing.Font("MS Reference Sans Serif", 15.75, FontStyle.Bold)
lbl0.Location = New Point(110, 12)
lbl0.AutoSize = True
Me.Controls.Add(lbl0)
lbl1.Text = "Té 180°"
lbl11.Text = Te180
lbl2.Text = "Té 90° Empalme - Codo Triple"
If Global1 <> 0 Then
lbl22.Text = 0
Else
lbl22.Text = Global2 - 1
End If
lbl3.Text = "Soporte 90° T/T"
lbl33.Text = SoporteTT90
lbl4.Text = "Base para tubo de 1 1/2 - 3 agujeros"
lbl44.Text = Int(lbl11.Text) + Int(lbl22.Text) + (lbl33.Text)
lbl5.Text = "Base para tubo de 1 1/2 - 1 agujero"
lbl55.Text = 2
lbl6.Text = "Tapas para base de tubo de 1 1/2"
lbl66.Text = lbl44.Text
lbl7.Text = "Tornillos y Tarugos 5 x 50 y N°8 Ladrillo Hueco"
lbl7.Font = New System.Drawing.Font("MS Reference Sans Serif", 15.75, FontStyle.Bold)
lbl77.Text = (Int(lbl44.Text) + Int(lbl55.Text)) * 3
Dim labellist As New List(Of Label)()
labellist.Add(lbl1)
labellist.Add(lbl2)
labellist.Add(lbl3)
labellist.Add(lbl4)
labellist.Add(lbl5)
labellist.Add(lbl6)
labellist.Add(lbl7)
labellist.Add(lbl11)
labellist.Add(lbl22)
labellist.Add(lbl33)
labellist.Add(lbl44)
labellist.Add(lbl55)
labellist.Add(lbl66)
labellist.Add(lbl77)
Dim h = 0
For i = 0 To 1
For j = 0 To 6
Dim etiqueta As New Label
etiqueta.Text = labellist(h).Text
TableLayoutPanel1.Controls.Add(etiqueta, i, j)
h = h + 1
Next
Next
End Sub
End Class
The problem is that the the full label doesn't appear in the table.[enter image description here][1]
I have tried Autosizing the labels, the tablelayout. Nothing works. It's like there's an invisible line that doesn't let the text of the label get past a line in the table. Any help appreciated. Thanks.
The layout in form2. enter image description here
What happens when I make the font in the TLP 5pts. The whole label appears in two lines. enter image description here
Mainly, you create all those labels, store them in a list. Then all you use them for is to set the text on another new label you create to add to the controls collection.
Here is a shorter, simpler way to do all that with no extra list, and just one scratch label variable for the TLP set:
Dim texts = {"Te 180", "Te 90 - blah blah blah", "Soprte 90 T/T",
"Torillas y salsa", "Torillas y salsa y guacamole"}
Dim lbl As Label
For n As Int32 = 0 To texts.Length - 1
lbl = New Label
lbl.Text = texts(n)
' more important than autozise, probably:
lbl.Dock = DockStyle.Fill
' debug: to see the border for tweaking
lbl.BorderStyle = BorderStyle.FixedSingle
' add the one you created to the controls collection
tlp1.Controls.Add(lbl, 0, n)
Next

VB Net Charts on Non-Selected Tabs Do Not Locate Properly After Resizing Form

I have 5 tabs on my form. Each tab has 4 charts equally sized and spaced. When I resize the form, the following will happen:
Charts on the selected tab (which is visible) will resize and relocate properly (good)
Charts on the non-selected tabs will resize correctly but not relocate properly (bug)
If I select one of the 4 tabs that were not selected (call it tab A), then select any other tab, and back tab A, the charts will then relocate properly
I am not using Anchor or Dock but am doing this via code. I initially tried Anchor (anchoring each chart to the 4 corners of the tab page but it didn't work when I tried resizing them). Dock doesn't apply since I have 4 charts on each tab page.
So here is my code:
Dim origWidth As Integer = 1140
Dim origHeight As Integer = 900
Dim origChrtWidth As Integer = 495
Dim origChrtHeight As Integer = 325
Dim chrt As Chart
Private Sub myForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
' For each tab in tab control
For Each c As Control In Me.tabCharts.Controls
' If tab has child
If c.HasChildren Then
Dim childControl As Control
' For each child control in tab
For Each childControl In c.Controls
chrt = TryCast(childControl, Chart)
' If child control is chart
If (chrt IsNot Nothing) Then
chrt.Width = origChrtWidth + (Me.Width - origWidth) / 2
chrt.Height = origChrtHeight + (Me.Height - origHeight) / 2
Me.Refresh()
' Charts are all named chart11, chart12, ..., chart21, chart22, etc.
' Where 1st # is the tab, 2nd # is the chart
Select Case Integer.Parse(chrt.Name.Substring(chrt.Name.Length - 1, 1))
Case 1
chrt.Left = 7
chrt.Top = 10
Case 2
chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
chrt.Top = 10
Case 3
chrt.Left = 7
chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7
Case 4
chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7
End Select
Me.Refresh()
End If ' If child control is chart
Next ' For each child control in tab
End If ' If tab has child
Next ' For each tab
End Sub
As mentioned, the above only worked properly for the selected tab and its charts. So I added code to handle a tab selection event. If the tab is selected, it should relocate the charts but this also did not work. Only after selecting the non-selected tab, then another tab, and back to the non-selected tab would the charts update properly.
Private Sub tabCharts_Selected(sender As Object, e As TabControlEventArgs) Handles tabCharts.Selected
' For each tab in tab control
For Each c As Control In Me.tabCharts.Controls
' If tab has child
If c.HasChildren And c.TabIndex = tabCharts.SelectedIndex Then
Dim childControl As Control
' For each child control in tab
For Each childControl In c.Controls
chrt = TryCast(childControl, Chart)
' If child control is chart
If (chrt IsNot Nothing) Then
' Charts are all named chart11, chart12, ..., chart21, chart22, etc.
' Where 1st # is the tab, 2nd # is the chart
Select Case Integer.Parse(chrt.Name.Substring(chrt.Name.Length - 1, 1))
Case 1
chrt.Left = 7
chrt.Top = 10
Case 2
chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
chrt.Top = 10
Case 3
chrt.Left = 7
chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7
Case 4
chrt.Left = 7 + origChrtWidth + (Me.Width - origWidth) / 2 + 7
chrt.Top = 10 + origChrtHeight + (Me.Height - origHeight) / 2 + 7
End Select
Me.Refresh()
End If ' If child control is chart
Next ' For each child control in tab
End If ' If tab has child
Next ' For each tab
End Sub
I tried adding a loop to relocate twice. Did not work.
I tried a work-around where upon selecting a tab, the form would size itself by +1 and -1 to resize and relocate the charts on the selected tab. Works for the most part but there is a quick shift when the form does this. Also, when the form is maximized, it cannot be changed in size so I have to set it to normal, change +1 -1, and then back to maximize, which is not quick enough. The user will see an annoying flicker. Here's the code for that.
I would prefer to fix the problem rather than use a work-around. Any help would be greatly appreciated. Thanks!
Private Sub tabCharts_Selected(sender As Object, e As TabControlEventArgs) Handles tabCharts.Selected
Dim flagMax As Boolean
If Me.WindowState = FormWindowState.Maximized Then
Me.WindowState = FormWindowState.Normal
flagMax = True
End If
If Me.Width = 1140 Then
Me.Width = Me.Width + 1
Me.Width = Me.Width - 1
Else
Me.Width = Me.Width - 1
Me.Width = Me.Width + 1
End If
If flagMax = True Then
Me.WindowState = FormWindowState.Maximized
End If
End Sub
I figured out a solution to my problem. Instead of coding the location and size, I added a Table Layout Panel into each Tab Page and placed the 4 charts from each tab into the Panel.
The Table Layout Panel was anchored in all directions while the charts themselves were docked to fill each cell of the Panel. Works like a charm!

How to again display vanished icon from my form?

i am using this code for form display in my form load event but after form load icon of the form vanished i want to show my icon please help me
Here is the code:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'Turning off border
Me.SetBounds(meleft, metop, Me.Width, MinimumSize.Height) 'setting the loaction and the size of the form
For i As Integer = 0 To meheight Step 20
If (Me.Height < meheight) Then
Me.Top = Me.Top - 20
Me.Height = Me.Height + 20
Me.Refresh()
For FadeCount = 40 To 40 Step 20
Me.Opacity = FadeCount / 100
Threading.Thread.Sleep(10)
Next
Else
Exit For
End If
Next
Me.Height = meheight
Me.Top = (msize(1, 1) - Me.Height) / 2 + msize(1, 3)
Me.Opacity = 99
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
At the start of the code you declare:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
This is what is making your border/icon not visible.
In Form1 Properties-->Window Style: Set ControlBox = True.

vb.net layering of controls not working properly

I simply want to add three controls to my form. The first two show up, but the third does not. I do not understand why this behavior is happening. I have tried using .bringToFront(), but I end up with the same results. I also tried using Me.controls.setChildIndex() but that does not help either.
I am at a loss. I must be doing something wrong. Please help.
Thanks.
This is my code:
' Add the label
' ------------------
Dim menu_label As New Label
menu_label.Text = "Menu"
menu_label.Location = New Point(50, 20)
menu_label.Width = 50
menu_label.Font = New Font(main_font, main_font_size, FontStyle.Regular)
menu_label.ForeColor = Color.White
Me.Controls.Add(menu_label)
' create the image
' ---------------
Dim logo As New PictureBox
Dim logo_image As Image
logo_image = My.Resources.logo
logo.Image = logo_image
logo.Width = 30
logo.Height = 30
logo.Left = 5
logo.Top = 0
Me.Controls.Add(logo)
' add a line
' ----------
Dim line As New Panel
line.Height = 1
line.Width = Me.Width
line.BackColor = Color.Red
line.Location = New Point(0, 32)
Me.Controls.Add(line)
' end code
' ---------------------------------
Only the first two items show up no matter which order I put them in. So I either get just the logo and the menu label, or just the logo and the line, or just the line and the menu label. It's nuts!
So I tried:
Me.Controls.SetChildIndex(logo, 0)
Me.Controls.SetChildIndex(menu_label, 1)
Me.Controls.SetChildIndex(line, 2)
Whichever item gets assigned to level 2 never shows up.
I also tested and it worked fine for me. Changed the form backcolor to black so you can see the results better:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Add the label
' ------------------
Dim menu_label As New Label
menu_label.Text = "Menu"
menu_label.Location = New Point(50, 20)
menu_label.Width = 50
'menu_label.Font = New Font(main_font, main_font_size, FontStyle.Regular)
menu_label.ForeColor = Color.White
Me.Controls.Add(menu_label)
' create the image
' ---------------
Dim logo As New PictureBox
Dim logo_image As Image
'logo_image = My.Resources.logo
'logo.Image = logo_image
logo.BackColor = Color.Yellow
logo.Width = 30
logo.Height = 30
logo.Left = 5
logo.Top = 0
Me.Controls.Add(logo)
' add a line
' ----------
Dim line As New Panel
line.Height = 1
line.Width = Me.Width
line.BackColor = Color.Red
line.Location = New Point(0, 32)
Me.Controls.Add(line)
Me.BackColor = Color.Black
End Sub
What else is on your form? Perhaps you could post a screenshot?