Random Button Text or location

cevik123123

Member
Joined
Oct 28, 2018
Messages
6
Programming Experience
Beginner
Hello , i am making a little game but i cant locate my buttons randomly is there any advice for me ?
C#:
[COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#004085]Orta[/COLOR]()
        {
            [COLOR=#008000]//[/COLOR]
            [COLOR=#008000]// The InitializeComponent() call is required for Windows Forms designer support.[/COLOR]
            [COLOR=#008000]//[/COLOR]
            [COLOR=#191970][B]InitializeComponent[/B][/COLOR]();
            [COLOR=#0000ff][B]for[/B][/COLOR] ([COLOR=#ff0000][B]int[/B][/COLOR] i = [COLOR=#00008b]0[/COLOR]; i < [COLOR=#00008b]40[/COLOR]; i++) {
                [COLOR=#004085]Button[/COLOR] buton = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#004085]Button[/COLOR] {
                Name = [COLOR=#0000ff]"buton"[/COLOR]+i,
                Text  = (i+[COLOR=#00008b]1[/COLOR]).[COLOR=#191970][B]ToString[/B][/COLOR](),
                Height = [COLOR=#00008b]50[/COLOR],
                Width = [COLOR=#00008b]50[/COLOR],
                };
                
                [I]flowLayoutPanel1[/I].Controls.[COLOR=#191970][B]Add[/B][/COLOR](buton);
            }
            
            [COLOR=#008000]//[/COLOR]
            [COLOR=#008000]// [COLOR=#ff0000][B]TODO[/B][/COLOR]: Add constructor code after the InitializeComponent() call.[/COLOR]
            [COLOR=#008000]//[/COLOR]
        }
i just made some buttons but i cant make them random.
C#:
[COLOR=#0000FF][B]for[/B][/COLOR] ([COLOR=#FF0000][B]int[/B][/COLOR] i = [COLOR=#00008B]0[/COLOR]; i < [COLOR=#00008B]40[/COLOR]; i++) {
                Random rnd = new Random();
                [COLOR=#004085]Button[/COLOR] buton = [COLOR=#008B8B][B]new[/B][/COLOR] [COLOR=#004085]Button[/COLOR] {
                Name = [COLOR=#0000FF]"buton"[/COLOR]+i,
                Location = new Point(rnd.Next(1,40)), // this is not working.
                Text  = (i+[COLOR=#00008B]1[/COLOR]).[COLOR=#191970][B]ToString[/B][/COLOR](),
                Height = [COLOR=#00008B]50[/COLOR],
                Width = [COLOR=#00008B]50[/COLOR],
                };
                
                [I]flowLayoutPanel1[/I].Controls.[COLOR=#191970][B]Add[/B][/COLOR](buton);
            }
i try something like this but i cant reach the solution.

i am trying to show the button's text random its hasn't to be location but i cant find anything to solve this problem.
i hope someone has some solve for this problem. Thanks a lot.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,362
Location
Norway
Programming Experience
10+
If you instead make 'i' one random number the buttons will appear as in random order.
 

cevik123123

Member
Joined
Oct 28, 2018
Messages
6
Programming Experience
Beginner
9i couldnt understand very well is there any sample for this? i try to make
C#:
Text = (random.Next(0,40)+i).ToString();
but it doesnt work either.
Screen shot is here
 

Attachments

  • random.GIF
    random.GIF
    36.7 KB · Views: 42

cevik123123

Member
Joined
Oct 28, 2018
Messages
6
Programming Experience
Beginner
i found the problem. i take the
C#:
[COLOR=#004085]Random[/COLOR] rnd = [COLOR=#008b8b][B]new[/B][/COLOR] [COLOR=#004085]Random[/COLOR]();
outside the for and it worked! thanks for your interest.:apple:
 
Top Bottom