Category: Csharp – C#


C Sharp İle MS Excel Dosyasını Okumak Ve Yazmak

April 2nd, 2008 — 10:18pm

Excel tartışmasız mükemmel bir program. Bence bu güne kadar geliştirilmiş en mükemmel
uygulama. Adı da İngilizce “Excellent” kelimesinden türetilmiştir. Detaylarını
bilip kullananlar üzerlerindeki işleri ona yıkar ve rahat ederler.

Excel aklınıza ne kadar firma geliyorsa hepsinde kullanılanda bir uygulamadır.

Kimisi Word kullanır gibi kullansa da :) neredeyse bütün firmalar Excel kullanır.

Yazdığımız uygulamalarda da zaman zaman Excel dosyası çıktısı vermek zorunda kalabiliriz.
Bazı durumlarda kullandığımız class’ların (Mesela CrystalReport) Excel çıktısı
veren metotları vardır. Ama olmadığı zaman nasıl yaparız. Onun için basit bir örnek
yaptım.

C# içinde Excel kullanmamız gerekiyorsa, Excel library’sini referans olarak
göstermemiz gerekiyor. “Add reference” kısmında “Com” sekmesinde
kullanabileceğiniz Excel libraryleri göreceksiniz. Ofis 2003 çıktısı için Microsoft
Excel 11.0 Object library’yi ekliyoruz. Birde Excel Com bir nesne, metotların
aldığı bazı parametrelerde “Missing.Value” kullanmamız gerekiyor bunun
için de Reflection ‘ı kullanacağımızı belirtmemiz gerekiyor.

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

Sonrasında yapmak istediklerimizi yazabiliriz.

Örnek’de bir excel dosyasını okumak ve içine bir şeyler yazmak canlandırılıyor.

Ayrıca bir datagrid otomatik olarak excel dosyası olarak kaydedilebilir. bunu yapabileceğimiz
bir metodu da örneğe ekledim.

Örneğin Kodları

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    ExcelOku(dt);
 
    ExcelYaz(dt);
 
    ExcelYeniDosyayaYaz("YeniDosya", dt);
 
    Response.Write("İşlem Tamamlandı");
}
private void ExcelYeniDosyayaYaz(string p, DataTable _excel)
{
    try
    {
        Excel.Application xlApp = new Excel.Application();
 
        if (xlApp == null)
        {
            throw new Exception("Excel Başlatılamadı. Excel Kurulu Olmaya Bilir.");
        }
 
        xlApp.Visible = false;
        xlApp.DisplayAlerts = false;
 
        Excel.Workbook wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
 
        Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
 
        for (int i = 1; i <= _excel.Rows.Count; i++)
        {
            int r = i + 1;
            Excel.Range _UrunAdi = (Excel.Range)ws.Cells[r, 1];
            Excel.Range _Marka = (Excel.Range)ws.Cells[r, 2];
            Excel.Range _Bolge = (Excel.Range)ws.Cells[r, 3];
            Excel.Range _Adet = (Excel.Range)ws.Cells[r, 4];
            Excel.Range _Fiyat = (Excel.Range)ws.Cells[r, 5];
            Excel.Range _Tutar = (Excel.Range)ws.Cells[r, 6];
            Excel.Range _Tarih = (Excel.Range)ws.Cells[r, 7];
 
            int j = i - 1;
            _UrunAdi.Formula = _excel.Rows[j][0].ToString();
            _Marka.Formula = _excel.Rows[j][1].ToString();
            _Bolge.Formula = _excel.Rows[j][2].ToString();
            _Adet.Formula = _excel.Rows[j][3].ToString();
            _Fiyat.Formula = _excel.Rows[j][4].ToString();
            _Tutar.Formula = _excel.Rows[j][5].ToString();
            _Tarih.Formula = _excel.Rows[j][6].ToString();
        }
 
        ws.Cells.EntireColumn.AutoFit();
 
        wb.SaveAs(Server.MapPath("YeniExcel.xls"), Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value,
        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value);
 
        wb.Close(true, Missing.Value, Missing.Value);
        xlApp.Quit();
 
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + " ");
    }
}
private void ExcelOku(DataTable BosDt)
{
    try
    {
        Excel.Application xlApp = new Excel.Application();
 
        if (xlApp == null)
        {
            throw new Exception("Excel Başlatılamadı. Excel Kurulu Olmaya Bilir.");
        }
 
        xlApp.Visible = false;
        xlApp.DisplayAlerts = false;
 
        Excel.Workbook wb = xlApp.Workbooks.Open(
                        Server.MapPath(ConfigurationManager.AppSettings["OkunacakExcelFile"]),
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value);
 
        Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
 
        Excel.Range satirSayisiIcin = ws.Cells.SpecialCells(
        Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Missing.Value);
 
        for (int c = 0; c < satirSayisiIcin.Column; c++)
        {
        BosDt.Columns.Add("C_" + c.ToString());
        }
 
        for (int i = 1; i <= satirSayisiIcin.Row; i++)
        {
            int r = i + 1;
            Excel.Range _UrunAdi = (Excel.Range)ws.Cells[r, 1];
            Excel.Range _Marka = (Excel.Range)ws.Cells[r, 2];
            Excel.Range _Bolge = (Excel.Range)ws.Cells[r, 3];
            Excel.Range _Adet = (Excel.Range)ws.Cells[r, 4];
            Excel.Range _Fiyat = (Excel.Range)ws.Cells[r, 5];
            Excel.Range _Tutar = (Excel.Range)ws.Cells[r, 6];
            Excel.Range _Tarih = (Excel.Range)ws.Cells[r, 7];
 
            int j = i - 1;
            BosDt.Rows.Add(_UrunAdi.Formula, _Marka.Formula, _Bolge.Formula, _Adet.Formula,
            _Fiyat.Formula, _Tutar.Formula, _Tarih.Formula);
        }
 
        ws.Cells.EntireColumn.AutoFit();
 
        wb.Close(true, Missing.Value, Missing.Value);
        xlApp.Quit();
 
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message + " ");
    }
}
private void ExcelYaz(DataTable _excel)
{
    try
    {
        Excel.Application xlApp = new Excel.Application();
 
        if (xlApp == null)
        {
            throw new Exception("Excel Başlatılamadı. Excel Kurulu Olmaya Bilir.");
        }
 
        xlApp.Visible = false;
        xlApp.DisplayAlerts = false;
 
        Excel.Workbook wb = xlApp.Workbooks.Open(
                                Server.MapPath(ConfigurationManager.AppSettings["BosExcelFile"]),
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value);
 
        Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
 
        for (int i = 1; i <= _excel.Rows.Count; i++)
        {
            int r = i + 1;
            Excel.Range _UrunAdi = (Excel.Range)ws.Cells[r, 1];
            Excel.Range _Marka = (Excel.Range)ws.Cells[r, 2];
            Excel.Range _Bolge = (Excel.Range)ws.Cells[r, 3];
            Excel.Range _Adet = (Excel.Range)ws.Cells[r, 4];
            Excel.Range _Fiyat = (Excel.Range)ws.Cells[r, 5];
            Excel.Range _Tutar = (Excel.Range)ws.Cells[r, 6];
            Excel.Range _Tarih = (Excel.Range)ws.Cells[r, 7];
 
            int j = i - 1;
            _UrunAdi.Formula = _excel.Rows[j][0].ToString();
            _Marka.Formula = _excel.Rows[j][1].ToString();
            _Bolge.Formula = _excel.Rows[j][2].ToString();
            _Adet.Formula = _excel.Rows[j][3].ToString();
            _Fiyat.Formula = _excel.Rows[j][4].ToString();
            _Tutar.Formula = _excel.Rows[j][5].ToString();
            _Tarih.Formula = _excel.Rows[j][6].ToString();
        }
 
        ws.Cells.EntireColumn.AutoFit();
 
        wb.Close(true, Missing.Value, Missing.Value);
        xlApp.Quit();
 
    }
    catch (Exception ex)
    {
         Response.Write(ex.Message + " ");
    }
}
private void DataGridToExcel()
{
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Projects.xls");
    Response.Charset = "iso-8859-9";
    Response.ContentType = "application/vnd.xls";
    Response.ContentEncoding = System.Text.Encoding.Default;
 
    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 
    DataGrid dg = new DataGrid();
    dg.ID = "Deneme";
 
    DataTable dt = new DataTable();
    dt.Columns.Add("A");
    dt.Columns.Add("B");
 
    dt.Rows.Add("Deneme", "1");
    dt.Rows.Add("Deneme", "2");
 
    dg.DataSource = dt;
    dg.DataBind();
 
    dg.HeaderStyle.BackColor = System.Drawing.Color.White;
    dg.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}

Kaynaklar

asp.net forum

msdn blog

Visual Studio 2008 ile hazırlandı.
Kodları
indirmek için Tıklayınız

Old format or invalid type library. (Exception from HRESULT: 0×80028018 (TYPE_E_INVDATAREAD))

Bu hatayı aldığımızda kültür sorunu yaşıyor olabiliriz. Workbook a birşey eklemeden
önce thread’in kültürünü değiştirerek sorunu çözebiliyoruz.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Msdn forum post
http://support.microsoft.com/kb/306022/
http://support.microsoft.com/default.aspx?scid=kb;en-us;320369
http://msdn.microsoft.com/en-us/library/aa168494.aspx

17 comments » | Csharp - C#, Excel

C Sharp ile Rastgele Sayı Üretmek

April 2nd, 2008 — 10:12pm

Programlarımızda ürettiğimiz rastgele sayılar gerçek anlamda rastgele sayılmaz. Bir algoritmaya göre üretilmiş sözde rastgele (Pseudo-random) sayılardır. Yani ortada yazılmış bir metot vardır ve bu metot bazı parametrelere göre bir çıktı oluşturur. Bu parametreler aynı ise aynı sonucu elde ederiz. İşin teorisinde durum budur.

Gerçek rastgele sayı herhangi bir şarta bağlı olmadan herhangi bir zamanda oluşan rastgele bir değerdir. Aynı şartlarda aynı sonucu vermemelidir.

Bu rastgele sayı üreteçleri ne kadar rastgele birkaç kodu deneyerek bir örnek hazırladım. Gördüğüm o ki bize sadece bir sayı lazımsa biz “.Net deki standart Random Class’ından şaşmayalım.”

Mersenne twister algoritması hızlı çıktı üretiyormuş fakat hep aynı sonuçları veriyor.

Cryptography’nin altındaki classlar byte olarak sonuç üretiyor. Biz bu byteları kullanmak istersek 50 defada 4-5 kez aynı sayıdan 2 tane üretme yapıyor. Ama byteları yan yana getirip daha uzun bi sayı oluşturursak bunlar 50 defada hiç tekrar etmedi.

Öte yandan Random Class’ı da 50 defada hiçbir ek yapmadan aynı sayıyı üretmedi.

Kaynaklar
memikyanik.com/forumlara/kripto_cs.htm
qbrundage.com/michaelb/pubs/essays/random_number_generation
codeproject.com/KB/cs/csharpmersennetwister.aspx

Visual Studio 2008 ile hazırlandı.
Kodları indirmek için Tıklayınız

Örneğin Kodları

protected void Page_Load(object sender, EventArgs e)
{
	//Marsene Twister Random       
	MT19937 m = new MT19937();
 
	DataTable dt = new DataTable();
	dt.Columns.Add("Sayilar");
 
	for (int i = 0; i < 50; i++)
	{
		dt.Rows.Add(m.genrand_int32().ToString());
	}
 
	//Standart Random
	Random rnd = new Random();
 
	DataTable dt2 = new DataTable();
	dt2.Columns.Add("Sayilar");
 
	for (int j = 0; j < 50; j++)
	{
		dt2.Rows.Add(rnd.Next().ToString());
	}
 
	//Kriptodan Random     
 
	RandomNumberGenerator rng = RandomNumberGenerator.Create();
 
	DataTable dt3 = new DataTable();
	dt3.Columns.Add("Sayilar");
 
	for (int k = 0; k < 50; k++)
	{
		byte[] _byt = new byte[4];
		//rng.GetBytes(_byt);
		rng.GetNonZeroBytes(_byt);
		dt3.Rows.Add(BytetanIntYap(_byt).ToString());
	}
 
	//Kriptodan Random        
	RNGCryptoServiceProvider rng2 = new RNGCryptoServiceProvider();
 
	DataTable dt4 = new DataTable();
	dt4.Columns.Add("Sayilar");
 
	for (int t = 0; t < 50; t++)
	{
		byte[] _byt2 = new byte[4];
		rng2.GetBytes(_byt2);
		dt4.Rows.Add(BytetanIntYap(_byt2).ToString());
	}
 
	DataRow[] dr1 = dt.Select("Sayilar is not null", "Sayilar ASC");
	DataRow[] dr2 = dt2.Select("Sayilar is not null", "Sayilar ASC");
	DataRow[] dr3 = dt3.Select("Sayilar is not null", "Sayilar ASC");
	DataRow[] dr4 = dt4.Select("Sayilar is not null", "Sayilar ASC");
 
	for (int s = 0; s < 50; s++)
	{
		if (s < 49) // dizinin dışına çıkmasın diye
		{
			EkranaYaz(dr1, s);
			Response.Write(" - ");
 
			EkranaYaz(dr2, s);
			Response.Write(" - ");
 
			EkranaYaz(dr3, s);
			Response.Write(" - ");
 
			EkranaYaz(dr4, s);
			Response.Write("<br />");
		}
	}
}
 
private void EkranaYaz(DataRow[] dr3, int s)
{
	//a farklı renkte göstersin diye
	if (dr3[s][0].ToString() == dr3[s + 1][0].ToString())
	{
		Response.Write("<a href=""deneme"">" 
				 + dr3[s][0].ToString() + "</a>");
	}
	else
	{
		Response.Write(dr3[s][0].ToString());
	}
}
 
private int BytetanIntYap(byte[] dizi)
{
	int iResult = 0;
	for (int i = 0; i < 4; i++)
		iResult = iResult | ((int)dizi[i] << ((3 - i) * 8));
	return iResult;
}

1 comment » | Csharp - C#

Engellere Çarpmama Oyunu

March 27th, 2008 — 9:58pm

Bu oyunda klavyedeki yön tuşları ile hareket eden cisimlere çarpmadan en yukarı
çıkmaya çalışıyoruz. En sona gelirsek yeni açılan formda kazandınız yazıyor. Eğer
bir engele çarparsak açılan formda kaybettiniz yazıyor.

Eğitsel Oyun, C#

Visual Studio 2005 ile yapıldı. (c# 2.0 ımsı)
Kodları indirmek için Tıklayınız

Form Nesnesindeki Kodlar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace _1021042___proje_28_nisan
{
public partial class Form1 : Form
{
int durum1, durum2, durum3, durum4 = 0;
int sol1 = 270;
int sol2 = 100;
int sol3 = 250;
int sol4 = 280;
 
public Form1()
{
    InitializeComponent();
}
 
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (panel1.Top &lt; 80)
{
    Form2 frm2 = new Form2();
    Form1 frm1 = new Form1();
    Label yazi = new Label();
    frm2.Show();
    frm2.Controls.Add(yazi);
    yazi.Text = "Kazandınız";
    yazi.Font = new Font("Microsoft Sans Serif",
                   15.75F, FontStyle.Regular);
    yazi.Location = new Point(100, 100);
    yazi.AutoSize = true;
}
 
if (e.KeyCode == Keys.Left)
{
    if (panel1.Left &lt;= 100)
    panel1.Left = 100;
    else if (panel1.Left != 100)
    panel1.Left = panel1.Left - 4;
}
 
if (e.KeyCode == Keys.Right)
{
    if (panel1.Left &gt;= 400)
    panel1.Left = 400;
    else if (panel1.Left != 400)
    panel1.Left = panel1.Left + 4;
}
 
if (e.KeyCode == Keys.Up)
{
    if (panel1.Top &lt;= 0)
        panel1.Top = 0;
    else if (panel1.Top != 0)
        panel1.Top = panel1.Top - 4;
}
 
if (e.KeyCode == Keys.Down)
{
    if (panel1.Top &gt;= 350)
        panel1.Top = 350;
    else if (panel1.Top != 350)
        panel1.Top = panel1.Top + 4;
}
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    if (durum1 == 1)
        sol1 += 5;
    else
        sol1 -= 3;
 
    if (sol1 &lt; 100)
        durum1 = 1;
 
    if (sol1 &gt; 320)
        durum1 = 2;
 
    panel4.Location = new Point(sol1, 100);
 
    if (panel1.Left &gt; panel4.Left &amp; panel1.Left &lt; panel4.Left + 80)
    {
        if (panel1.Top &lt; panel4.Top + 20 &amp; panel1.Top &gt; panel4.Top)
        {
            timer1.Enabled = false;
            Form2 frm2 = new Form2();
            Form1 frm1 = new Form1();
            Label yazi = new Label();
            frm2.Show();
            frm2.Controls.Add(yazi);
            yazi.Text = "Kaybettiniz";
            yazi.Font = new Font("Microsoft Sans Serif",
                           15.75F, FontStyle.Regular);
            yazi.Location = new Point(100, 100);
            yazi.AutoSize = true;
        }
    }
}
 
private void timer2_Tick(object sender, EventArgs e)
{
    if (durum2 == 1)
        sol2 += 5;
    else
        sol2 -= 5;
 
    if (sol2 &lt; 100)
        durum2 = 1;
 
    if (sol2 &gt; 320)
        durum2 = 2;
 
    panel5.Location = new Point(sol2, 162);
 
    if (panel1.Left &gt; panel5.Left &amp; panel1.Left &lt; panel5.Left + 80)
    {
        if (panel1.Top &lt; panel5.Top + 20 &amp; panel1.Top &gt; panel5.Top)
        {
            timer2.Enabled = false;
            Form2 frm2 = new Form2();
            Form1 frm1 = new Form1();
            Label yazi = new Label();
            frm2.Show();
            frm2.Controls.Add(yazi);
            yazi.Text = "Kaybettiniz";
            yazi.Font = new Font("Microsoft Sans Serif",
                          15.75F, FontStyle.Regular);
            yazi.Location = new Point(100, 100);
            yazi.AutoSize = true;
        }
    }
}
 
private void timer3_Tick(object sender, EventArgs e)
{
    if (durum3 == 1)
        sol3 += 7;
    else
        sol3 -= 7;
 
    if (sol3 &lt; 100)
        durum3 = 1;
 
    if (sol3 &gt; 320)
        durum3 = 2;
 
    panel6.Location = new Point(sol3, 224);
 
    if (panel1.Left &gt; panel6.Left &amp; panel1.Left &lt; panel6.Left + 80)
    {
        if (panel1.Top &lt; panel6.Top + 20 &amp; panel1.Top &gt; panel6.Top)
        {
            timer3.Enabled = false;
            Form2 frm2 = new Form2();
            Form1 frm1 = new Form1();
            Label yazi = new Label();
            frm2.Show();
            frm2.Controls.Add(yazi);
            yazi.Text = "Kaybettiniz";
            yazi.Font = new Font("Microsoft Sans Serif",
                            15.75F, FontStyle.Regular);
            yazi.Location = new Point(100, 100);
            yazi.AutoSize = true;
        }
    }
}
 
private void timer4_Tick(object sender, EventArgs e)
{
    if (durum4 == 1)
        sol4 += 5;
    else
        sol4 -= 5;
 
    if (sol4 &lt; 100)
        durum4 = 1;
 
    if (sol4 &gt; 320)
        durum4 = 2;
 
    panel7.Location = new Point(sol4, 286);
 
    if (panel1.Left &gt; panel7.Left &amp; panel1.Left &lt; panel7.Left+80)
    {
        if (panel1.Top &lt; panel7.Top + 20 &amp; panel1.Top &gt;panel7.Top)
        {
            timer4.Enabled = false;
            Form2 frm2 = new Form2();
            Form1 frm1 = new Form1();
            Label yazi = new Label();
            frm2.Show();
            frm2.Controls.Add(yazi);
            yazi.Text = "Kaybettiniz";
            yazi.Font = new Font("Microsoft Sans Serif",
                         15.75F, FontStyle.Regular);
            yazi.Location = new Point(100, 100);
            yazi.AutoSize = true;
        }
    }
}
}
}

Comment » | Csharp - C#

Kızma Biraderimsi Sponge Bob ve Patrik Oyunu

March 27th, 2008 — 9:24pm

Buda ödev olarak yaptığım bi oyun. Sponge Bob ve Patrik için 2 ayrı düğme var. Basıyosun
gelen rasgele zar sayısını otomatik ilerliyolar. Önce bitiren kazanıyo.

Eğitsel Oyun, C#

Visual Studio 2005 ile yapıldı. (c# 2.0 ımsı)
Kodları indirmek için Tıklayınız

Form Nesnesindeki Kodlar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ödev_1
{
public partial class Form1 : Form
{
    int i, n, m, a, b, c = 0;
    int[] zarrr = new int[6] { 1, 2, 3, 4, 5, 6 };
 
    public Form1()
    {
        InitializeComponent();
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
        //Rastgele Bir Zar Görüntülemek İçin
        Random zar = new Random();
        int zarr=zar.Next(6);
 
        //zarın koşulları
        if (zarrr[zarr] == 1)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible=true;
            pictureBox4.Visible=false;
            pictureBox6.Visible=false;
            pictureBox7.Visible=false;
            pictureBox8.Visible=false;
            pictureBox9.Visible=false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "1";
            // ilerlenilen toplam kare sayısı
            a = a + 1;
        }
        else if (zarrr[zarr] == 2)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = true;
            pictureBox6.Visible=false;
            pictureBox7.Visible=false;
            pictureBox8.Visible=false;
            pictureBox9.Visible=false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "2";
            // ilerlenilen toplam kare sayısı
            a = a + 2;
        }
        else if (zarrr[zarr] == 3)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible=false;
            pictureBox6.Visible = false;
            pictureBox7.Visible=false;
            pictureBox8.Visible=false;
            pictureBox9.Visible=true;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "3";
            // ilerlenilen toplam kare sayısı
            a = a + 3;
        }
        else if (zarrr[zarr] == 4)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible=false;
            pictureBox6.Visible=false;
            pictureBox7.Visible =false ;
            pictureBox8.Visible=true;
            pictureBox9.Visible=false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "4";
            // ilerlenilen toplam kare sayısı
            a = a + 4;
        }
        else if (zarrr[zarr] == 5)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible=false;
            pictureBox6.Visible=false;
            pictureBox7.Visible=true;
            pictureBox8.Visible = false;
            pictureBox9.Visible=false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "5";
            // ilerlenilen toplam kare sayısı
            a = a + 5;
        }
        else if (zarrr[zarr] == 6)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = false;
            pictureBox6.Visible = true;
            pictureBox7.Visible = false;
            pictureBox8.Visible = false;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "6";
            // ilerlenilen toplam kare sayısı
            a = a + 6;
        }
 
        //sponge bob'ın kaç kare gittiğini göstermek için
        //skorbord
        label6.Text = a.ToString();
 
        //piyonun ilerlemesini sağlayan koşul
        i = a;
        //oyunun kazanıldığının uyarılması
        if (a &gt;= 80)
        {
            MessageBox.Show("Sponge Bob Kazandı! "
                                + label7.Text + " / " + label6.Text);
            //piyonların ortaya gelmesi
            pictureBox5.Location = new Point(375, 290);
            pictureBox11.Location = new Point(375, 270);
            //zar atma butonlarının kapatılması
            button1.Enabled = false;
            button2.Enabled = false;
            //oyuna başlama butonunun tekrar aktif olması
            button3.Enabled = true;
            // skorboardun sıfırlanması
            a = 0;
            c = 0;
            label6.Text = a.ToString();
            label7.Text = c.ToString();
        }
        else if (i &gt;74)
        {
            n = 30;
            m = 355;
            b = i - 74;
            pictureBox5.Location = new Point(n, m - (b * 50));
        }
        else if (i &gt;61)
        {
            n = 680;
            m = 355;
            b = i - 61;
            pictureBox5.Location = new Point(n - (b * 50), m);
        }
        else if (i &gt;55)
        {
            n = 730;
            m = 55;
            b = i - 55;
            pictureBox5.Location = new Point(n, m + (b * 50));
        }
        else if (i &gt; 40)
        {
            n = 30;
            m = 55;
            b = i - 40;
            pictureBox5.Location = new Point(n + (b * 50), m);
        }
        else if (i&gt;34)
        {
            n = 30;
            m = 355;
            b = i - 34;
            pictureBox5.Location = new Point(n, m - (b * 50));
        }
        else if (i &gt;20)
        {
            n = 680;
            m = 355;
            b = i - 21;
            pictureBox5.Location = new Point(n - (b * 50), m);
        }
        else if(i &gt;14)
        {
            n = 730;
            m = 55;
            b = i - 14;
            pictureBox5.Location = new Point(n, m + (b * 50));
        }
        else
        {
            n = 30;
            m = 55;
            pictureBox5.Location = new Point(n + (i * 50), m);
        }
 
        //zar atma sırası öbür oyuncuda mı?
        if (a == 6 | a == 18 | a == 23 | a == 46 | a == 59 | a == 63)
        {
            MessageBox.Show("dadadada bir kere daha zar at");
        }
        else
        {
            button1.Enabled = false;
            button2.Enabled = true;
        }
    }
 
    private void button3_Click(object sender, EventArgs e)
    {
        pictureBox5.Location = new Point(30, 55);
        pictureBox11.Location = new Point(30, 35);
        //oyuna sponge bob başlar
        button1.Enabled = true;
        //oyuna başla düğmesi deaktif olur
        button3.Enabled = false;
    }
 
    private void button2_Click(object sender, EventArgs e)
    {
        //Rastgele Bir Zar Görüntülemek İçin
        Random zar = new Random();
        int zarr = zar.Next(6);
 
        //zarın koşulları
        if (zarrr[zarr] == 1)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = true;
            pictureBox4.Visible = false;
            pictureBox6.Visible = false;
            pictureBox7.Visible = false;
            pictureBox8.Visible = false;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "1";
            // ilerlenilen toplam kare sayısı
            c = c + 1;
        }
        if (zarrr[zarr] == 2)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = true;
            pictureBox6.Visible = false;
            pictureBox7.Visible = false;
            pictureBox8.Visible = false;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "2";
            // ilerlenilen toplam kare sayısı
            c = c+ 2;
        }
        if (zarrr[zarr] == 3)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = false;
            pictureBox6.Visible = false;
            pictureBox7.Visible = false;
            pictureBox8.Visible = false;
            pictureBox9.Visible = true;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "3";
            // ilerlenilen toplam kare sayısı
            c = c + 3;
        }
        if (zarrr[zarr] == 4)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = false;
            pictureBox6.Visible = false;
            pictureBox7.Visible = false;
            pictureBox8.Visible = true;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "4";
            // ilerlenilen toplam kare sayısı
            c = c + 4;
        }
        if (zarrr[zarr] == 5)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = false;
            pictureBox6.Visible = false;
            pictureBox7.Visible = true;
            pictureBox8.Visible = false;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "5";
            // ilerlenilen toplam kare sayısı
            c = c + 5;
        }
        if (zarrr[zarr] == 6)
        {
            // zar sonucunun resmini görüntülemek için
            pictureBox1.Visible = false;
            pictureBox4.Visible = false;
            pictureBox6.Visible = true;
            pictureBox7.Visible = false;
            pictureBox8.Visible = false;
            pictureBox9.Visible = false;
            // zar resminin altındaki sayıyı yazmak için
            label2.Text = "6";
            // ilerlenilen toplam kare sayısı
            c = c + 6;
        }
 
        //patrik'in kaç kare gittiğini göstermek için
        //skorbord
        label7.Text = c.ToString();
 
        //piyonun ilerlemesini sağlayan koşul
        i = c;
        //oyunun kazanıldığının uyarılması
        if (c &gt;= 80)
        {
            MessageBox.Show("Patrik Kazandı! " + label7.Text
                                               + " / " + label6.Text);
            //piyonların ortaya gelmesi
            pictureBox5.Location = new Point(375, 290);
            pictureBox11.Location = new Point(375, 270);
            //zar atma butonlarının kapatılması
            button1.Enabled = false;
            button2.Enabled = false;
            //oyuna başlama butonunun tekrar aktif olması
            button3.Enabled = true;
            // skorboardun sıfırlanması
            a = 0;
            c = 0;
            label6.Text = a.ToString();
            label7.Text = c.ToString();
        }
        else if (i &gt; 74)
        {
            n = 30;
            m = 335;
            b = i - 74;
            pictureBox11.Location = new Point(n, m - (b * 50));
        }
        else if (i &gt; 61)
        {
            n = 680;
            m = 335;
            b = i - 61;
            pictureBox11.Location = new Point(n - (b * 50), m);
        }
        else if (i &gt; 55)
        {
            n = 730;
            m = 35;
            b = i - 55;
            pictureBox11.Location = new Point(n, m + (b * 50));
        }
        else if (i &gt; 40)
        {
            n = 30;
            m = 35;
            b = i - 40;
            pictureBox11.Location = new Point(n + (b * 50), m);
        }
        else if (i &gt; 34)
        {
            n = 30;
            m = 335;
            b = i - 34;
            pictureBox11.Location = new Point(n, m - (b * 50));
        }
        else if (i &gt; 20)
        {
            n = 680;
            m = 335;
            b = i - 21;
            pictureBox11.Location = new Point(n - (b * 50), m);
        }
        else if (i &gt; 14)
        {
            n = 730;
            m = 35;
            b = i - 14;
            pictureBox11.Location = new Point(n, m + (b * 50));
        }
 
        else
        {
        n = 30;
        m = 35;
        pictureBox11.Location = new Point(n + (i * 50), m);
        } 
 
        //zar atma sırası öbür oyuncuda
        if (c == 6 | c == 18 | c == 23 | c == 46 | c == 59 | c == 63)
        {
            MessageBox.Show("dadadada bir kere daha zar at");
        }
        else
        {
            button1.Enabled = true;
            button2.Enabled = false;
        }
        }
        }
    }

Comment » | Csharp - C#

Page 6 of 6« First...23456

Back to top