0

I've some controls, Textboxes which I am going to put on a PictureBox in run time. I mean i am going to reposition them during run time.

I need to change their parent from winform to picturebox. because i need transparency of textboxes and I can get it only by making that picturebox as it's parent.

But when I change their parent. e.g

textbox1.parent=picturebox1;

It changes their position too.

How can I prevent this from happening? How can I keep their location same as before?

something like this.

textbox1.location=location before...

ASh
  • 34,632
  • 9
  • 60
  • 82
cooder_one
  • 301
  • 1
  • 2
  • 11
  • I am not downvoting this but others may.Anyway,please be a bit more clear.And the tags you added are not equilevant to your needs because i really don't get what u r trying to achieve But i'll make a a guess : If it's winforms and you don't want the control to change it's position,then either you can lock it's location/position or you can set Dock` property.If WPF , then set up your alignments as required – Software Dev Mar 13 '18 at 16:02
  • @zackraiyan how can i lock it's position? – cooder_one Mar 13 '18 at 16:09
  • So it is winforms ? – Software Dev Mar 13 '18 at 16:11
  • Yes! It is a windows form. – cooder_one Mar 13 '18 at 16:11
  • I will post an answer but please edit your post :( – Software Dev Mar 13 '18 at 16:12
  • Ok. I've made some changes. Please have a look. – cooder_one Mar 13 '18 at 16:12
  • If you change the parent of a control it will be very difficult to keep its position because when the control has a left of 10 for example, that 10 is based from its parents left. Changing the parent will keep the control at left 10 that does not changes. But the parent changed so 10 left from the new parent may be very different than 10 left from the prior parent – GuidoG Mar 13 '18 at 16:56
  • https://stackoverflow.com/a/9387562/17034 – Hans Passant Mar 13 '18 at 16:58
  • @HansPassant Thank you for sharing this answer but i really need to use textbox. And by default, the textbox in .net does not support transparency. That is why i use alphblend textbox. which is customized. and i am using the parent property to get the transparency. But thanks again. – cooder_one Mar 13 '18 at 17:54
  • @GuidoG Yes! I understand that problem. that is why, i was thinking that might be there is a solution to match the values of two parents. But no luck :9 – cooder_one Mar 13 '18 at 17:55
  • You are missing the big picture here. That post shows you how to use PointToScreen and PointToClient to translate the Location. Exactly what you need to do when you change the Parent property. That might get you closer to discovering that TextBox cannot support transparency. – Hans Passant Mar 13 '18 at 17:57
  • Yes, using PointToScreen and PointToClient you can convert the location of your control on your first parent, to a location on the new parent, that will look the same again – GuidoG Mar 14 '18 at 07:44
  • @HansPassant GuidoG Thank you very much for the Help. :) – cooder_one Mar 14 '18 at 15:07

1 Answers1

0

This is not tested.Feel free to comment before downvoting

You may have several options.You can use the Dock or Anchor property and set the values as required.

Another way is,on design time,take a note of your Textbox's location.When u change it's parent,set it's location back to it's previous location.E.g.

Textbox1.Location = new Point(127,283); ///use previous location points here
Software Dev
  • 5,368
  • 5
  • 22
  • 45