segunda-feira, 17 de dezembro de 2018

C# - Preenchendo um array com textBox

Os números são inseridos num textBox,
que está protegido contra a entrada de caracteres
ou acentos, ou tudo o que não seja dígitos.





//Preenchendo um array com textBox

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 keypress {

    public partial class Form1 : Form {
        public System.Windows.Forms.TextBox textBox;
        int a = 0, i = 0;
        int [ ] arr = new int [ 5 ];

        public System.Windows.Forms.Button button_1;
        public System.Windows.Forms.Button button_2;
        private System.Windows.Forms.ListBox listBox;

        /*============================================================*/
        public void gerarNumeros ( ) {
            button_2 = new System.Windows.Forms.Button ( );
            button_2.Size = new System.Drawing.Size ( 40, 25 );
            //button_2.AutoSize = true;
            button_2.Location = new System.Drawing.Point ( 360, 65 );
            button_2.Text = "&Ok";
            button_2.BackColor = Color.Blue;
            button_2.Click += new EventHandler ( this.okClick );
        }

        /*============================================================*/
        private void okClick ( object sender, EventArgs e ) {
            arr [ a ] = Int32.Parse ( textBox.Text );
            //limpa o campo do TextBox a cada novo click do botão
            textBox.Clear ( );
            //Quando a recebe o quinto clique imprime.
            if ( a == 4 ) {
                for ( i = 0; i < arr.Length; i++ ) {
                    listBox.Items.Add ( arr [ i ] );
                }
            }
            //O a indexa o array a cada click no botão
            a++;
        }

        /*============================================================*/
        public void text_Box ( ) {
            textBox = new System.Windows.Forms.TextBox ( );
            textBox.ForeColor = Color.Red;
            textBox.BackColor = Color.Black;
            textBox.Location = new Point ( 250, 60 );
            //Propriedades de fonte no listBox
            textBox.Font = new Font ( "Times New Roman", 15F,
            System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0 );
            //Colororindo a font do texttBox com rgb
            textBox.ForeColor = Color.FromArgb ( 255, 0, 255 );
        }

        public void textBox1_KeyPress ( object sender, KeyPressEventArgs e ) {
            //Se a tecla digitada não for número e nem backspace
            if ( !char.IsDigit ( e.KeyChar ) && e.KeyChar != 08 ) {
                //Atribui True no Handled para cancelar o evento
                e.Handled = true;
            }           
        }
        /*============================================================*/
        public void list_Box ( ) {
            listBox = new System.Windows.Forms.ListBox ( );
            listBox.Location = new Point ( 250, 100 );
            //Propriedades de fonte no listBox
            listBox.Font = new Font ( "Times New Roman", 15F,
             System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0 );
            //Colororindo a font do listBox com rgb
            listBox.ForeColor = Color.FromArgb ( 255, 0, 255 );
        }

        /*============================================================*/
        public void teste ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Blue, 10 );
            Pen RedPen = new Pen ( Color.Red, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont = new System.Drawing.Font ( "Helvetica", 16,
                FontStyle.Italic );
            Brush myBrush = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "PREENCHENDO LISTBOX COM ARRAY",
                myFont, myBrush, 110, 20 );

        }
        /*============================================================*/
        public void buttonSair ( ) {
            button_1 = new System.Windows.Forms.Button ( );
            button_1.Size = new System.Drawing.Size ( 50, 30 );
            button_1.Location = new System.Drawing.Point ( 285, 210 );
            button_1.Text = "&Sair";
            button_1.BackColor = Color.Green;
            button_1.Click += new EventHandler ( this.sair );
        }
        protected void sair ( object sender, System.EventArgs e ) {
            Application.Exit ( );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "CRIANDO UMA MOLDURA EM C# xxx";
            this.BackColor = Color.LightPink;
            teste ( e );
        }
        /*============================================================*/
        public Form1 ( ) {
            text_Box ( );
            this.textBox.KeyPress += new System.Windows.Forms.
                KeyPressEventHandler ( this.textBox1_KeyPress );
            list_Box ( );
            buttonSair ( );
            gerarNumeros ( );
            this.Controls.Add ( this.textBox );
            this.Controls.Add ( this.listBox );
            this.Controls.Add ( this.button_1 );
            this.Controls.Add ( this.button_2 );
        }
        /*============================================================*/
    }
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}

C# - imprimindo um Array de inteiros num ListBox

Imprimindo um array de inteiros num ListBox




//IMPRIMINDO ARRAY EM LISTBOX
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;
using System.Threading;

namespace keypress {
    public partial class Form1 : Form {
        private System.Windows.Forms.ListBox listBox;
        int i = 0;
        int [ ] arr = new int [ 100 ];
        /*============================================================*/
        public void List_Box ( ) {
            listBox.Location = new Point ( 250, 100 );
            //Propriedades de fonte no listBox
            listBox.Font = new Font ( "Times New Roman", 15F,
             System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0 );
            //Colororindo a font do listBox com rgb
            listBox.ForeColor = Color.FromArgb ( 255, 0, 255 );
        }
        /*============================================================*/
        public void Ini_Array ( ) {
            listBox = new System.Windows.Forms.ListBox ( );
            for ( i = 0; i < arr.Length; i++ ) {
                arr [ i ] = i;
                listBox.Items.Add ( arr [ i ] );
            }
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "IMPRIMINDO ARRAY EM LISTBOX";
            this.BackColor = Color.LightBlue;
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Green, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont = new System.Drawing.Font ( "Helvetica", 14,
                FontStyle.Italic );
            Brush myBrush = new SolidBrush ( System.Drawing.Color.Black );
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            dc.DrawString ( "IMPRIMINDO ARRAY EM LISTBOX",
                myFont, myBrush_1, 140, 15 );
            dc.DrawString ( "Abaixo o array de inteiros ",
             myFont, myBrush, 190, 40 );
        }
        /*============================================================*/
        public Form1 ( ) {
            Ini_Array ( );
            List_Box ( );
            this.Controls.Add ( this.listBox );
        }
    }
    /*============================================================*/
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}