- Generar Números Aleatorios gracias a la clase Random
- Trabajar con Colores gracias a la clase Color
- Generar algunas figuras
Primeramente crearemos una nueva Aplicación de Windows Forms, plasmaremos el siguiente diseño:
Cada control tiene un nombre especifico (el cual es deducible), el codigo es el sgte:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AplicacionesCSharp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
String ColorEjegido;
Random rnd = new Random();
int[,] Matriz = new int[9, 9];
String Figuras;
private void Form1_Load(object sender, EventArgs e)
{
DgvListado.ColumnCount = 9;
DgvListado.RowCount = 9;
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
DgvListado.Columns[i].HeaderText = (i + 1).ToString();
}
GenerarNumerosAleatorios();
DgvListado.ClearSelection();
DgvListado.DefaultCellStyle.SelectionBackColor = DgvListado.DefaultCellStyle.BackColor;
DgvListado.DefaultCellStyle.SelectionForeColor = DgvListado.DefaultCellStyle.ForeColor;
}
private void LimpiandoColor()
{
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
for (int j = 0; j <= Matriz.GetUpperBound(1); j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = Color.White;
}
}
}
private void GenerarNumerosAleatorios()
{
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
for (int j = 0; j <= Matriz.GetUpperBound(1); j++)
{
Matriz[i, j] = (int)(rnd.NextDouble() * 10);
DgvListado.Rows[i].Cells[j].Value = Matriz[i, j];
}
}
}
private void ColoreandoGrilla(String ColorF, String Forma)
{
Color cl = new Color();
switch (ColorF)
{
case "Rojo": cl = Color.Red; break;
case "Amarillo": cl = Color.Yellow; break;
case "Verde": cl = Color.Green; break;
case "Naranja": cl = Color.Orange; break;
case "Azul": cl = Color.Blue; break;
}
if (Forma.Equals("Cuadrado"))
{
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
for (int j = 0; j <= Matriz.GetUpperBound(1); j++)
{
if ((i == Matriz.GetLowerBound(0) || i == Matriz.GetUpperBound(0)) || (j == Matriz.GetLowerBound(1) || j == Matriz.GetUpperBound(1)))
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
}
}
}
if (Forma.Equals("Rombo"))
{
int x, y, a, b;
x = 4;
y = 4;
a = 7;
b = 1;
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
if (x > -1 || x == 0)
{
for (int j = x; j <= y; j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
x = x - 1;
y = y + 1;
}
else
{
for (int j = b; j <= a; j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
a = a - 1;
b = b + 1;
}
}
}
if (Forma.Equals("Triangulo"))
{
int x, y;
x = 4;
y = 4;
for (int i = 0; i <= Matriz.GetUpperBound(0); i++)
{
if (x > -1 || x == 0)
{
for (int j = x; j <= y; j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
x = x - 1;
y = y + 1;
}
}
}
if (Forma.Equals("Lados Internos"))
{
int x, y, i, j;
x = 0;
y = 0;
i = 0; j = x;
for (i = 0; i <= Matriz.GetUpperBound(0); i++)
{
for (j = x; j <= y; j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
x = x + 1;
y = y + 1;
}
x = 8;
y = 0;
for (i = x; i >= 0; i--)
{
for (j = y; j <= y; j++)
{
DgvListado.Rows[i].Cells[j].Style.BackColor = cl;
}
x = x + 1;
y = y + 1;
}
}
}
private void AsignandoColor(object sender, EventArgs e)
{
ColorEjegido = ((RadioButton)sender).Text;
LimpiandoColor();
if (!String.IsNullOrEmpty(Figuras))
{
ColoreandoGrilla(ColorEjegido, Figuras);
}
}
private void AsignarFigura(object sender, EventArgs e)
{
Figuras = ((RadioButton)sender).Text;
LimpiandoColor();
ColoreandoGrilla(ColorEjegido, Figuras);
}
private void btnGenNumAle_Click(object sender, EventArgs e)
{
GenerarNumerosAleatorios();
}
}
}
Seguidamente ejecutaremos el aplicativo, elegiremos el color Amarillo y la Figura Geométrica Cuadrado, nos mostrara así:
Ahora elegiremos el color Azul y la Figura Geométrica Rombo, nos mostrara así:
Ahora elegiremos el color Rojo y la Figura Lados Internos, nos mostrara así:
Puedes descargar el Proyecto desde el sgte link:
No hay comentarios:
Publicar un comentario