BD

Conectare la o baza de date in Acces cu C#

Mai intai realizati o baza de date numita bd2.mdb care sa contina o tabela cu doua campuri cod si nume si pe care o slvati in c: .

Codul C#

1. Ne conectam folosind un obiect Connection
2. Realizam un obiect de comanda Command
3. Executam citirea din baza de date si afisam rezultatele

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

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

private void button1_Click(object sender, EventArgs e)
{

OleDbConnection con = new OleDbConnection (@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\bd2.mdb”);
con.Open();
MessageBox.Show(“M-am conectat”);

OleDbCommand cmd = new OleDbCommand(“Select * from scoala”,con);
OleDbDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{

object cod = reader[“cod”];
object nume = reader[“nume”];
System.Console.WriteLine(“{0}\t{1}”, cod, nume);

}

reader.Close();
con.Close();

}
}
}

Lista de obiecte

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace ImageListExample
{

public class ImageListExample : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button contruieste_lista;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;

public ImageListExample()
{

InitializeComponent();

}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.contruieste_lista = new System.Windows.Forms.Button();
this.imageList1 = new System.Windows.Forms.ImageList this.components);
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();

this.contruieste_lista.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.contruieste_lista.Location = new System.Drawing.Point(23, 248);
this.contruieste_lista.Name = “contruieste_lista”;
this.contruieste_lista.Size = new System.Drawing.Size(120, 32);
this.contruieste_lista.TabIndex = 2;
this.contruieste_lista.Text = “Desene”;
this.contruieste_lista.Click += new System.EventHandler(this.contruieste_lista_Click);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.pictureBox2.Location = new System.Drawing.Point(298, 190);
this.pictureBox2.Name = “pictureBox2″;
this.pictureBox2.Size = new System.Drawing.Size(136, 96);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox2.TabIndex = 4;
this.pictureBox2.TabStop = false;
this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(177, 248);
this.button1.Name = “button1″;
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 5;
this.button1.Text = “Animate”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// ImageListExample
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(950, 312);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.contruieste_lista);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = “ImageListExample”;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
this.Text = “PV6″;
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new ImageListExample());
}

ImageList desene_animate = new System.Windows.Forms.ImageList();

private void contruieste_lista_Click(object sender, System.EventArgs e)
{
// Configureaza lista
desene_animate.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
desene_animate.ImageSize = new System.Drawing.Size(60, 60);
desene_animate.Images.Clear();

string[] gif_uri = Directory.GetFiles(“\\Imagini”, “*.gif”);

// se construieste un obiect Imagine pentru fiecare fisier si se adauga la ImageList.

foreach (string fisier_gif in gif_uri)
{
Bitmap desen= new Bitmap (fisier_gif);
desene_animate.Images.Add(desen);
pictureBox2.Image=desen;
}

Graphics g = this.CreateGraphics();

// Deseneaza fiecare imagine utilizand metoda ImageList.Draw()
//MessageBox.Show(“”+desene_animate.Images.Count);

for (int i = 0; i < desene_animate.Images.Count; i++)
{
//if (i<10)
desene_animate.Draw(g, 60 + i * 60, 60, i);
//else desene_animate.Draw(g, 60 + (i-10) * 60, 120, i);
}
g.Dispose();

}

private void button1_Click(object sender, System.EventArgs e)
{
string[] gif_uri = Directory.GetFiles(“E:\\Imagini”, “*.gif”);

foreach (string fisier_gif in gif_uri)
{
Bitmap desen= new Bitmap (fisier_gif);
pictureBox2.Image=desen;
MessageBox.Show(“Next”);
}

}

private void pictureBox2_Click(object sender, EventArgs e)
{

}

}
}

Form din alt Form

1. Pe Forma1 adaugati un buton
2. Selectati Add din Solution Explorer (clic cu butonul din dreapta pe Form1) şi apoi Windows Form. Va apare fereastra de dialog Add New Item în care vom alege Windows Form, iar la Name vom tasta Form2.cs şi apoi Add.
3. A aparut o noua macheta pe care putem realiza o interfata
4. La apasarea butonului se activeaza forma2 cu metoda ShowDialog() a obiectului ob din clasa Form2.

private void Button1_Click(object sender, EventArgs e)
{
Form2 ob = new Form2();
ob.ShowDialog();
}

Tema1

Realizati un meniu care sa cuprinda 4 Itemi ce vor referi 4 forme:
1.Primul panou sa fie verde si sa aiba trasata o elipsa
2. Al doilea panou sa fie albastru si sa aiba trasat un dreptunghi
3. Al treilea panou sa fie roz si sa contina un triunghi
4. Helpul sa fie un panou galben care sa contina formulele de calcul pentru aria celor trei figuri geometrice de mai sus.

Tema 2

Modificati tema1 a.i. in fiecare panou sa apara posibilitatea introducerii elementelor de calcul pentru aria figurii geometrice din panou si afisarea rezultatului.

Menu

Pentru studierea meniurilor in C# avem la dispozitie urmatoarea resursa.

http://en.csharp-online.net/Tool%2C_Menu%2C_and_Status_Strips

Tema:

Realizati o aplicati pentru tema ” O afacere de succes” in care sa proiectati un meniu in care sa aveti prevazut un Help, Exit, Descrierea, ERD-ul.

LabelLink

Fisa de laborator

1. Adaugati pe panou etichetele si etichetele de legatura:
Click pentru a explora C:\
Click pentru a naviga http://www.microsoft.com/ro/ro/
Click pentru a rula Notepad

Asociati codul etichetelor de legatura:
private void etichetaC_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
{
etichetaC.LinkVisited = true;
System.Diagnostics.Process.Start( @”C:\” );
}

private void etichetaI_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
{
etichetaI.LinkVisited = true;
System.Diagnostics.Process.Start( “IExplore”, “http://www.microsoft.com/romania/&#8221; );
}

private void etichetaN_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
{
etichetaN.LinkVisited = true;
System.Diagnostics.Process.Start( “notepad” );
}

Tema:

Adaugati un link catre o fotografie stocata in calculator.

Grafica

TEMA 1

Sa se plaseze pe panoul de intrare doua butoane cu textul Elipsa si Dreptunghi. La apasarea lor intr-un panel sa apara o elipsa si un dreptunghi.

Solutie data de Bogdan:

private void Dreptunghi_Click(object sender, EventArgs e)
{
int L=100;
int H=80;
Graphics g = panel1.CreateGraphics();
Color fundal = panel1.BackColor;
g.Clear(fundal);
Pen pen = new Pen(Color.Black);
int x = panel1.Width / 2 – L / 2;
int y = panel1.Height / 2 – H / 2;
Rectangle a = new Rectangle(x, y, L, H);
g.DrawRectangle (pen, a);
g.Dispose();
}

private void Elipsa_Click(object sender, EventArgs e)
{
int L = 100;
int H = 80;
Graphics g = panel1.CreateGraphics();
Color fundal = panel1.BackColor;
g.Clear(fundal);
Pen pen = new Pen(Color.Black);
int x = panel1.Width / 2 – L / 2;
int y = panel1.Height / 2 – H / 2;
Rectangle a = new Elipsa(x, y, L, H);
g.DrawEllipse(pen, a);
g.Dispose();

}

Tema 2

Colorati panel-ul in verde si umpleti figurile grafice cu galben.

Follow

Get every new post delivered to your Inbox.