„Sznikák példakódok” változatai közötti eltérés
A VIK Wikiből
Eseménykezelés |
rajzolás |
||
| 119. sor: | 119. sor: | ||
{{Infobox | |||
| cím = Négyzet rajzolás | |||
| háttérszín = #C0ffee | |||
| keretszín = black | |||
| tartalom = | |||
Írjon olyan C# nyelvű alkalmazásrészletet, amely a (10,20) koordinátában megjelenít egy közepesen szürke színnel kitöltött 10 pixel hosszúságú négyzetet. A négyzet színe minden "r" bilentyű megnyomására legyen egyre sötétebb. A megjelenítés DGI-re épüljön. | |||
{{Infobox-táblázat| | |||
{{Infobox-táblázatsor|A kód nyelve|C#}} | |||
}} | |||
}} | |||
public partial class Form1 : Form | |||
{ | |||
private Brush brush1; | |||
private int grey = 200; | |||
public Form1() | |||
{ | |||
InitializeComponent(); | |||
} | |||
protected override void OnKeyDown(KeyEventArgs e) | |||
{ | |||
base.OnKeyDown(e); | |||
if(e.KeyCode == Keys.R) { | |||
if(grey == 0) // ha elértük a színtartomány végét | |||
grey = 200; | |||
grey -= 10; | |||
Invalidate(); | |||
} | |||
} | |||
protected override void OnPaint(PaintEventArgs e) | |||
{ | |||
base.OnPaint(e); | |||
using (brush1 = new SolidBrush(ConsoleColor.FromArgb(grey, grey, grey))) | |||
{ | |||
e.Graphics.FillRectrangle(brush1, 10, 20, 20, 30); | |||
} | |||
} | |||
} | |||
A lap 2013. május 28., 12:40-kori változata
class ThreadTestClass
{
public static void Main(string[] args)
{
Thread t = null;
if (args.Length == 0)
{
t = new Thread(new ThreadStart(ThreadMethod1));
t.Start();
}
else
{
t = new Thread(new ParameterizedThreadStart(ThreadMethod2));
t.Start(args[0]);
}
}
public static void ThreadMethod1()
{
Console.WriteLine("Thread without parameter.");
}
public static void ThreadMethod2(object param)
{
Console.WriteLine("Thread with parameter: {0}", param.ToString());
}
}
SqlConnection conn = null;
try
{
// Kapcsolódás azadatbázishoz
conn = new SqlConnection(@"Data Source=LAPTOP\SQLEXPRESS;InitialCatalog=Northwind;Integrated Security=True");
// A kapcsolat megnyitása
conn.Open();
// Az adatbázis parancs létrehozása
SqlCommand command = new SqlCommand("SELECT ShipperID, CompanyName, Phone FROM Shippers");
// Adatbázis kapcsolat megadása
command.Connection = conn;
Console.WriteLine("{0,0}{1,15}{2,15}", "ShipperID", "CompanyName", "Phone");
Console.WriteLine("-----------------------------------------------------------------");
// Az adatok lekérdezése és kiiratása
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
Console.WriteLine("{0,4}{1,20}{2,20}",
reader["ShipperID"].ToString(),
reader["CompanyName"].ToString(),
reader["Phone"].ToString());
}
}
catch (Exception ex)
{
// Kivétel szövegének kiiratása
Console.WriteLine(ex.Message);
}
finally
{
// Az adatbázis kapcsolat lezárása, ha meg lett nyitva
if((conn!=null)&&(conn.State==System.Data.ConnectionState.Open))
conn.Close();
}
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);
}
protected override void OnKeyDown(KeyEventArgs e)
{
// Meghívjuk az eredeti függvényt is
base.OnKeyDown(e);
MessageBox.Show("A billentyű (virt. fv.): " + e.KeyCode.ToString());
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("A billentyű (eseménykez.): " + e.KeyCode.ToString());
}
}
public partial class Form1 : Form
{
private Brush brush1;
private int grey = 200;
public Form1()
{
InitializeComponent();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(e.KeyCode == Keys.R) {
if(grey == 0) // ha elértük a színtartomány végét
grey = 200;
grey -= 10;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (brush1 = new SolidBrush(ConsoleColor.FromArgb(grey, grey, grey)))
{
e.Graphics.FillRectrangle(brush1, 10, 20, 20, 30);
}
}
}
