Advertisement
ivandrofly

Align controls using mod

Aug 7th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApp1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             InitButtons();
  19.         }
  20.  
  21.         private void InitButtons()
  22.         {
  23.             int lastX = textBox1.Location.X;
  24.             Point points = textBox1.Location;
  25.  
  26.             int defaultX = textBox1.Left;
  27.  
  28.             int x = defaultX;
  29.             int y = textBox1.Bottom + 10;
  30.  
  31.             for (int i = 1; i <= 12; i++)
  32.             {
  33.                 // spawn an button
  34.                 Button button = SpawnButton(i.ToString(), $"button{i}");
  35.  
  36.                 button.Location = new Point(x, y);
  37.                 Controls.Add(button);
  38.  
  39.                 // if mod 4 go down couple pixels
  40.                 if (i % 4 == 0)
  41.                 {
  42.                     x = defaultX;
  43.                     y = button.Bottom + 10;
  44.                 }
  45.                 else
  46.                 {
  47.                     x = button.Right + 10;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         public static Button SpawnButton(string text, string name)
  53.         {
  54.             return new Button
  55.             {
  56.                 Name = name,
  57.                 Text = text
  58.             };
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement