Prevent form moved out side of my main sdi form - c#

suppose i have have two sdi form and when apps run then a sdi form show and from there i am showing another sdi form but the problem is i can drga any where the second sdi form which i do not want. in case of MDI form the mdi child form can not be drag out of mdi form boundary.so in my case i want to simulate the same thing. i want no other sdi form can not be drag out form my main sdi form's boundary. so just guide me how to do this.
i could guess that i have to work with form drag event and from there i have to check form top and left but need more suggestion.
private void Form1_Move(object sender, EventArgs e)
{
this.Location = defaultLocation;
}
i try to do it this way but it is not working.
public partial class Form2 : Form
{
Form1 _parent = null;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void Form2_Move(object sender, EventArgs e)
{
if((this.Location.X+this.Width) > _parent.Width)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
}
if ((this.Location.Y + this.Height) > _parent.Height)
{
this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
}
if (this.Location.Y < 0)
{
this.Location = new System.Drawing.Point(this.Location.X);
}
if (this.Location.X < 0)
{
this.Location = new System.Drawing.Point(this.Location.Y);
}
}
}
please guide me where i made the mistake.
thanks
UPDATE
private void Form2_Move(object sender, EventArgs e)
{
int left = this.Left;
int top = this.Top;
if (this.Left < _parent.Left)
{
left = _parent.Left;
}
if (this.Right > _parent.Right)
{
left = _parent.Right - this.Width;
}
if (this.Top < _parent.Top)
{
top = _parent.Top;
}
if (this.Bottom > _parent.Bottom)
{
top = _parent.Bottom - this.Height;
}
this.Location = new Point(left, top);
}

I recomend to you follow the #ProgrammerV5 recomendation. But if you realy need to control the form movement, please see the use of Cursor.Clip property
here are some information: http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property
Also you may need to do a MouseCapture.

Related

GMap.NET and polygon names not getting passed from second form

So basically im trying to make polygons with a name entered from Form2, called Apgabala_nosaukums (it's in my language, sorry for that). I have been trying to debug this, first 2 times the name entered from Form2 did get read and i was able to see that the name was added to the Polygon. But now it is not getting in the fromVisibleChanged anymore, ending in that the polygon is not getting name. Meaning that I cannot get the bool to true, so I could add 4 points and make a square or rectangle area out of them. Any ideas? Basically the btnAdd_Click function is not working properly, rest is working fine. Any ideas?
Form1 (Main form):
namespace GMapTest
{
public partial class Form1 : Form
{
GMapOverlay polygons = new GMapOverlay("polygons");
List<PointLatLng> points = new List<PointLatLng>();
double lat;
double lng;
int clicks = 0;
bool add = false;
string nosaukums;
public Form1()
{
InitializeComponent();
}
private void gMapControl1_Load(object sender, EventArgs e)
{
gmap.MapProvider = GoogleMapProvider.Instance;
GMaps.Instance.Mode = AccessMode.ServerOnly;
gmap.SetPositionByKeywords("Riga, Latvia");
gmap.ShowCenter = false;
gmap.Overlays.Add(polygons);
}
private void gmap_MouseDown(object sender, MouseEventArgs e)
{
if (add == true)
{
if (e.Button == MouseButtons.Left)
{
lat = gmap.FromLocalToLatLng(e.X, e.Y).Lat;
lng = gmap.FromLocalToLatLng(e.X, e.Y).Lng;
clicks += 1;
points.Add(new PointLatLng(lat, lng));
}
if (clicks == 4)
{
GMapPolygon polygon = new GMapPolygon(points, nosaukums);
polygons.Polygons.Add(polygon);
clicks = 0;
points.Clear();
add = false;
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
Apgabala_nosaukums addName = new Apgabala_nosaukums();
addName.ShowDialog();
addName.VisibleChanged += formVisibleChanged;
if (nosaukums != null)
{
this.add = true;
}
}
private void formVisibleChanged(object sender, EventArgs e)
{
Apgabala_nosaukums frm = (Apgabala_nosaukums)sender;
if (!frm.Visible)
{
this.nosaukums = (frm.ReturnText);
frm.Dispose();
}
}
}
}
Form2 (Apgabala_nosaukums):
namespace GMapTest
{
public partial class Apgabala_nosaukums : Form
{
public string ReturnText { get; set; }
public Apgabala_nosaukums()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.txtName.Text;
this.Visible = false;
}
}
}
The problem is in your btnAdd_Click function. When you call ShowDialog your other form is shown and the next line, addName.VisibleChanged += formVisibleChanged; isn't called until you close the new form. ShowDialog shows the form modally, you can't interact with the parent until you close the new form.
There are a couple ways you could fix this.
1) Subscribe to the VisibleChanged event before you show the form,
addName.VisibleChanged += formVisibleChanged;
addName.ShowDialog();
2) Call addName.Show() instead of addName.ShowDialog(). This shows the form in a non-modal way. The event will get subscribed to because execution continues in btnAdd_Click before the new form is closed. But, the parent form will be interactable, not sure if this is desired or not.
3) You could also get rid of the VisibleChanged event stuff and instead do ShowDialog and read the property after. This is what I'd recommend from seeing the code.
private void btnAdd_Click(object sender, EventArgs e)
{
Apgabala_nosaukums addName = new Apgabala_nosaukums();
addName.ShowDialog();
this.nosaukums = addName.ReturnText;
addName.Dispose();
}

open wpf from winform throws object reference not set to an instance of

I have a WPF custom control and I needs to open it from WinForm. I have followed all steps mentioned in http://weblogs.asp.net/jdanforth/open-a-wpf-window-from-winforms and Open WPF window in WindowsForm APP
But still it gives me an object reference not set to an instance of exceptions.
Winform:
private void button1_Click(object sender, EventArgs e)
{
var notificatioinapp = new WpfCustomControlLibrary1.Window1();
ElementHost.EnableModelessKeyboardInterop(notificatioinapp);
notificatioinapp.Show();
}
WPF custom control:
public partial class Window1 : Window
{
public Window1() : base()
{
InitializeComponent();
this.Closed += this.NotificationWindowClosed;
}
public new void Show()
{
this.Topmost = true;
base.Show();
this.Owner = System.Windows.Application.Current.MainWindow;
this.Closed += this.NotificationWindowClosed;
var workingArea = Screen.PrimaryScreen.WorkingArea;
this.Left = workingArea.Right - this.ActualWidth;
double top = workingArea.Bottom - this.ActualHeight;
foreach (Window window in System.Windows.Application.Current.Windows)
{
string windowName = window.GetType().Name;
if (windowName.Equals("NotificationWindow") && window != this)
{
window.Topmost = true;
top = window.Top - window.ActualHeight;
}
}
this.Top = top;
}
private void ImageMouseUp(object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
this.Close();
}
private void DoubleAnimationCompleted(object sender, EventArgs e)
{
if (!this.IsMouseOver)
{
this.Close();
}
}
private void NotificationWindowClosed(object sender, EventArgs e)
{
foreach (Window window in System.Windows.Application.Current.Windows)
{
string windowName = window.GetType().Name;
if (windowName.Equals("NotificationWindow") && window != this)
{
// Adjust any windows that were above this one to drop down
if (window.Top < this.Top)
{
window.Top = window.Top + this.ActualHeight;
}
}
}
}
}
Appreciate any support.
Application.Current is Specific for WPF Application actually. So I think since you are trying to open WPF application from WinForms Application you need to initialize instance of WPF Application first before accessing it.
if ( null == System.Windows.Application.Current )
{
new System.Windows.Application();
}
If this doesn't work try setting Application.Current.MainWindow = this; in loaded event of WPF window.
This should do the fix.
EDIT :
private void button1_Click(object sender, EventArgs e)
{
if (null == System.Windows.Application.Current)
{
new System.Windows.Application();
}
var wpfwindow = new Window();
wpfwindow = new WpfCustomControlLibrary1.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
}

Keeping child form centered in MDI parent

I've created a main form that is an MDI form, and also a child form.
How I can make the child form remain centered in the parent MDI form whenever I "maximize" or "restore" the MDI form?
Let me see if I understand you right, you want your MDI application to open your child form in the center of the Form, then keep it there no matter what you do to resize it. The Mdi interface has its own ideas on how to place forms, the first step is to have the Child form set its position in its load event, then you can keep it in the center by using the Parents Resize event. This is a sample using 2 Forms, see if it is what you are looking for.
Form1 Mdi Parent
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();
}
protected override void OnResize(EventArgs e)
{
CenterForms();
base.OnResize(e);
}
private void CenterForms()
{
foreach (var form in MdiChildren) //This will center all of the Child Forms
{
form.Left = (ClientRectangle.Width - form.Width) / 2;
form.Top = (ClientRectangle.Height - form.Height) / 2;
}
}
}
Form2 Mdi Child
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Left = (MdiParent.ClientRectangle.Width - Width) / 2;
Top = (MdiParent.ClientRectangle.Height - Height)/2;
}
}
You just need to set the child form starting position in center of Parent Like This
your Child Form
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
You can set the child form property "StartPosition" to CenterParent
OR
When you are loading new form, add this code
Form frm = new Form();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(this.Location.X + (this.Width - frm.Width) / 2, this.Location.Y + (this.Height - frm.Height) / 2);
frm.Show(this);

Open childform in upper right corner of mainform

I am trying to open a (non decorated) childform at the upper right corner of my main form no matter if the main form is maximized or at it's normal size.
But no matter how I try I don't get it to open where I want it to.
I found a post that described how to open the form relative to another control in the form, but that didn't work either:
How to display a Modal form in a position relative to the a control in the parent window (opener)
Have tried to search for a few hours now on google for a solution, but either there's no answer (doubdfull) or I am not searching for the tight words combination (more likely).
Could anyone please either point me to a similar question, or help me how to achieve what I am hoping for?
Sounds to me you ought to be using a UserControl that you anchor to the top and right. But let's make a form work. You'll need to wire its Load event so you can move it into the right spot after it rescaled itself. Then you need the main form's LocationChanged and Resize events so you can keep the child form in the right spot.
So a sample program with boilerplate Form1 and Form2 names and a button on Form1 to display the child could look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.button1.Click += button1_Click;
this.Resize += this.Form1_Resize;
this.LocationChanged += this.Form1_LocationChanged;
}
Form child;
private void button1_Click(object sender, EventArgs e) {
if (child != null) return;
child = new Form2();
child.FormClosed += child_FormClosed;
child.Load += child_Load;
child.Show(this);
}
void child_FormClosed(object sender, FormClosedEventArgs e) {
child.FormClosed -= child_FormClosed;
child.Load -= child_Load;
child = null;
}
void child_Load(object sender, EventArgs e) {
moveChild();
}
void moveChild() {
child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0));
}
private void Form1_Resize(object sender, EventArgs e) {
if (child != null) moveChild();
}
private void Form1_LocationChanged(object sender, EventArgs e) {
if (child != null) moveChild();
}
}
Try something like that:
private void button1_Click(object sender, EventArgs e)
{
ChildForm win = new ChildForm();
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Point parentPoint = this.Location;
int parentHeight = this.Height;
int parentWidth = this.Width;
int childHeight = win.Height;
int childWidth = win.Width;
int resultX = 0;
int resultY = 0;
if ((parentPoint.X + parentWidth + childWidth) > screenWidth)
{
resultY = parentPoint.Y;
resultX = parentPoint.X - childWidth;
}
else
{
resultY = parentPoint.Y;
resultX = parentPoint.X + parentWidth;
}
win.StartPosition = FormStartPosition.Manual;
win.Location = new Point(resultX, resultY);
win.Show();
}
I think you should try something like this:
private void buttonOpen_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
//"this" is the parent form
frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y);
}
Simple, easy, and works (for me).

Fading the background while modal dialog is shown

When shutting down a Windows XP system, it displays a modal dialog box while the background fades to a grayscale. I will like to achieve the same effect in any of the programming languages in the tag list. Can anyone help?
This is pretty easy to do with Winforms. You need a borderless maximized window with a gray background whose Opacity you change with a timer. When the fade is done, you can display a dialog that is borderless and uses the TransparencyKey to make its background transparent. Here's a sample main form that implements this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.FromArgb(50, 50, 50);
this.Opacity = 0;
fadeTimer = new Timer { Interval = 15, Enabled = true };
fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
}
void fadeTimer_Tick(object sender, EventArgs e) {
this.Opacity += 0.02;
if (this.Opacity >= 0.70) {
fadeTimer.Enabled = false;
// Fade done, display the overlay
using (var overlay = new Form2()) {
overlay.ShowDialog(this);
this.Close();
}
}
}
Timer fadeTimer;
}
And the dialog:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
this.TransparencyKey = this.BackColor = Color.Fuchsia;
this.StartPosition = FormStartPosition.Manual;
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.Location = new Point((this.Owner.Width - this.Width) / 2, (this.Owner.Height - this.Height) / 2);
}
private void button1_Click(object sender, EventArgs e) {
this.DialogResult = DialogResult.OK;
}
}

Categories

Resources