Re: Same application in dual monitor by Morten
Morten
Thu Jul 19 08:23:41 CDT 2007
On Thu, 19 Jul 2007 14:56:02 +0200, Amb k <Ambk@discussions.microsoft.co=
m> wrote:
> Hi Morten,
> Can you please elaborate on this. Actually i do not have any idea rega=
rding
> databinding and it's implementation. Also can you please share some ar=
ticles
> related to this. Thanks
>
What DataBinding does is bind a property on a Control to a property on a=
n object. Whenever you change the value of the control, the value of th=
e property on the object will automatically change as well. Likewise ca=
n you programmatically change the value of the property on the object an=
d have it "magically" displayed.
The code below has a MainForm the launches two Form1 on two different sc=
reens. MainForm also passes a MyObject object to the constructor of For=
m1. MyObject will hold the data value of the checkbox (true or false) s=
o MyObject has a property for this, called Checked (the name of the prop=
erty can be anything, but the data type must be identical to the propert=
y of the control).
Form1 specifies that checkBox1 should have a DataBinding and adds a new =
Binding object that mirrors the Checked property of the CheckBox to the =
Checked property of MyObject. DataSourceUpdateMode.OnPropertyChanged te=
lls the DataBinding to update the underlying object immediatly if the co=
ntrol value changes. For TextBoxes this means, whenever a character is =
entered, as opposed to DataSourceUpdateMode.OnValidation, which will cha=
nge the underlying object once you leave the control. DataSourceUpdateM=
ode.Never does just that, any changes in the control will not be sent to=
the object.
I have added code on MyObject that will call any controls bound to a spe=
cific property when the property is changed. Although the code will wor=
k without calling notifyPropertyChanged, if you change the Checked prope=
rty programmatically you will have to use it to have the display update =
itself.
Search for 'C# DataBinding Tutorial' on the web to get you started.
In addition to the code below you need to add a CheckBox called checkBox=
1 to Form1. MainForm and Form1 are Windows forms and MyObject is a clas=
s.
PS! MainForm is set to auto hide and will open if you click on the tray =
icon called "ScreenTest"
// MainForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ScreenTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
TopMost =3D true;
NotifyIcon icon =3D new NotifyIcon();
icon.Icon =3D this.Icon;
icon.Text =3D "ScreenTest";
icon.Visible =3D true;
icon.MouseDown +=3D new MouseEventHandler(icon_MouseDown);
}
void icon_MouseDown(object sender, MouseEventArgs e)
{
if (WindowState !=3D FormWindowState.Minimized)
{
WindowState =3D FormWindowState.Minimized;
Hide();
}
else
{
WindowState =3D FormWindowState.Normal;
Show();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
MyObject myObj =3D new MyObject();
new Form1(myObj, Screen.AllScreens[0].Bounds).Show();
new Form1(myObj, Screen.AllScreens[1].Bounds).Show();
WindowState =3D FormWindowState.Minimized;
Hide();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (WindowState =3D=3D FormWindowState.Minimized)
ShowInTaskbar =3D false;
else
ShowInTaskbar =3D true;
}
}
}
//Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ScreenTest
{
public partial class Form1 : Form
{
MyObject myObj =3D null;
public Form1(MyObject myObj, Rectangle bounds)
{
InitializeComponent();
this.StartPosition =3D FormStartPosition.Manual;
this.myObj =3D myObj;
this.Bounds =3D bounds;
}
protected override void OnLoad(EventArgs e)
{
checkBox1.DataBindings.Add("Checked", myObj, "Checked", fal=
se, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
//MyObject
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ScreenTest
{
public partial class Form1 : Form
{
MyObject myObj =3D null;
public Form1(MyObject myObj, Rectangle bounds)
{
InitializeComponent();
this.StartPosition =3D FormStartPosition.Manual;
this.myObj =3D myObj;
this.Bounds =3D bounds;
}
protected override void OnLoad(EventArgs e)
{
checkBox1.DataBindings.Add("Checked", myObj, "Checked", fal=
se, DataSourceUpdateMode.OnPropertyChanged);
}
}
}
-- =
Happy coding!
Morten Wennevik [C# MVP]