Hola buen día a todos, me encuentro realizando un cotizador en C# y SQL, decidí crear los controles segun los articulos que se le pueden o no agregar al modelo final, separe cada componente en un grupbox y si llega a tener varias opciones agrege un radio button para que el cliente seleccione el articulo que mas le convenga en su cotizacion, ya me dibuja el formulario tal cual el problema :laugh: es como agrego el precio a el total si no puedo definir los eventos en cada radio button, tengo 2 dias leyendo si existe alguna instancia o algo donde le defina el precio de cada articulo pero no e tenido suerte.
este es el codigo que estoy usando:
int y = 100;
int y2 = 1;
foreach (DataRow row in dt.Rows)
{
GroupBox grupo = new GroupBox();
grupo.Text = row[0].ToString();
grupo.Size = new System.Drawing.Size(1150, 100);
grupo.Location = new System.Drawing.Point(10, y);
this.Controls.Add(grupo);
string comparar = row[1].ToString();
if (comparar == "1")
{
using (SqlConnection con = new SqlConnection(datosConexion))
{
con.Open();
string textoCmd2 = "SELECT Articulo FROM [Norte].[dbo].[Componentes] where Nombre = '" + row[0].ToString() + "'";
SqlCommand cmd2 = new SqlCommand(textoCmd2, con);
cmd2.ExecuteNonQuery();
con.Close();
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(dt2);
}
foreach (DataRow row2 in dt2.Rows)
{
RadioButton btn = new RadioButton();
btn.Text = row2[0].ToString();
btn.Size = new System.Drawing.Size(130, 40);
btn.Location = new System.Drawing.Point(y2, 40);
grupo.Controls.Add(btn);
y2 = y2 + 135;
ToolTip buttonToolTip = new ToolTip();
buttonToolTip.SetToolTip(btn, btn.Name.ToString());
}
dt2.Clear();
y2 = 1;
}
else
{
RadioButton btn = new RadioButton();
btn.Text = row[0].ToString();
btn.Size = new System.Drawing.Size(120, 40);
btn.Location = new System.Drawing.Point(1, 40);
grupo.Controls.Add(btn);
}
y = y + 100;
}
}
catch
{
MessageBox.Show("No hay conexion", "No se puede descargar la informacion del servidor ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Gracias por responder, hasta ponto. ::)
En tiempo de diseño ya debes tener definido el evento (lo que tenga que hacer)... simplemente crea la función que actúa como evento y que reciba los parámetros que recibiría si lo definieras sobre el tipo de control que creas dinámicamente.
Luego al momento de crear el control, lo enganchas al controlador (AddHandler... ) de eventos que creaste durante diseño...
Sencillamente debes utilizar el operador += (el equivalente al método AddHandler que mencionó NEBIRE) ...
- + and += operators (C# reference) | Microsoft Docs (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator)
- How to: Subscribe to and Unsubscribe from Events (C# Programming Guide) (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events)
Muchas Gracias (NEBIRE, Eleкtro) me dieron buena direccion, dejo el codigo por si alguien mas busca este tema:
public Recetas()
{
InitializeComponent();
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
private void Recetas_Load(object sender, EventArgs e)
{
cargador();
}
public void cargador()
{
DataTable dt2 = new DataTable();
try
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(datosConexion))
{
con.Open();
string textoCmd = "SELECT DISTINCT Nombre, Opcion FROM [Norte].[dbo].[Componentes]";
SqlCommand cmd = new SqlCommand(textoCmd, con);
cmd.ExecuteNonQuery();
con.Close();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
int y = 100;
int y2 = 1;
foreach (DataRow row in dt.Rows)
{
GroupBox grupo = new GroupBox();
grupo.Text = row[0].ToString();
grupo.Size = new System.Drawing.Size(1150, 100);
grupo.Location = new System.Drawing.Point(10, y);
MainFLP.Controls.Add(grupo);
string comparar = row[1].ToString();
if (comparar == "1")
{
using (SqlConnection con = new SqlConnection(datosConexion))
{
con.Open();
string textoCmd2 = "SELECT Articulo FROM [Norte].[dbo].[Componentes] where Nombre = '" + row[0].ToString() + "'";
SqlCommand cmd2 = new SqlCommand(textoCmd2, con);
cmd2.ExecuteNonQuery();
con.Close();
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(dt2);
}
foreach (DataRow row2 in dt2.Rows)
{
RadioButton btn = new RadioButton();
btn.Text = row2[0].ToString();
btn.Size = new System.Drawing.Size(130, 40);
btn.Location = new System.Drawing.Point(y2, 40);
grupo.Controls.Add(btn);
y2 = y2 + 135;
ToolTip buttonToolTip = new ToolTip();
buttonToolTip.SetToolTip(btn, btn.Name.ToString());
btn.CheckedChanged += AsignadorRadios;
}
dt2.Clear();
y2 = 1;
}
else
{
RadioButton btn = new RadioButton();
btn.Text = row[0].ToString();
btn.Size = new System.Drawing.Size(120, 40);
btn.Location = new System.Drawing.Point(1, 40);
grupo.Controls.Add(btn);
btn.CheckedChanged += AsignadorRadios;
}
y = y + 100;
}
}
catch
{
MessageBox.Show("No hay conexion", "No se puede descargar la informacion del servidor ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
void AsignadorRadios(object sender, EventArgs e)
{
RadioButton ctrl = sender as RadioButton;
if (ctrl.Checked == true)
{
using (SqlConnection con = new SqlConnection(datosConexion))
{
con.Open();
string textoCmd = "SELECT Costo FROM [Norte].[dbo].[Componentes] where Articulo = '" + ctrl.Text + "'";
SqlCommand cmd = new SqlCommand(textoCmd, con);
string textoCmd2 = "SELECT Cantidad FROM [Norte].[dbo].[Componentes] where Articulo = '" + ctrl.Text + "'";
SqlCommand cmd2 = new SqlCommand(textoCmd2, con);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery(); try
{
Costo = cmd.ExecuteScalar().ToString();
Cantidad = cmd2.ExecuteScalar().ToString();
}
catch
{
using (SqlConnection con3 = new SqlConnection(datosConexion))
{
con3.Open();
string textoCmd3 = "SELECT SUM (Costo * Cantidad) FROM [Norte].[dbo].[Componentes] where Nombre = '" + ctrl.Text + "'";
SqlCommand cmd3 = new SqlCommand(textoCmd3, con3);
cmd3.ExecuteNonQuery();
sumatoria = cmd3.ExecuteScalar().ToString();
neto = neto + Convert.ToDouble(sumatoria);
}
}
con.Close();
}
Multi = Convert.ToDouble(Costo) * Convert.ToDouble(Cantidad);
neto = neto + Multi;
}
else
{
using (SqlConnection con = new SqlConnection(datosConexion))
{
con.Open();
string textoCmd = "SELECT Costo FROM [Norte].[dbo].[Componentes] where Articulo = '" + ctrl.Text + "'";
SqlCommand cmd = new SqlCommand(textoCmd, con);
string textoCmd2 = "SELECT Cantidad FROM [Norte].[dbo].[Componentes] where Articulo = '" + ctrl.Text + "'";
SqlCommand cmd2 = new SqlCommand(textoCmd2, con);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
Costo = cmd.ExecuteScalar().ToString();
Cantidad = cmd2.ExecuteScalar().ToString();
con.Close();
}
Multi = Convert.ToDouble(Costo) * Convert.ToDouble(Cantidad);
neto = neto - Multi;
}
label1.Text = "$" + neto.ToString("N2");
}