Sie sind auf Seite 1von 44

Programao com Visual C#

Introduo
Prof. Srgio Fred

Aplicao Visual

Estrutura de GUI
GUI

Internal structure

Form
Button

Form

containers

Panel
Button

Panel

Label

Label

outros

Definir Projeto

Novo Projeto

Referenciar APIs

Adicionar Referncias .NET

Adicionar Referncias .NET /


Implementar Form

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 ProjetoConsole5
{
class Program : Form
{
static void Main(string[] args)
{
Application.Run(new Program());
}
}
}
9

Execuo do programa

10

Windows Application
anular o console

11

Declarao 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 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

Construtor
private Program() {
inicializeComponentes();
}

private void inicializeComponentes()


{
button1 = new Button();
textbox1 = new TextBox();
listbox1 = new ListBox();
// button1
button1.Name ="button1";
button1.Text = "Executar ao";
button1.Location = new Point(200, 40);
button1.Size = new Size(200, 20);
//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";
14
this.Text = "Sistema de Teste";

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

System.Drawing.Point
Representao para 2-D point
Constructor
Point(int x, int y)

Propriedades
X get/set of X coordinate
Y get/set of Y coordinate

16

System.Drawing.Size
Estrutura de largura e altura do objeto
Construtor
Size(int width, int height)

Propriedades
Width get/set width
Height get/set height

17

Windows Applications
public class GreetingForm : Form {
Label
greetingLabel;
Button cancelButton;

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;
18

Evento para Boto

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});

Mtodo para encerrar aplicao


protected void cancelButton_Click(
object sender, EventArgs e)
Application.Exit();
}

{
20

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

Sada de mensagem

22

DataGridView

23

Colunas no DataGridView

24

public partial class Form1 : Form


{

int L, C;
int[,] A = new int[3,5];
int[,] S = new int[3,5]

Preencher dataViewGrid

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++)
{
S[L,C] = A[L,C];
textBox1.AppendText(Convert.ToString(S[L,C] + "\t"));
}
}

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");
}
}
}

//PREENCHE O GRID COM VALORES DA MATRIZ

for (int L = 0; L < 3; L++)


{
dataGridView1.Rows.Add(,,);
for (int C = 0; C < 5; C++)
{
dataGridView1.Rows[L].Cells[C].Value = Convert.ToString(S[L,C]);
}

Listar Elementos

public partial class Form1 : Form


{
int[,] vet = new int[100,100];
int C, L;

Dimensionamento de matriz pelo


usurio

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("Posio ["+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++)
{
DataGridViewCell celula = dataGridView1[C, L];
matriz2[L, C] = Convert.ToInt32(celula.Value);
textBox1.AppendText("Posio[" + L + "," + C + "]" + Convert.ToString(matriz2[L, C]) + "\n");

}
}
}

Listar Elementos na Matriz


private void bt2_click(object sender, EventArgs e)
{
int[,] mat = new int[3,3];
for (int i = 0; i < 3; i++)
{
grid2.Rows.Add("", "", "");
for (int j = 0; j < 3; j++)
{
grid2.Rows[i].Cells[j].Value = Convert.ToString(mat[i,j]);
}
}
}

Outro Form no Projeto


Form1

Form2

...
using System.Drawing;
using System.Windows.Forms;

private void bt3_click(object sender, EventArgs e)


{
Class1 form1 = new Class1();
form1.Show();

namespace datagrid1
{
class Class1 : Form
{
public Class1()
{
inicializar();
}

private void inicializar()


{
this.Text = "2o. formulario";
this.CenterToScreen();
}
}
}

30

Serializao/Desserializao

31

Criar e escrever no arquivo


private void botao1_click(object sender, EventArgs e) {
string Caminho = "C:\\CodeGear_\\arq01.txt";
StreamWriter arquivo = File.CreateText(Caminho);
arquivo.WriteLine(Texto gravado no arquivo...");
arquivo.Close();
}

32

Leitura no arquivo
private void botao2_click(object sender, EventArgs e) {
string Caminho = "C:\\CodeGear_\\arq01.txt";
StreamReader arquivo = File.OpenText(Caminho);
string linhas = arquivo.ReadLine();
tx1.AppendText(linhas);
// while (linha != null)
// {
//
tx1.AppendText(linhas);
//
linha = leitor.ReadLine();
// }
arquivo.Close();
}

33

Pesquisa no Arquivo
StreamReader
public override string Consultar(string nome)
{
StreamReader sr = new StreamReader(@"D:\lientes.txt");

while (!sr.EndOfStream)
{
if (sr.ReadLine().Contains(nome))
{
registro = sr.ReadLine();
}
}
return registro;
}

34

Pesquisa no Arquivo
StreamReader
while (!sr.EndOfStream)
{
var texto = sr.ReadLine();
if (texto.Contains(nome))
{
registro = texto;
}
}

35

Pesquisa no Arquivo

using (System.IO.StreamWriter file = new


System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string line in lines)
{
//se no existir na linha a palavra Second, escreve a linha no arquivo
if (!line.Contains("Second"))
{
file.WriteLine(line);
}
}
}

36

TreeView

37

TreeView

38

TreeView

treeView1.Sele
treeView1.SelectedNode.
tedNode.FullPath.
FullPath.ToString ();
treeView1.CheckBoxes
treeView1.CheckBoxes = true;
true;

39

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");
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 ());
}
}
}

40

TreeView

41

TreeView.Nodes

42

Gerar grfico 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;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
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);
//series1.ChartType = SeriesChartType.Line;
}
}
}

Continua com outros mtodos ...


44

Das könnte Ihnen auch gefallen