Set form location c# - c#

I would like to know how to set my form position. i have tried to do the following:
this.Location = Point;
or:
Form2.Left = Point.X;
Form2.Top = Point.Y;
Form2.ShowDialog();
This is does not work. What do I do wrong?

private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form();
form2.StartPosition = FormStartPosition.Manual;
form2.Left = 500;
form2.Top = 500;
form2.ShowDialog();
}

to set the position programmatically , you should set StartPosition to FormStartPosition.Manual as shown below :
Form myform = new Form()
{
Size = new Size(200,200),
StartPosition = FormStartPosition.Manual,
Location = new Point(10,10) // or Cursor.Position if you want to set it to cursor position
}

Try this:
private void Form_Load(object sender, EventArgs e)
{
this.SetDesktopLocation(x, y);
}

The first approach of you works anyways.
this.Location = new Point(/*XPosition*/, /*YPosition*/);

You can set it like this:
form1.Location = new Point(4, 370);

// Point(specify location of x, specify location of y)
// with object initializer
var frmUsers = new FrmUsers
{
StartPosition = FormStartPosition.Manual,
Location = new Point(0, 0)
};
// or
var frmUsers = new FrmUsers();
frmUsers.StartPosition = FormStartPosition.Manual;
frmUsers.Location = new Point(0, 0);

Related

access labels created on button click outside event

I am new to C# and programming in general. I was trying to make a simple shopping list app using a windows form application and Visual studio. This is how I am adding items to the list.
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
y = y + 25;
}
The main issue I have is that I can't have another event change the properties of the label. EX:
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
Button deletButton = new Button();
deletButton.Text = "delete";
this.Controls.Add(deletButton);
deletButton.Location = new Point(x + 260, y);
deletButton.Width = 50;
y = y + 25;
}
private void deletButton_Click(object sender, EventArgs e)
{
itemName.Text = "";
}
It says
the name itemName does not exist in the current context
which make sense because it is in a different method.
My main question is, can I make itemName available outside of that method? Or am I totally going about this wrong and have to redesign the program for the ground up?
Let's say that you want to stick with the dynamic addition of controls, as you're doing now, then a simple way is to give it a name and the find it by that name:
// When you're creating it.
itemName.Name = "itemName";
// Finding it.
var itemName = (Label)this.Controls["itemName"];
// Another way to find it.
var itemName = (Label)this.Controls.Find("itemName", true);

GMap.Net marker initially in incorrect position

I have added a marker using GMap with the lat/long specified. When the application starts, the marker is placed in the incorrect position(at the center of the GMap control) and then when I zoom, it then goes to the specified coordinates. Is this a bug in GMap or am I doing something wrong? Here is the code.
GMapOverlay markersOverlay, mo2;
GMarkerGoogle marker, marker5;
GMapOverlay polyOverlay;
List<PointLatLng> points;
GMapRoute gr;
Graphics g;
bool start = true;
double move = .0001;
double lt = 73, lg = -180;
public Form1()
{
AllocConsole();
InitializeComponent();
try
{
System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com");
}
catch
{
gmap.Manager.Mode = AccessMode.CacheOnly;
MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
gmap.MapProvider = GMapProviders.BingHybridMap;
gmap.Position = new PointLatLng(32, -100);
gmap.MinZoom = 3;
gmap.MaxZoom = 15;
gmap.Zoom = 9;
markersOverlay = new GMapOverlay("markers");
mo2 = new GMapOverlay("markers5");
marker5 = new GMarkerGoogle(new PointLatLng(lt, lg), GMarkerGoogleType.orange_small);
g = this.CreateGraphics();
}
private void Form1_Load(object sender, EventArgs e)
{
gmap.DragButton = MouseButtons.Left;
gmap.ShowCenter = false;
points = new List<PointLatLng>();
polyOverlay = new GMapOverlay("polygons");
GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Magenta));
polygon.Stroke = new Pen(Color.Magenta, 2);
}
protected void OnMouseMove(object sender, MouseEventArgs e)
{
PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
MouseLatLong.Text = Convert.ToString(p);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
marker = new GMarkerGoogle(new PointLatLng(double.Parse(LattextBox.Text), double.Parse(LongtextBox.Text)), new Bitmap(#"C:\Users\Vaib\Documents\Visual Studio 2013\Projects\testGmap\testGmap\Resources\wpt.png"));
mo2.Markers.Add(marker);
gmap.Overlays.Add(mo2);
marker.ToolTip = new GMapToolTip(marker);
marker.ToolTipText = NametextBox.Text;
marker.ToolTipMode = MarkerTooltipMode.Always;
if (start)
{
gmap.Position = new PointLatLng(marker.Position.Lat, marker.Position.Lng);
start = false;
}
points.Add(new PointLatLng(marker.Position.Lat, marker.Position.Lng));
gr = new GMapRoute(points, "route");
gr.Stroke = new Pen(Color.Magenta, 2);
polyOverlay.Routes.Add(gr);
gmap.Overlays.Add(polyOverlay);
ga = new GMarkerArrow(new PointLatLng(gr.From.Value.Lat, gr.From.Value.Lng));
if (points.Count >= 2)
{
ga.Bearing = (float)final(gr.From.Value.Lat, gr.From.Value.Lng, points[1].Lat, points[1].Lng);
}
markersOverlay.Clear();
markersOverlay.Markers.Add(ga);
gmap.Overlays.Add(markersOverlay);
}
The trick is to first add overlay and then the marker:
gMapControl.Overlays.Add(markersOverlay);
markersOverlay.Markers.Add(marker);
Solution
Like you can read in the comments: Adding
gmap.Overlays.Clear()
at the very beginning of the method
private void SubmitButton_Click(object sender, EventArgs e)
was the answer to his problem.
I'm working in MSVC2010 (C++) on a WinForms app and had the same problem - took most of the afternoon to resolve.
This thread was useful, but I find all you need to do is (sorry it's not C#) is comment out the first time you add the marker - see
// DO NOT ADD... line
// Make marker
WindowsForms::Markers::GMarkerGoogle ^MyMarker;
WindowsForms::Markers::GMarkerGoogleType MyType = safe_cast<WindowsForms::Markers::GMarkerGoogleType>(3); // Blue marker 3
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
// MyOverlay->Markers->Add(MyMarker); // DO NOT ADD THE MARKER!!!
gMapControl1->Overlays->Add(MyOverlay);
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
MyOverlay->Markers->Add(MyMarker);
gMapControl1->Overlays->Add(MyOverlay);
gMapControl1->ReloadMap();

PictureBox Is Nowhere To Be Seen

I've been experimenting with adding elements to Windows Forms dynamically via code.
I need to create a PictureBox element. So, far, I have the following code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
However, when this code is run, I get nothing but the black background which I set earlier. I've looked at many questions similar to mine; and they all say I need to add it to the control, which I have done, yet it still abstains from showing.
I would really appreciate it if you could give me an insight into my wrong-doing.
Thanks, Computo.
You need to set Width and Height Properties of the PictureBox.
Try This:
bgui.Width = 500;
bgui.Height = 500;
Complete Code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Width = 500;
bgui.Height = 500;
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
Turns out that System.Drawing.Point does not translate to the actual pixels on the screen. I will have to investigate how Point translates into pixels.
Here its works perfect. Specify the SizeMode and change the location.
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Location = new System.Drawing.Point(100, 0);
bgui.Name = "pictureBox1";
bgui.SizeMode = PictureBoxSizeMode.AutoSize;
this.Controls.Add(bgui);
}

Cursor Position relative to Application

I know how to get the cursor's position :
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
But this is relative to the screen. How do i get the coordinates relative to my Form?
Use the Control.PointToClient method. Assuming this points to the form in question:
var relativePoint = this.PointToClient(new Point(X, Y));
Or simply:
var relativePoint = this.PointToClient(Cursor.Position);
I would use PointToClient like this:
Point p = yourForm.PointToClient(Cursor.Position);
//if calling it in yourForm class, just replace yourForm with this or simply remove it.
How about trying like this using the Control.PointToClient:-
public Form()
{
InitializeComponent();
panel = new System.Windows.Forms.Panel();
panel.Location = new System.Drawing.Point(90, 150);
panel.Size = new System.Drawing.Size(200, 100);
panel.Click += new System.EventHandler(this.panel_Click);
this.Controls.Add(this.panel);
}
private void panel_Click(object sender, EventArgs e)
{
Point point = panel.PointToClient(Cursor.Position);
MessageBox.Show(point.ToString());
}

Show Dialog box at center of its parent

It's been a mess to show a DialogBox at the center of its parent form. Here is a method to show a dialog.
I am positioning its parent to center but not able to center the DialogBox
private void OpenForm(Object point, Object height, Object width)
{
FormLoading frm = new FormLoading();
Point temp = (Point)point;
Point location = new Point(temp.X + (int)((int)width) / 2,
temp.Y + (int)((int)height) / 2);
frm.Location = location;
frm.ShowDialog();
}
private void btnView_Click(object sender, EventArgs e)
{
try
{
ThreadStart starter= delegate { OpenForm(currentScreenLocation,
this.Height, this.Width); };
Thread t = new Thread(starter);
t.Start();
////// Some functionality here...
t.Abort();
}
catch (Exception)
{
}
}
You might want to check the Form.StartPosition property.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx
something along the lines of:
private void OpenForm(Form parent)
{
FormLoading frm = new FormLoading();
frm.Parent = parent;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog();
}
This of course requires setting the form's parent.
form1.StartPosition = FormStartPosition.CenterScreen;
See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(v=vs.110).aspx
if you are making a custom MessageBox,you can simply put this:
CenterToParent();
in your custom MessageBox formload() method.
In addition, if you want to set up arbitrary location you can use this
FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();
NewForm.Show();
NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;

Categories

Resources