Last post Apr 17, 2018 12:46 PM by oned_gk
Apr 17, 2018 09:18 AM|FaridGN|LINK
I'm new to C#. I'm currently working on one form application. I added one picturebox to the form which has the same left and top position with the default Form frame (0,0). The picturebox (which is my board) is square with width and height of 320. I don't
want to add visible square cells with 20, 20, but I assume it has them. I added two buttons to different positions, but they are within imaginative cells ( first button's location, 0,0, second buttons location is 60,60). When first button intersects the second
one, I need to randomize second one to any other cell within picturebox (board). When I do it, it goes to such positions that are not on the certain cell. Say, for ex. 35, 65 left and top accordingly. But I need it to be somewhere on any cell, say 120, 40.
In other words, move with the value that is dividable into 20.
Here is my code for random when first button moves right and intersects with the second:
private void Moveright(object sender, EventArgs e)
firstbtn.Left += firstbtn.Width;
if (firstbtn.Location == secondbtn.Location)
public void Randomsec()
int maxLeftPos = myboard.Width - secondbtn.Width;
int maxTopPos = myboard.Height - secondbtn.Height;
Random rand = new Random();
secondbtn.Left = rand.Next(0, maxLeftPos);
secondbtn.Top = rand.Next(0, maxTopPos);
Note that I posted only questionable part of my code, if you need more, just tell me and I'll post again. Please, help me with this.
Apr 17, 2018 11:54 AM|PatriceSc|LINK
ASP.NET is for web based application. For Windows Forms based application see
https://social.msdn.microsoft.com/Forums/en-US/home to find a suitable forum.
For now my understanding is that you should get a cell number ie maybe rand.Next(0,maxLeftPos/20) and then multiply by 20 so that the produced value is always a multiple of 20 and goes at a "cell" location rather then being placed at any coordinate between
0 and maxLeftPos.
Apr 17, 2018 12:46 PM|oned_gk|LINK
AFAIK, you can random int then i * 20
i=1 => 20
i=3 => 60