Baixe o app para aproveitar ainda mais
Prévia do material em texto
Programação com Visual C# Introdução Prof. Sérgio Fred 1 Aplicação Visual 2 Estrutura de GUI Panel Button Form GUI Internal structure containers Form 3 Panel Label Panel Button Label outros … Definir Projeto 4 Novo Projeto 5 Referenciar API’s 6 Adicionar Referências .NET 7 Adicionar Referências .NET / Implementar Form 8 Estrutura de um programa using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using System.Drawing; using System.Windows.Forms; namespace ProjetoConsole5namespace ProjetoConsole5 { class Program : Form { static void Main(string[] args) { Application.Run(new Program()); } } } 9 Execução do programa 10 Windows Application anular o console 11 Declaração de Objetos Visuais using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using System.Drawing; using System.Windows.Forms; namespace ProjetoConsole5namespace ProjetoConsole5 { class Program : Form { private Button button1; private TextBox textbox1; private ListBox listbox1; static void Main(string[] args) { Application.Run(new Program()); } } } 12 Construtor ... class Program : Form { private Button button1; private TextBox textbox1; private ListBox listbox1; private Program() { inicializeComponentes(); } private void inicializeComponentes() { listbox1= new ListBox(); textbox1 = new TextBox(); button1 = new Button(); Controls.AddRange(new Control[] {listbox,1 button1, textbox1}); } static void Main(string[] args) { Application.Run(new Program()); } } } 13 private void inicializeComponentes() { button1 = new Button(); textbox1 = new TextBox(); listbox1 = new ListBox(); // button1 button1.Name ="button1"; button1.Text = "Executar ação"; button1.Location = new Point(200, 40); button1.Size = new Size(200, 20); //textbox1 Construtor private Program() { } inicializeComponentes(); //textbox1 textbox1.Name = "textbox1"; textbox1.Text = "nada aqui..."; textbox1.Location = new Point(200, 80); textbox1.Size = new Size(200, 30); textbox1.Multiline = true; ClientSize = new Size(500, 500); this.CenterToScreen(); Controls.AddRange(new Control[]{button1, textbox1,listbox1}); this.Name = "Form1"; this.Text = "Sistema de Teste"; 14 Atributos do Form //Atributos do form1 ClientSize = new Size(500, 500); StartPosition = FormStartPosition.CenterScreen; Controls.Add(button1); Controls.Add(listbox1); Controls.Add(textbox1); //Controls.AddRange(new Control[] {button1,textbox1,listbox1}); this.Name = "Form1"; this.Text = "Sistema de Teste"; 15 this.Text = "Sistema de Teste"; System.Drawing.Point • Representação para 2-D point • Constructor – Point(int x, int y) • Propriedades 16 • Propriedades – X – get/set of X coordinate – Y – get/set of Y coordinate System.Drawing.Size • Estrutura de largura e altura do objeto • Construtor – Size(int width, int height) • Propriedades 17 • Propriedades – Width – get/set width – Height – get/set height Windows Applications • Depois, atribui propriedades ao label public class GreetingForm : Form { Label greetingLabel; Button cancelButton; … } 18 • Depois, atribui propriedades ao label greetingLabel = new Label(); greetingLabel.Location = new Point(16, 24); greetingLabel.Text = "Hello, World"; greetingLabel.Size = new Size(216, 24); greetingLabel.ForeColor = Color.Black; Evento para Botão 19 Adicionar objetos no Form • Adicionar controles no Form this.Controls.Add(cancelButton); this.Controls.Add(greetingLabel); // ou Controls.AddRange(new Control[] {button1,textbox1,listbox1}); 20 • Método para encerrar aplicação protected void cancelButton_Click( object sender, EventArgs e) { Application.Exit(); } Popular um ListBox • Adicionar valores no ListBox • Converter para String com ToString() for(int i = 0; i < 50; i++) { listBox1.Items.Add("Item " + i.ToString()); 21 listBox1.Items.Add("Item " + i.ToString()); } Saída de mensagem 22 DataGridView 23 Colunas no DataGridView 24 Listar Elementos public partial class Form1 : Form { int[,] vet = new int[100,100]; int C, L; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int Lin = Convert.ToInt32(textBox2.Text); int Col = Convert.ToInt32(textBox3.Text); textBox1.Clear(); for (L = 0; L < Lin; ++L) { for (C = 0; C < Col; ++C) { DataGridViewCell celula = dataGridView1[C, L]; vet[L, C] = Convert.ToInt32(celula.Value); textBox1.AppendText("Posição ["+L+","+C+"]..."+Convert.ToString(vet[L, C]) + "\n"); } } } } } Preencher Matriz com Dados Externos private void button2_Click(object sender, EventArgs e) { textBox1.Clear(); for (int L=0; L < 2; L++) { for (int C = 0; C < 3; C++)for (int C = 0; C < 3; C++) { DataGridViewCell celula = dataGridView1[C, L]; matriz2[L, C] = Convert.ToInt32(celula.Value); textBox1.AppendText("Posição[" + L + "," + C + "]" + Convert.ToString(matriz2[L, C]) + "\n"); } } } Preencher dataViewGrid private void button3_Click(object sender, EventArgs e) { for (L = 0; L < 2; L++) { for (C = 0; C < 2; C++) { DataGridViewCell celula = dataGridView2[C,L]; A[L,C] = Convert.ToInt32(celula.Value); textBox2.AppendText("["+Convert.ToString(A[L,C])+"]"+"\t"); } } } } public partial class Form1 : Form { int L, C; int[,] A = new int[3,5]; int[,] S = new int[3,5] public Form1() { InitializeComponent(); dataGridView2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { //gera vetor de soma, linha-a-linha for (L = 0; L < 3; L++) { for (C = 0; C < 5; C++) } for (C = 0; C < 5; C++) { S[L,C] = A[L,C]; textBox1.AppendText(Convert.ToString(S[L,C] + "\t")); } } //imprime o vetor do somatório dataGridView1.RowCount = S.Length; dataGridView1.ColumnCount = S.Length; for (int L = 0; L < 3; L++) { for (int C = 0; C < 5; C++) { dataGridView1.Rows[L].Cells[C].Value = Convert.ToString(S[L,C]); } Listar Elementos na Matriz TreeView 30 TreeView 31 TreeView treeView1.SelectedNode.FullPath.ToString (); 32 treeView1.CheckBoxes = true; using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { TreeNode tNode ; tNode = treeView1.Nodes.Add("Websites"); treeView1.Nodes[0].Nodes.Add("Net-informations.com"); 33 treeView1.Nodes[0].Nodes.Add("Net-informations.com"); treeView1.Nodes[0].Nodes[0].Nodes.Add("CLR"); treeView1.Nodes[0].Nodes.Add("Vb.net-informations.com"); treeView1.Nodes[0].Nodes[1].Nodes.Add("String Tutorial"); treeView1.Nodes[0].Nodes[1].Nodes.Add("Excel Tutorial"); treeView1.Nodes[0].Nodes.Add("Csharp.net-informations.com"); treeView1.Nodes[0].Nodes[2].Nodes.Add("ADO.NET"); treeView1.Nodes[0].Nodes[2].Nodes[0].Nodes.Add("Dataset"); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(treeView1.SelectedNode.FullPath.ToString ()); } } } TreeView 34 TreeView.Nodes 35 Gerar gráfico com Chart, Visual Studio using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent();InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int[] vetorX = {2,3,4}; int[] vetorY = { 1, 2, 5 }; chart1.Series["Series1"].Points.DataBindXY(vetorX, vetorY); } } } Continua com outros métodos ... 37
Compartilhar