Windows Develop Bookmark and Share   
 index > Windows Forms General > Identify Components in a Form
 

Identify Components in a Form


I have created a normal Windows From.(Form1.cs)

There i have created(draw) 2 lines. like this;

Code Snippet

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.DarkGray, 2);

g.DrawLine(pen, 10, 20, 10, 700);
g.DrawLine(pen, 210, 20, 210, 700);

}


Thats fine.

what i want to do is;
if i drag and drop a Button to the from, it should detect its location and give a message box.

Say;
If the button is in between 2 lines, it should display a MessageBox.
MessageBox.Show("button is between lines");

if the button is outside the 2 lines, no need of a MessageBox.

How can i do this !
need help


Code Snippet

namespace WindowsAppTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
/* foreach (Control c in Controls)
{
if (c is Button)
{
MessageBox.Show("button is between lines");
}
}*/
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.DarkGray, 2);

g.DrawLine(pen, 10, 20, 10, 700);
g.DrawLine(pen, 210, 20, 210, 700);

}



}
}



ugp  Monday, August 13, 2007 4:01 AM

ok,

e.g

if u draw two horizontallines like that,

//g.DrawLine(pen, x1,y1,x2,y2);

g.DrawLine(pen, 0, 0, 50, 0);

g.DrawLine(pen, 0, 10, 50, 10);

then u can test,

if(button.Top > 0 //because first y is 0

&& button.Bottom < 10) //because secondy is 10

{

MessageBox.Show("Button is between two lines");

}

Does it solve?

WIROL

wirol  Monday, August 13, 2007 7:10 AM
yes; thanks for the help!

testing the Y cordinates wan't do.
checking the X coordinates will done the anticipated thing..(need to check in between)


g.DrawLine(pen, 10, 20, 10, 700);
g.DrawLine(pen, 210, 20, 210, 700);


if(button1.Left>10 && button1.Right<210)
{

MessageBox.Show("button is between lines");
}

ugp  Monday, August 13, 2007 9:08 AM

hi

if u definitely know the position of two lines, you can compare the x and y location of these two lines and button by using the properties of button, like button.Top , button.Bottom

WIROL

wirol  Monday, August 13, 2007 5:11 AM
im struggling with that comparing part.
pls provide a code sample if possible.
thanks
ugp  Monday, August 13, 2007 6:13 AM

ok,

e.g

if u draw two horizontallines like that,

//g.DrawLine(pen, x1,y1,x2,y2);

g.DrawLine(pen, 0, 0, 50, 0);

g.DrawLine(pen, 0, 10, 50, 10);

then u can test,

if(button.Top > 0 //because first y is 0

&& button.Bottom < 10) //because secondy is 10

{

MessageBox.Show("Button is between two lines");

}

Does it solve?

WIROL

wirol  Monday, August 13, 2007 7:10 AM
yes; thanks for the help!

testing the Y cordinates wan't do.
checking the X coordinates will done the anticipated thing..(need to check in between)


g.DrawLine(pen, 10, 20, 10, 700);
g.DrawLine(pen, 210, 20, 210, 700);


if(button1.Left>10 && button1.Right<210)
{

MessageBox.Show("button is between lines");
}

ugp  Monday, August 13, 2007 9:08 AM

You can use google to search for other answers

Custom Search

More Threads

• .Net CF form loading problem
• Problem in VS 2005 - c++/cli
• WebBrowser browsing pages with invalid SSL certificate
• richtextbox.zoomfactor
• vb.net form...linking listbox and label problem
• SMTP-email
• Module Load Failed - CAB
• allow only number in textbox
• Problem- Unable to check/uncheck Check box column in datagridview
• ComboBox dropdownwidth in DataGridView Control