Ajouter des boutons dynamiquement avec changement de couleurs pour ces boutons (smartdevice)

Description

Ajouter des boutons dynamiquement avec changement de couleurs pour ces boutons (SmartDevice)

ce projet montre comment on peut ajouter des contrôles dynamiquement et de changer la proprité couleur.

ce projet est utilisé dans l'envirenment SmartDevice.

Youssef; che.moor

Source / Exemple :


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceProject1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;
            int h;
            int w;
            int w_b = 0;
            int h_b = 0;

            string txt_dec;
            
            
            w = Screen.PrimaryScreen.Bounds.Width;
            h = Screen.PrimaryScreen.Bounds.Height;
            List<Button> txb= new List<Button>();

            Color myColor = new Color();

            for (i = 0; i <= 100; i++)
            {
                
                Button xb = new Button();
                Font fn = new Font("Tahoma", 6, FontStyle.Regular);
                Size sz = new Size();

                txt_dec = String.Format("{0:x6}", 16711680 + i * 2);//"F400A1"; //
                myColor = HexToColor(txt_dec);
                xb.BackColor = myColor;
                
               sz.Height = 24;
                sz.Width = 32;
                xb.Size = sz;
                
                xb.Text = "1_" + i;
                xb.Click += new EventHandler(NewButton_YYY);
                xb.KeyDown += new KeyEventHandler(NewButton_XXX);
                xb.KeyUp+= new KeyEventHandler(NewButton_XXX);
                
                this.Controls.Add(xb);
                if (i != 0)
                {
                    if (w_b + sz.Width >= w)
                    {
                        w_b = 0;
                        h_b =h_b+ sz.Height;
                        xb.Location = new Point(w_b, h_b);

                    }
                    else
                       xb.Location = new Point(txb[i - 1].Location.X + txb[i - 1].Width, txb[i - 1].Location.Y);
                }
                else
                    xb.Location = new Point(w_b, h_b);

                xb.Font = fn;
                txb.Add(xb);
                w_b = w_b + sz.Width;
            }
        }
        void NewButton_YYY(object sender, EventArgs e)
        {
            Button CurrentButton = (Button)sender;

            CurrentButton.Text = CurrentButton.Text + "_2";
        }

        void NewButton_XXX(object sender, EventArgs e)
        {
            Button CurrentButton = (Button)sender;
            Color c = new Color();
            c = CurrentButton.ForeColor;
            CurrentButton.ForeColor = CurrentButton.BackColor;
            CurrentButton.BackColor = c;
        }

        private Color HexToColor(string sHex)
        {
            int iR = 0;
            int iV = 0;
            int iB = 0;
            iR = Convert.ToInt32(sHex.Substring(0, 2),16);
            iV = Convert.ToInt32(sHex.Substring(2, 2),16);
            iB = Convert.ToInt32(sHex.Substring(4, 2),16);
            return  Color.FromArgb((int)iR, (int)iV, (int)iB);
        }
    }
}

Conclusion :


IMPORTANT

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (cheMoor)