Unable to position form to center - vb.net

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

Related

Use Math.NET library for plotting polynomial in vb.net

I have equations that I need to plot in VB.Net. I found this library, MATH.Net. It has a lot of examples with C#.
Can I use this library to plot polynomial functions in VB.NET?
If so, then how? As I couldn't find any examples on the Internet.
If not, then can anyone please guide me on how to plot polynomials on a graph in VB.Net?
I'm using Microsoft Visual Studio 2010, and have very little experience in visual basic (I have made so simple applications like calculators, etc).
Generate some data points and add them to a series on the chart.
For example,
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Function Polynom(x As Double) As Double
Return Math.Pow(x, 3) + Math.Pow(x, 2) + x
End Function
Function Polynom2(x As Double) As Double
Return 3 * Math.Pow(x, 2) + 2 * x + 1
End Function
Sub CreateChart()
Dim xMin = -2.0
Dim xMax = 2.0
Dim nPoints = 21
Dim xInc = (xMax - xMin) / (nPoints - 1)
Dim c As New Chart
c.Size = New Size(Me.Width * 0.9, Me.Height * 0.9)
c.Series.Clear()
Dim ca As New ChartArea With {.Name = "ChartArea1"}
ca.AxisX.Title = "variable"
ca.AxisY.Title = "dependent variable"
ca.AxisX.Minimum = xMin
ca.AxisX.Maximum = xMax
c.ChartAreas.Add(ca)
Dim s1 As New Series
s1.Name = "Polynomial"
s1.MarkerStyle = MarkerStyle.Circle
Dim s2 As New Series With {
.Name = "Derivative",
.MarkerStyle = MarkerStyle.Diamond,
.ChartType = SeriesChartType.Line
}
For i = 0 To nPoints - 1
Dim x = xMin + i * xInc
s1.Points.AddXY(x, Polynom(x))
s2.Points.AddXY(x, Polynom2(x))
Next
c.Series.Add(s1)
c.Series.Add(s2)
c.Series("Polynomial").ChartType = SeriesChartType.Line
Dim lgnd As New Legend With {.Name = "Legend"}
c.Legends.Add(lgnd)
lgnd.DockedToChartArea = "ChartArea1"
lgnd.Docking = Docking.Top Or Docking.Left
s1.Legend = "Legend"
s2.Legend = "Legend"
Me.Controls.Add(c)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(640, 480)
Me.Text = "Simple polynomial plot"
CreateChart()
End Sub
End Class
generates

Resizing existing pdfpage not top centered

All,
I am trying to resize an existing PDF file but the result is not centered, what am I doing wrong? I am using PDFsharp, please see below code:
Private Sub ResizePDF(sourcePDF As String, outPDF As String)
Dim form As XPdfForm = XPdfForm.FromFile(sourcePDF)
Dim doc2 As New PdfSharp.Pdf.PdfDocument()
doc2.Options.CompressContentStreams = True
doc2.Options.ColorMode = PdfSharp.Pdf.PdfColorMode.Rgb
For pagenumber As Integer = 1 To (form.PageCount + 1) - 1
Dim page As PdfSharp.Pdf.PdfPage = doc2.Pages.Add()
form.PageNumber = pagenumber
page.Width = form.PixelWidth + 15.36
page.Height = form.PixelHeight + 40.32
Dim graph As XGraphics = XGraphics.FromPdfPage(page)
Dim rect As New XRect(0, 0, page.Width, page.Height)
graph.DrawImage(form, rect)
Next
If IO.Directory.Exists(IO.Path.GetDirectoryName(outPDF)) = False Then IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(outPDF))
doc2.Save(outPDF)
End Sub
Input file: https://www.dropbox.com/s/po87x8c44r90py2/HFD_20151007_0_001_061.pdf?dl=0
Ouput File: https://www.dropbox.com/s/pt5pcyfayri6ory/HFD_20151007_0_001_061_output.pdf?dl=0
We have a workaround for you: Instead of calling graph.DrawImage(form, rect) please call
graph.DrawImage(form, rect.X - form.Page.MediaBox.X1, rect.Y + form.Page.MediaBox.Y1, rect.Width, rect.Height).
I'm not sure if this is valid VB.NET, but it works in C# and should work for VB.NET with little or no changes.
This is a workaround needed for PDFsharp 1.50 beta 2 and all earlier versions.
When switching to PDFsharp 1.50 beta 3 or a later version (not yet available), you will have to use the initial code graph.DrawImage(form, rect) again.

Doesn't get a form in CenterScreen programmatically

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()

How to get the value of a textbox from sa MDI form Groupbox?

I'm new to vb.net and always searching for solution. My forms involved are MainMenu, poCustom, and rdlcForm. All are working well until I got a new look with MDI Forms.
My "New" MainMenu are now containing the poCustom into a Groupbox. Which I searched the code as
For Each f As Form In Application.OpenForms
If TypeOf f Is poCustom Then
f.Activate()
Return
End If
Next
Dim ch As New poCustom
ch.TopLevel = False
ch.Visible = True
ch.StartPosition = FormStartPosition.Manual
Dim leftStart As Integer = 1220 - (ch.Width + (SystemInformation.Border3DSize.Width * 2))
Dim topStart As Integer = 670 - (ch.Height + (SystemInformation.Border3DSize.Height * 2))
ch.Location = New Point(leftStart, topStart)
GroupBox1.Controls.Add(ch)
Problem: The rdlcForm(report) cannot get the value of textboxes in poCustom form. Code below:
Private Sub rptPOView2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rptParam1(11) As Microsoft.Reporting.WinForms.ReportParameter
rptParam1(0) = New Microsoft.Reporting.WinForms.ReportParameter("rptDate", poCustom.Label1.Text)
rptParam1(1) = New Microsoft.Reporting.WinForms.ReportParameter("rptREF", poCustom.Label5.Text)
rptParam1(2) = New Microsoft.Reporting.WinForms.ReportParameter("rptCompany", poCustom.CompanyName.Text)
rptParam1(3) = New Microsoft.Reporting.WinForms.ReportParameter("rptQTY", poCustom.txBoxQTY.Text)
rptParam1(4) = New Microsoft.Reporting.WinForms.ReportParameter("rptUOM", poCustom.txBoxUOM.Text)
rptParam1(5) = New Microsoft.Reporting.WinForms.ReportParameter("rptDesciption", poCustom.txBoxDesc.Text)
rptParam1(6) = New Microsoft.Reporting.WinForms.ReportParameter("rptUnit", poCustom.txBoxUnit.Text)
rptParam1(7) = New Microsoft.Reporting.WinForms.ReportParameter("rptTotal", poCustom.txBoxTotal.Text)
rptParam1(8) = New Microsoft.Reporting.WinForms.ReportParameter("rptSubTotal", poCustom.Label25.Text)
rptParam1(9) = New Microsoft.Reporting.WinForms.ReportParameter("rptVAT", poCustom.Label26.Text)
rptParam1(10) = New Microsoft.Reporting.WinForms.ReportParameter("rptTotalAmount", poCustom.Label27.Text)
rptParam1(11) = New Microsoft.Reporting.WinForms.ReportParameter("rptRequest", poCustom.Label30.Text)
ReportViewer1.LocalReport.SetParameters(rptParam1)
Me.ReportViewer1.RefreshReport()
TextBox1.Text = poCustom.CompanyName.Text
End Sub
Which happens to work without using MDI Forms. I would like to know the cause of the problem for future use. Thank you in advance!
You are using two different instances of poCustom. ch is the first, and you populate its textboxes. But on the rptPOView2_Load event, you are using the other instance, which contains textboxes that has no value yet. One way to fix the problem is to use poCustom itself and not ch.
poCustom.TopLevel = False
poCustom.Visible = True
poCustom.StartPosition = FormStartPosition.Manual
Dim leftStart As Integer = 1220 - (ch.Width + (SystemInformation.Border3DSize.Width * 2))
Dim topStart As Integer = 670 - (ch.Height + (SystemInformation.Border3DSize.Height * 2))
poCustom.Location = New Point(leftStart, topStart)
GroupBox1.Controls.Add(poCustom)

How to position MDIChild form in bottom right corner of MDIParent?

Looks like a dumb question, but I tried the following (where "Me" is a MDIParent form):
Dim frmNotif As New frmNotifica
With frmNotif
.MdiParent = Me
.StartPosition = FormStartPosition.Manual
.Location = New Point(ClientSize.Width - .Width, ClientSize.Height - .Height)
.Show()
End With
but it doesn't work.
Ideas?
Assuming you have an "empty" MDI Parent form (no Panels or ToolStrips docked), this should work:
Dim frmNotif As New frmNotifica
With frmNotif
.MdiParent = Me
.StartPosition = FormStartPosition.Manual
Dim leftStart As Integer = Me.ClientSize.Width - (.Width + (SystemInformation.Border3DSize.Width * 2))
Dim topStart As Integer = Me.ClientSize.Height - (.Height + (SystemInformation.Border3DSize.Height * 2))
.Location = New Point(leftStart, topStart)
.Show()
End With
If you have a Panel or a ToolStrip added to the MDIParent, you would have to factor that in to your equation, too.