Hola tengo una pregunta sobre C#

(1/1)

NetFire97:
Lo que quiero hacer es limitar la selección de un GroupBox
en los cuales tengo 3 elementos,pero cuando seleccione 2 se des habiliten los demas.
por favor me pueden ayudar ^_^

Eleкtro:
Hola

Cita de: NetFire97 en  8 Septiembre 2013, 05:36 am

en los cuales tengo 3 elementos

...De que elementos se tratan, o tenemos que adivinarlo xD?.

Bueno, como no has proporcionado la suficiente información te muestro mi solución en VB.NET, requiere el uso de Framework 4.0 3.5 por las extensiones LINQ:

Básicamente lo que tienes que hacer es recorrer la colección de controles del contenedor y comprobar la cantidad de esos controles que están "seleccionados" para "deshabilitar" el resto de controles.

Código
   Private CheckBoxes() As CheckBox
   Private CheckIt As Boolean
 
   Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) _
   Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, _
           CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
 
       CheckBoxes = Me.GroupBox1.Controls.OfType(Of CheckBox)().ToArray
 
       Select Case CheckBoxes.Where(Function(x) x.Checked).Count
           Case Is >= 2 : CheckIt = False
           Case Is < 2 : CheckIt = True
       End Select
 
       For Each ctrl As CheckBox In CheckBoxes.Where(Function(x) Not x.Checked)
           ctrl.Enabled = CheckIt
       Next
 
       CheckBoxes = Nothing
 
   End Sub


Aquí tienes una traducción al vuelo para C#,
convierte el Switch a un par de IF's y te debería funcionar

Código:

si Checkeds es igual o mayor que 2...
  Enable = false
si checkeds es menor que 2...
  Enable = true


Código
 
private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
dynamic Controls = this.GroupBox1.Controls.OfType<CheckBox>();
dynamic Checkeds = Controls.Where(x => x.Checked).Count;
bool Enable = false;
 
switch (Checkeds) {
case  // ERROR: Case labels with binary operators are unsupported : GreaterThanOrEqual
2:
Enable = false;
break;
case  // ERROR: Case labels with binary operators are unsupported : LessThan
2:
Enable = true;
break;
}
 
foreach (CheckBox ctrl in Controls.Where(x => !x.Checked)) {
ctrl.Enabled = Enable;
}
 
}
 
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================

Saludos!

Novlucker:
Cita de: EleKtro H@cker en  8 Septiembre 2013, 08:26 am

Bueno, como no has proporcionado la suficiente información te muestro mi solución en VB.NET, requiere el uso de Framework 4.0 por las extensiones LINQ
3.5 :P


[ELEKTRO] Gracias por la correción NovLucker!

NetFire97:
Pues haci es como lo tengo:
Código
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Supermercado
{
   public partial class Supermercado : Form
   {
       public Supermercado()
       {
           InitializeComponent();
       }
 
       private void radioButton2_CheckedChanged(object sender, EventArgs e)
       {
           pastas.Enabled = true;
           refresco.Enabled = true;
           te.Enabled = true;
 
       }
 
       private void checkBox1_CheckedChanged(object sender, EventArgs e)
       {
 
       }
 
       private void pescado_CheckedChanged(object sender, EventArgs e)
       {
           pastas.Enabled = false;
           refresco.Enabled = false;
           te.Enabled = false;
       }
 
       private void aceptar_Click(object sender, EventArgs e)
       {
           if (pescado.Checked)
               txt.Text = "carne de pescado";
 
           if(hondureño.Checked)
               txt.Text = "carne de hondureño";
 
           if (pollo.Checked)
               txt.Text = "carne de pollo";
 
 
           if (letras.Checked)
               txt.Text =txt.Text + " + sopa de letras";
 
           if (coditos.Checked)
               txt.Text = txt.Text + " + sopa de coditos";
 
           if (numeros.Checked)
               txt.Text = txt.Text + " + sopa de números";
 
           if (zanahorias.Checked)
               txt.Text = txt.Text + " + zanahorias";
 
           if (brocoli.Checked)
               txt.Text = txt.Text + " + brocoli";
 
           if (aselgas.Checked)
               txt.Text = txt.Text + " + aselgas";
 
           if (refresco.Checked)
               txt.Text = txt.Text + " + refresco";
 
           if (agua.Checked)
               txt.Text = txt.Text + " + agua";
 
           if (te.Checked)
               txt.Text = txt.Text + " + te";
 
           if (pay.Checked)
               txt.Text = txt.Text + " + pay";
 
           if (pastel.Checked)
               txt.Text = txt.Text + " + pastel";
 
           if (flan.Checked)
               txt.Text = txt.Text + " + flan";
 
       }
 
       private void pollo_CheckedChanged(object sender, EventArgs e)
       {
           pastas.Enabled = true;
           refresco.Enabled = true;
           te.Enabled = true;
     }
 }
}
 
[img][img]

Navegación

[0] Índice de Mensajes