c# - How can I know point in form have object or not? -
i want add many buttons in random position form (c#) don't these buttons overlapping, question can know if point empty or there object in point?
try out:
public partial class form1 : form { public form1() { initializecomponent(); } private random r = new random(); private list<button> buttons = new list<button>(); private void button1_click(object sender, eventargs e) { while (buttons.count > 0) { buttons[0].dispose(); buttons.removeat(0); } bool overlapping; for(int = 1; <= 10; i++) { button btn = new button(); btn.text = i.tostring(); this.controls.add(btn); { overlapping = false; btn.location = new point(r.next(this.clientsize.width - btn.width), r.next(this.clientsize.height - btn.height)); foreach(button otherbtn in buttons) { if (btn.bounds.intersectswith(otherbtn.bounds)) { overlapping = true; break; } } } while (overlapping); buttons.add(btn); } } }
Comments
Post a Comment