Doesn't get a form in CenterScreen programmatically - vb.net

I am trying to get a form in a center screen programmatically.
in the codes of Else part is executing perfectly.
Dim X%, Y%
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X = 100 And Y = 100 Then
'Me.StartPosition = FormStartPosition.CenterScreen
Me.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
Else
Me.Location = New System.Drawing.Point(X, Y)
End If
''FrmCommonCodes.FormLocationXYValues()
Public Sub FormLocationXYValues(ByRef X As Double, ByRef Y As Double)
Using MyConnection As OleDb.OleDbConnection = GetConnection(), MyCommand As New OleDb.OleDbCommand("SELECT * FROM options", MyConnection)
If MyConnection.State = ConnectionState.Closed Then MyConnection.Open()
Using MyDataReader As OleDb.OleDbDataReader = MyCommand.ExecuteReader
While MyDataReader.Read
X = MyDataReader("formlocation_x")
Y = MyDataReader("formlocation_y")
Return
End While
End Using
End Using
End Sub

Looking at your code, what you should do is set the StartPosition property for the form to CenterScreen in the designer. Then if the X and Y coordinates match your default the Form will already be in the right place. That will simplify the code, and fix the problem where setting a start position for a form has no effect if the form has already started:
Dim X As Integer, Y As Integer
Call FrmCommonCodes.FormLocationXYValues(X, Y)
If X <> 100 OrElse Y <> 100 Then
'Default is "CenterScreen", if this code does not run
Me.Location = New System.Drawing.Point(X, Y)
End If
Additionally, you may want to move this code to the Form's constructor, or at least a method that be called before the form is shown. Otherwise, you can see form load up CenterScreen and then jump to the desired location afterwards.

Just one line :
Me.CenterToScreen()

Related

Read data from dynamically created text-box

I'm trying to collect data after creating dynamic text-box with vb.net
Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs)
Handles btn_OK_lines_number.Click
Dim i As Integer
Dim x As Integer
Dim Z As Integer
Z = 150
If IsNumeric(txt_lines_number.Text) Then
Int32.TryParse(txt_lines_number.Text, x)
For i = 1 To x
Dim newTB As New TextBox
Dim newLB As New Label
newLB.Name = "lbl_workstation_number_line" & i
newLB.Text = "Nbr Work Station in Line" & i
newLB.Size = New Size(190, 20)
newLB.ForeColor = Color.White
newLB.Font = New Font("consolas", 12, FontStyle.Regular, GraphicsUnit.Pixel)
newLB.Location = New Point(20, Z + i * 30)
newTB.Name = "Textbox" & i
newTB.Size = New Size(170, 20)
newTB.Location = New Point(200, Z + i * 30)
Me.Controls.Add(newTB)
Me.Controls.Add(newLB)
Next
i = i + 1
Else
MessageBox.Show("please enter a number")
txt_lines_number.Text = ""
End If
End Sub
Let's say you just have one row, and only create one TextBox. You set the name here:
newTB.Name = "Textbox" & i
where the resulting TextBox is named Textbox1. The problem is you can't just reference the identifier Textbox1 directly in your code, as you do with txt_lines_number. You can't even reference it as a member of the class (Me.Textbox1). This name didn't exist at compile time, and so it's not an identifier you can use, and it's not a member of the class at all. There was never a matching Dim statement for that name.
What you can do, though, is look again in the Controls collection where you added the TextBox to the form:
Me.Controls("Textbox1")
or
Me.Controls("Textbox1").Text
You may also need to cast the value to a TextBox:
Dim box As TextBox = DirectCast(Me.Controls("Textbox1"), TextBox)
MessageBox.Show(box.Text)
Remember that case matters here.
Further saving this in a DB is out of scope for one question. There are as many ways to do that as there are programmers in the world. You should make your own attempt first, and come back here with a new question when you run into specific problems.
Thank you,
this is my attempt and it is done !
Dim userInput As TextBox = Form1.Controls.Item("TextBox" & i.ToString)
mycommand.Parameters.AddWithValue("#workstation", userInput.Text)
:D
Because you creating dynamic amount of input controls, right tool for the job will be DataGridView control.
Create a class to represent your data
Public Class LineInfo
Public Property Number As Integer
Public Property WorkStationNumber As Integer
End Class
Create `DataGridView in the form designer.
Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs) Handles btn_OK_lines_number.Click
Dim linesAmount As Integer
If Integer.TryParse(txt_lines_number.Text, linesAmount) = False Then
MessageBox.Show("please enter a number")
txt_lines_number.Text = ""
Exit Sub
End If
' Create class instance for every line
Dim lines =
Enumerable.Range(1, linesAmount)
.Select(Function(i) New LineInfo With { .Number = i })
.ToList()
'Set lines as DataSource to the DataGridView
Me.DataGridView1.DataSource = lines
End Sub
DataGridView will display all lines and provide input fields to update work station numbers.
You can access updated lines later by casting DataSource back to the List
Dim lines = DirectCast(Me.DataGridView1.DataSource, List(Of LineInfo))
' Now you can access all data and save it to the database
Dim parameters =
lines.Select(Function(line)
Return new SqlParameter With
{
.ParameterName = $"#workstation{line.Number}",
.SqlDbType = SqlDbType.Int,
.Value = line.WorkStationNumber
}
End Function)
.ToList()
myCommand.Parameters.AddRange(parameters)
You can freely change style, font colors of different columns in the datagridview.

Sending text to projector in VB. NET

I have a need to send text inside richtextbox to projector screen. I have tried to create a new form and make it fullscreen but I cannot yet figure out how I can communicate the text projector as in EasyWorship or PowerPoint. I will appreciate any suggestion. Thanks
I think you don't have to communicate with projector itself.
You can list the available screens, in order to send that form to the correct one (secondary screen). I mean, something like this:
Dim oYourScreen As Screen = nothing
If Screen.AllScreens.Length > 1 Then
For iAux As Integer = 0 To Screen.AllScreens.Length - 1
If Not Screen.AllScreens(iAux).Primary Then
oYourScreen = Screen.AllScreens(iAux)
Exit For
End If
Next
End If
If oYourScreen Is Nothing Then
oYourScreen = Screen.AllScreens(0)
End If
Me.StartPosition = FormStartPosition.Manual
Dim x, y As Integer
x = oYourScreen.Bounds.Location.X + 100 ' adjust as you want
y = oYourScreen.Bounds.Location.X + 30
Me.Location = oYourScreen.Bounds.Location ' or New Point(x, y)

Opening and closing multiple forms from a main form in VB.net

In my project (kind of a screen saver) I have a main form and second form (PicView).
I want to open many instances of the second form and display a picture in it.
I can do that fine, but when I have opened e.g. 4 forms I would like to close the first before opening the next one so that the total number of forms stays at 4.
The problem I have is that the closing routine does not know about the form instances and the debugger tells me to create a new instance, but I would like to use the existing ones.
How can I make the form names visible to the main routine or do I need to pass reference to the created forms?
Thanks
Private Sub btnTest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
gsPic1 = "D:\My Stuff\411CANON\IMG_1161.JPG"
gsPic2 = "D:\My Stuff\411CANON\IMG_1167.JPG"
gsPic3 = "D:\My Stuff\411CANON\IMG_1174.JPG"
gsPic4 = "D:\My Stuff\411CANON\IMG_1178.JPG"
ScreenSavePicIndex(gsPic1, 1)
Application.DoEvents()
ScreenSavePicIndex(gsPic2, 2)
Application.DoEvents()
ScreenSavePicIndex(gsPic3, 3)
Application.DoEvents()
ScreenSavePicIndex(gsPic4, 4)
MsgBox("Now Closing 1")
CloseScreenSaverPic(1)
Application.DoEvents()
MsgBox("Now Closing 3")
CloseScreenSaverPic(3)
Application.DoEvents()
MsgBox("Now Closing 4")
CloseScreenSaverPic(4)
Application.DoEvents()
End Sub
Private Sub CloseScreenSaverPic(ByVal iIndex As Integer)
Dim frmPicViewerScrSav(iIndex) As PicView
frmPicViewerScrSav(iIndex) = New PicView
frmPicViewerScrSav(iIndex).Close()
End Sub
Private Sub ScreenSavePicIndex(ByVal sFilePathandName As String, iIndex As Integer)
Dim iTargetHeight As Integer
Dim iTargetWidth As Integer
Dim dFactorHeight As Double
Dim dFactorWidth As Double
Dim objImage As System.Drawing.Image
Try
objImage = System.Drawing.Image.FromFile(sFilePathandName)
Catch ex As Exception
objImage = Nothing
End Try
Dim frmPicViewerScrSav(iIndex) As PicView
frmPicViewerScrSav(iIndex) = New PicView
Dim dFactor As Double
dFactor = 1
frmPicViewerScrSav(iIndex).FormBorderStyle = Windows.Forms.FormBorderStyle.None
iTargetWidth = Screen.PrimaryScreen.Bounds.Width / giScreenSavePicSize
iTargetHeight = Screen.PrimaryScreen.Bounds.Height / giScreenSavePicSize
'Check if the pic is bigger than what we want to display
If objImage.Width > iTargetWidth Then
'if it is wider then we need to find out how much bigger it is by factor
dFactorWidth = iTargetWidth / objImage.Width
End If
If objImage.Height > iTargetHeight Then
'if it is higher then we need to find out how much bigger it is by factor
dFactorHeight = iTargetHeight / objImage.Height
End If
If dFactorWidth > dFactorHeight Then
dFactor = dFactorWidth
Else
dFactor = dFactorHeight
End If
'Console.WriteLine("Factor is: " & dFactor)
frmPicViewerScrSav(iIndex).Width = objImage.Width * dFactor
frmPicViewerScrSav(iIndex).Height = objImage.Height * dFactor
objImage.Dispose()
Dim r As New Random()
Dim x As Integer = r.Next(Screen.PrimaryScreen.Bounds.Width - frmPicViewerScrSav(iIndex).Width)
Dim y As Integer = r.Next(Screen.PrimaryScreen.Bounds.Height - frmPicViewerScrSav(iIndex).Height)
Dim p As New Point(x, y)
'Console.WriteLine("Putting it at x= " & x & ", y= " & y)
frmPicViewerScrSav(iIndex).Location = p
frmPicViewerScrSav(iIndex).PictureBox1.Hide()
frmPicViewerScrSav(iIndex).PictureBox1.ImageLocation = sFilePathandName
frmPicViewerScrSav(iIndex).PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
frmPicViewerScrSav(iIndex).PictureBox1.Show()
frmPicViewerScrSav(iIndex).Show()
End Sub

set the location of all open forms

this code lets me set the location of the form each time I drag my map.
Private Sub map_OnMapDrag() Handles map.OnMapDrag
If f2c1.Visible Then
f2c1.Location = camera1.LocalPosition + New Point(20, -240)
End If
If f2c2.Visible Then
f2c2.Location = camera2.LocalPosition + New Point(20, -240)
End If
If f2c3.Visible Then
f2c3.Location = camera3.LocalPosition + New Point(20, -240)
End If
End Sub
however, I want it on a public sub..
and this code, which I think gets all the visible forms ..
Dim forms = Application.OpenForms.OfType(Of frmCamera)()
While forms.Count > 0
forms(forms.Count - 1).Visible = True
End While
how can I make it so that all visible forms gets their location everytime I drag it, so that even if I dynamically added another form it won't be a problem. that's my goal.
can you guys fix this for me..
Dim forms = Application.OpenForms.OfType(Of frmCamera)()
While forms.Count > 0
forms(forms.Count - 1).Visible = True
End While
forms.Location = 'location that I want
Try this:
Dim forms As Collections.Generic.IEnumerable(Of frmMain) = Application.OpenForms.OfType(Of frmMain).Where(Function(frm) frm.Visible)
For Each f As Form In forms
f.Location = New Point(0, 0) ' set coordinate as needed
Next

Unable to position form to center

I have a form called Form1. Its set to startup-Position = Center but when executed it opens up somewhere else (At a random position evrytime).
I am working under Windows XP SP3 , using IDE Visual Studio - 2010. Please provide a workaround to this problem.
I have uploaded a sample project showing the above mentioned problem .
Download link:
http://www.6ybh-upload.com/vt5i4z1wz9pl/Light.zip
You have to set:
Form1.StartPosition = FormStartPosition.Manual
Edit:
Here is a working sample:
Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
Me.StartPosition = FormStartPosition.Manual
Me.Location = New System.Drawing.Point(X, Y)
Edit 2:
Here is the improved code based on comments by Hans Passant, (much better):
Dim mainScreen As Screen = Screen.FromPoint(Me.Location)
Dim X As Integer = (mainScreen.WorkingArea.Width - Me.Width) / 2 + mainScreen.WorkingArea.Left
Dim Y As Integer = (mainScreen.WorkingArea.Height - Me.Height) / 2 + mainScreen.WorkingArea.Top
Me.StartPosition = FormStartPosition.Manual
Me.Location = New System.Drawing.Point(X, Y)
Try to use this after resize the screen
Me.Size = New System.Drawing.Size(800, 436)
Me.CenterToScreen()
In your question it isn't quite clear what you have actually tried since there is no such option as "Center" for the StartPosition property of a Form.
However, setting StartPosition to CenterScreen or
Me.StartPosition = FormStartPosition.CenterScreen
if you are doing it programmatically, should get you exactly what you need.
Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.formstartposition.aspx
Here is the solution:
Dim screen__1 As Screen = Screen.FromControl(frm)
Dim workingArea As Rectangle = screen__1.WorkingArea
frm.Location = New Point() With { _
.X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - frm.Width) / 2), _
.Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - frm.Height) / 2) _
}
The Second One:
'frm = is the form object
Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2
Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2
frm.StartPosition = FormStartPosition.Manual
frm.Location = New System.Drawing.Point(X, Y)
For VB.net 2010
put code to form load event
Call CenterToScreen()
this is built in method provided by VS