Tuesday, September 29, 2009

Create Controls At Runtime in C#

//form level declaration
private Button btnClickableButton;
private PictureBox picBox;
private TextBox txtRunningText;
-------------------------------------------------------------

private void btnAddPictureBox_Click(object sender, EventArgs e)
{
   picBox = new PictureBox();
   picBox.Image = Image.FromFile(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg");
   picBox.Top = 5;
   picBox.Left = 5;
   picBox.Width = 100;
   picBox.Height = 100;
   picBox.SizeMode = PictureBoxSizeMode.StretchImage;
   Controls.Add(picBox);
}
-------------------------------------------------------------
private void btnAddText_Click(object sender, EventArgs e)
{
   txtRunningText = new TextBox();
   txtRunningText.Text = "I am running around!";
   txtRunningText.Top = 5;
   txtRunningText.Left = 110;
   Controls.Add(txtRunningText);

}
-------------------------------------------------------------
private void btnClearForm_Click(object sender, EventArgs e)
{
   Controls.Clear();
}
-------------------------------------------------------------
private void btnAddCLickableButton_Click(object sender, EventArgs e)
{
   EventHandler handler = new EventHandler(btnClickableButton_Click);
   btnClickableButton = new Button();
   btnClickableButton.Click += handler;
   btnClickableButton.Text = "Click Me Please!";
   btnClickableButton.Top = 5;
   btnClickableButton.Left = 215;
   Controls.Add(btnClickableButton);
}
-------------------------------------------------------------
private void btnClickableButton_Click(object sender, EventArgs e)
{
   MessageBox.Show("Yippee! You clicked me!");
}
-------------------------------------------------------------
*** end of program ***

Monday, September 21, 2009

The Magic in the Square

for an odd-n magic square, use de la loubere's algorithm which can be expressed as :
(1)begin by placing a 1 in the middle location of the top row:
(2)now put successive integers in an upward-right diagonal path
**(3)if the upward-right movement results in a location outside the boundaries of the square, place the new number at the opposite end of the row or column that would contain the new number, if the rows and columns were not bounded
**(4)if the upward-right square is already occupied, place the new number directly below the current one




convert that to pseudo code first then translate it into any desired programming language.

the following is created using c#.

//form level declaration
    int[,] magicSquare = new int[15, 15];
---------------------------------------------
private void btnGenerate_Click(object sender, EventArgs e)
{
   int dimension = Convert.ToInt32(this.cboSize.Text);
   this.populateMagicSquare(dimension);
   this.generateMagicSquare(dimension);
   this.setDataGrid(dimension);
}
---------------------------------------------
private void populateMagicSquare(int size)
{// populate magicSquare array with zeroes
   for (int row= 0; row < size; row++)
       for (int col = 0; col < size; col++)
         magicSquare[row, col] = 0;
}
---------------------------------------------
private void generateMagicSquare(int size)
{
   int curr_row = 0;
   int curr_col = size / 2;
   int temp_row = curr_row;
   int temp_col = curr_col;

   magicSquare[curr_row, curr_col] = 1; // (1)

   for (int ctr = 2; ctr <= size * size; ctr++)
{
      if (curr_row - 1 >= 0)
          temp_row = curr_row - 1; // (2)
      else
          temp_row = size - 1; // (3)

       if (curr_col + 1 < size)
          temp_col = curr_col + 1;
       else
          temp_col = 0;

       if (magicSquare[temp_row, temp_col] != 0)
          curr_row++; // (4)
       else
       {
          curr_row = temp_row;
          curr_col = temp_col;
       }
       magicSquare[curr_row, curr_col] = ctr;
   }
}
---------------------------------------------
private void setDataGrid(int size)
{
   this.dgvMagicSquare.Rows.Clear();
   this.dgvMagicSquare.ColumnCount = size;
   this.dgvMagicSquare.Rows.Add(size-1);

   for( int row=0; row < size; row++)
       for (int col=0; col < size; col++)
          this.dgvMagicSquare[col, row].Value = magicSquare[row,col].ToString();
}
---------------------------------------------
***end of program***

Extreme Car Make-Over @ Gaisano Mall during Kadayawan

BC Intrams 2009



photos courtesy of sir joel! :) aja BSIT! over-all 3rd placer @ BC Intrams 2009 yey!