Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code.every time i run the program it returns null.can you solve this?
C#
Point P = new Point(64, 46);
           Control locatecontrol =this.GetChildAtPoint(P);
           Button BTN1 = new Button();
           BTN1 = (Button)locatecontrol;
           MessageBox.Show(BTN1.Text);

i have a Button there but again null :|
help me.
i have searched for this but i could not solve my code.
Posted
Comments
Richard MacCutchan 21-Dec-13 4:26am    
I just tested this on my system and it works fine. I suggest you use your debugger to try and determine exactly what exists at that point on your form. You might also like to explain exactly what you are trying to achieve.

It works fine for me, if there is a Button at the appropriate point.
I'd change it slightly and use as instead myself (or you could get casting exceptions):
C#
Point p = new Point(64, 46);
Control c = GetChildAtPoint(p);
Button btn1 = c as Button;
if (btn1 != null)
    {
    MessageBox.Show(btn1.Text);
    }


Assuming there is a button at the point, it should work, but put a breakpoint on the first line of the code and step through looking at the variables - you need to know what GetChildAtPoint is returning is there is a problem.
 
Share this answer
 
Comments
Farshad72 21-Dec-13 4:27am    
i put break point on this line
Control c = GetChildAtPoint(p);
and after that line C is present NULL again.
OriginalGriff 21-Dec-13 4:38am    
Which means there is no control at that point.
So, look at your form in the designer, and (assuming this code is in your form class) drop a new button onto the form. Pick it up with the mouse and drag it around the form - look at the bottom right of the VS window: numbers will be changing. They show you where the top left of the control is - so move it to (roughly) (60,40) and look to see what is beneath that point.
Anything?
Farshad72 21-Dec-13 4:51am    
i changed my code too see that my point is the exact location of my button
Point p = B1.Location;
Control c = GetChildAtPoint(p);
Button btn1 = c as Button;
if (btn1 != null)
{
MessageBox.Show(btn1.Text);
}
agian null :|
OriginalGriff 21-Dec-13 4:58am    
That means there is no control at that location.
Look at your form in the designer: drop a button onto the form, and pick it up with the mouse. Move it around, and look at the bottom right of the VS screen - there are numbers moving. Those are the coordinates of the top left corner of the new button. Move it to roughly (60,40) and look at what is below it.
Anything?
Farshad72 21-Dec-13 5:09am    
Point p = B1.Location;
Control c = GetChildAtPoint(p);
Button btn1 = c as Button;
if (btn1 != null)
{
MessageBox.Show(btn1.Text);
}
again null
you use RightToLeft
change back to LeftToRight or mybe can use


C#
if(this.RightToLeft==System.Windows.Forms.RightToLeft.Yes)
   pos.x=Width-pos.x;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900