2012년 11월 29일 목요일

c#(WPF)에서 동적으로 컨트롤 만들기

c#(WPF)에서 동적으로 컨트롤 만들기



        private void MakeKeyBoard()
        {
            int iPosXInit = 50;
            int iPosX;
            int iPosY;
            int iRow;
            string[] saKey = { "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=","\\"
                             ,"ㅂ","ㅈ","ㄷ","ㄱ","ㅅ","ㅛ","ㅕ","ㅑ","ㅐ","ㅔ","[","]"
                             ,"ㅁ","ㄴ","ㅇ","ㄹ","ㅎ","ㅗ","ㅓ","ㅏ","ㅣ",";","'"
                             ,"ㅋ","ㅌ","ㅊ","ㅍ","ㅠ","ㅜ","ㅡ",",",".","/"};

            iRow = 1;
            iPosX = iPosXInit;
            iPosY = 100;
            Random RndNo = new Random();
            for (int i = 0; i < 47; i++)
            {
                Button MyButton = new Button();
                int sizeValue = RndNo.Next(50);
                MyButton.Width = 50;
                MyButton.Height = 50;
                // 행에 따른 버튼 위치 변경
                if (i == 14 || i == 26 || i == 37 || i == 47)
                {
                    iPosX = iPosXInit + (int)MyButton.Width / 2 * iRow;
                    iPosY = iPosY + 60;
                    iRow++;
                }
                // ~ 키 처리 X축
                if (i == 0)
                {
                    iPosX = iPosXInit - (int)MyButton.Width;
                }
                iPosX = iPosX + (int)MyButton.Width;
                               
                MyButton.HorizontalAlignment = HorizontalAlignment.Left;
                MyButton.VerticalAlignment = VerticalAlignment.Top;
                MyButton.Margin = new Thickness(iPosX, iPosY, 0, 0);
                MyButton.Name = "MyBtn" + i;
                MyButton.Content = saKey[i];
                MyButton.Click += new RoutedEventHandler(button2_Click);
                MainGround.Children.Add(MyButton);
            }
        }

댓글 없음:

댓글 쓰기