1. Kapat butonunu pasifleştirmek

    private const int SC_CLOSE = 0xF060;
    private const int MF_GRAYED = 0x1;
    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport("user32.dll")]
    private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);
    private void Form1_Load(object sender, System.EventArgs e)
    {
    EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
    }

    Continue reading »
  2. Karakterlerin Program Üzerindeki Yer Bilgisi

    using System;
    using System.Windows.Forms;
    namespace ListBoxMouse1
    {
    public class MainForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ListBox listBox1;
    public MainForm()
    {
    InitializeComponent();
    // now set this particular event handler to function ButtonDown()
    this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler( this.ButtonDown );
    }
    // program entry point
    public static void Main(string[] args)
    {
    Application.Run(new MainForm());
    }
    #region Windows Forms Designer generated code
    private void InitializeComponent()
    {
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // listBox1
    //
    this.listBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
    this.listBox1.ItemHeight = 16;
    this.listBox1.Location = new System.Drawing.Point(8, 16);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(240, 196);
    this.listBox1.TabIndex = 0;
    //
    // label1
    //
    this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
    this.label1.Location = new System.Drawing.Point(16, 224);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(232, 112);
    this.label1.TabIndex = 1;
    this.label1.Text = "label1";
    //
    // MainForm
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
    this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0)));
    this.ClientSize = new System.Drawing.Size(320, 344);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.listBox1);
    this.Name = "MainForm";
    this.Text = "MouseDown in a ListBox";
    this.Load += new System.EventHandler(this.MainFormLoad);
    this.ResumeLayout(false);
    }
    #endregion
    // respond to mouse events in the listBox
    private void ButtonDown(object sender, MouseEventArgs mea)
    {
    // the 0 based index of the row/item clicked
    int nDx = 0;
    if ( mea.Button == MouseButtons.Right )
    {
    label1.Text = "Right Button Clicked\n";
    }
    else if ( mea.Button == MouseButtons.Left )
    {
    label1.Text = "Left Button Clicked\n";
    }
    // calculate the index of the selected item or show error
    nDx = mea.Y / listBox1.ItemHeight;
    label1.Text = label1.Text +
    "Item height = " + listBox1.ItemHeight.ToString()+ "\n" +
    "X position = " + mea.X.ToString() + "\n" +
    "Y position = " + mea.Y.ToString() + "\n" +
    "Index = " + nDx.ToString();
    if ( listBox1.Items.Count <= nDx )
    {
    label1.Text = label1.Text + "\nRow clicked past items loaded";
    }
    else
    {
    label1.Text = label1.Text + "\nIndex = " + listBox1.Items[nDx];
    //highlight the selected row/item
    listBox1.SetSelected(nDx,true);
    }
    }
    // load the listBox with some names
    void MainFormLoad(object sender, System.EventArgs e)
    {
    listBox1.Items.Add("Helmut");
    listBox1.Items.Add("Helga");
    listBox1.Items.Add("Andreas");
    listBox1.Items.Add("Volger");
    listBox1.Items.Add("Kurt");
    listBox1.Items.Add("Erich");
    listBox1.Items.Add("Bjorn");
    listBox1.Items.Add("Lena");
    listBox1.Items.Add("Kristina");
    listBox1.Items.Add("Ulrike");
    listBox1.Items.Add("Heidrun");
    }
    }
    }

    Continue reading »
  3. Görev Yöneticisi

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Microsoft.Win32;
    using System.IO;
    using System.Diagnostics;
    using System.Resources;
    namespace Process_Manager
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    Process myProcess = new Process();
    //Declare process
    txtMachine.Text = System.Environment.MachineName.ToString();
    //set the machine name to textbox
    }
    private void FillList(string MachineName)
    {
    Process[] Prc;
    ListViewItem lvwP;
    Cursor.Current = Cursors.WaitCursor;
    //Set cusrsor as wait
    try
    {
    lvwProcesses.Items.Clear();
    Prc = Process.GetProcesses(MachineName.ToString());
    foreach (Process Prcs in Prc)
    {
    lvwP = lvwProcesses.Items.Add(Prcs.ProcessName.ToUpper());
    if (MachineName != System.Environment.MachineName)
    {
    lvwP.SubItems.Add("Unavailable...");
    lvwP.SubItems.Add("Unavailable...");
    lvwP.SubItems.Add(Prcs.Id.ToString());
    }
    else
    {
    lvwP.SubItems.Add(Prcs.MainWindowTitle);
    lvwP.SubItems.Add(Prcs.Responding.ToString());
    lvwP.SubItems.Add(Prcs.Id.ToString());
    }
    }
    }
    catch
    {
    lvwProcesses.Items.Add("Error enumerating items...");
    }
    Cursor.Current = Cursors.Default;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    FillList(txtMachine.Text);
    }
    private void cmdRefresh_Click(object sender, EventArgs e)
    {
    FillList(txtMachine.Text);
    }
    }
    }

    Continue reading »
  4. Geri Dönüşüm Kutusunu Boşaltma

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;
    namespace RBinTest
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    // İşlemi yapan kod bu kısımdır
    uint result = SHEmptyRecycleBin(IntPtr.Zero, null, 0);
    // Program açılışında geri dönüşüm kutusunu boşaltır
    }
    enum RecycleFlags : uint
    {
    SHERB_NOCONFIRMATION = 0x00000001,
    SHERB_NOPROGRESSUI = 0x00000001,
    SHERB_NOSOUND = 0x00000004
    }
    [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
    static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
    RecycleFlags dwFlags);
    }
    }

    Continue reading »
  5. Ekrana rastgele daireler çizmek

    /*
    * Created using SharpDevelop free C# IDE from
    * http://www.icsharpcode.net/opensource/sd/
    * User: vegaseat
    *
    * Draw a bunch of random circles or lines on a windows form
    * A very merry Windows Application
    */
    using System;
    using System.Drawing; // GDI+ stuff
    using System.Drawing.Imaging; // ImageFormat
    using System.Windows.Forms;
    namespace DrawLineCircle
    {
    // Summary description for Form1
    // a window with five buttons
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button btnLine;
    private System.Windows.Forms.Button btnCircle;
    private System.Windows.Forms.Button btnFill;
    private System.Windows.Forms.Button btnSave;
    private System.Windows.Forms.Button btnClear;
    private Bitmap DrawArea; // make a persistent drawing area
    // Required designer variable
    private System.ComponentModel.Container components = null;
    private Random rnd;
    private Pen myPen;
    public Form1()
    {
    InitializeComponent();
    rnd = new Random((int)DateTime.Now.Ticks); // seeded with ticks
    myPen = new Pen(Color.Red);
    }
    // Clean up any resources being used
    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.btnCircle = new System.Windows.Forms.Button();
    this.btnSave = new System.Windows.Forms.Button();
    this.btnLine = new System.Windows.Forms.Button();
    this.btnFill = new System.Windows.Forms.Button();
    this.btnClear = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // btnCircle
    //
    this.btnCircle.Location = new System.Drawing.Point(136, 296);
    this.btnCircle.Name = "btnCircle";
    this.btnCircle.Size = new System.Drawing.Size(56, 20);
    this.btnCircle.TabIndex = 0;
    this.btnCircle.Text = "Circle";
    this.btnCircle.Click += new System.EventHandler(this.btnCircle_Click);
    //
    // btnSave
    //
    this.btnSave.Location = new System.Drawing.Point(328, 296);
    this.btnSave.Name = "btnSave";
    this.btnSave.Size = new System.Drawing.Size(48, 20);
    this.btnSave.TabIndex = 0;
    this.btnSave.Text = "Save";
    this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
    //
    // btnLine
    //
    this.btnLine.Location = new System.Drawing.Point(264, 296);
    this.btnLine.Name = "btnLine";
    this.btnLine.Size = new System.Drawing.Size(54, 20);
    this.btnLine.TabIndex = 1;
    this.btnLine.Text = "Line";
    this.btnLine.Click += new System.EventHandler(this.btnLine_Click);
    //
    // btnFill
    //
    this.btnFill.Location = new System.Drawing.Point(200, 296);
    this.btnFill.Name = "btnFill";
    this.btnFill.Size = new System.Drawing.Size(56, 20);
    this.btnFill.TabIndex = 2;
    this.btnFill.Text = "FCircle";
    this.btnFill.Click += new System.EventHandler(this.btnFill_Click);
    //
    // btnClear
    //
    this.btnClear.Location = new System.Drawing.Point(8, 296);
    this.btnClear.Name = "btnClear";
    this.btnClear.Size = new System.Drawing.Size(56, 20);
    this.btnClear.TabIndex = 3;
    this.btnClear.Text = "Clear";
    this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
    this.ClientSize = new System.Drawing.Size(380, 325);
    this.Controls.Add(this.btnClear);
    this.Controls.Add(this.btnFill);
    this.Controls.Add(this.btnSave);
    this.Controls.Add(this.btnLine);
    this.Controls.Add(this.btnCircle);
    this.Name = "Form1";
    this.Text = "Draw a few circles ...";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Closed += new System.EventHandler(this.Form1_Closed);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);
    }
    #endregion
    //
    // This is the main entry point for the application.
    //
    public static void Main()
    {
    Application.Run(new Form1());
    }
    // Line button click event
    private void btnLine_Click(object sender, System.EventArgs e)
    {
    Graphics xGraph;
    int k;
    xGraph = Graphics.FromImage(DrawArea);
    for(k = 1; k <>
    {
    myPen.Color = Color.FromArgb(
    (rnd.Next(0,255)),
    (rnd.Next(0,255)),
    (rnd.Next(0,255)));
    xGraph.DrawLine(
    myPen,
    (int) rnd.Next(0, this.Width),
    (int) rnd.Next(0, this.Height),
    (int) rnd.Next(0, this.Width),
    (int) rnd.Next(0, this.Height));
    }
    xGraph.Dispose();
    this.Invalidate();
    }
    // Circle button click event
    private void btnCircle_Click(object sender, System.EventArgs e)
    {
    Graphics xGraph;
    int k;
    int r; // radius of circle
    int x, y; // center coordinates of circle
    xGraph = Graphics.FromImage(DrawArea);
    for(k = 1; k <>
    {
    // radius for circle, max 1/2 the width of the form
    r = rnd.Next(0, (this.Width / 2));
    x = rnd.Next(0, this.Width);
    y = rnd.Next(0, this.Height);
    myPen.Color = Color.FromArgb(
    (rnd.Next(0,255)),
    (rnd.Next(0,255)),
    (rnd.Next(0,255)));
    // convert centerX, centerY, radius to bounding rectangle
    xGraph.DrawEllipse( myPen, x-r, y-r, r, r );
    }
    xGraph.Dispose();
    this.Invalidate();
    }
    // FCircle (solid circle) button click event
    private void btnFill_Click(object sender, System.EventArgs e)
    {
    Graphics xGraph;
    int k;
    int r; // radius of circle
    int x, y; // center coordinates of circle
    xGraph = Graphics.FromImage(DrawArea);
    // Create solid brush.
    SolidBrush Brush = new SolidBrush(Color.Red);
    for(k = 1; k <>
    {
    // radius for circle, max 1/2 the width of the form
    r = rnd.Next(0, (this.Width / 2));
    x = rnd.Next(0, this.Width);
    y = rnd.Next(0, this.Height);
    Brush.Color = Color.FromArgb(
    (rnd.Next(0,255)),
    (rnd.Next(0,255)),
    (rnd.Next(0,255)));
    // convert centerX, centerY, radius to bounding rectangle
    xGraph.FillEllipse( Brush, x-r, y-r, r, r );
    }
    xGraph.Dispose();
    this.Invalidate();
    }
    // form load event
    private void Form1_Load(object sender, System.EventArgs e)
    {
    DrawArea = new Bitmap(this.ClientRectangle.Width,
    this.ClientRectangle.Height,
    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    InitializeDrawArea();
    }
    private void InitializeDrawArea()
    {
    Graphics xGraph;
    xGraph = Graphics.FromImage(DrawArea);
    // clear the drawing area to background color
    xGraph.Clear(Color.LightYellow);
    }
    // free up resources on program exit
    private void Form1_Closed(object sender, System.EventArgs e)
    {
    DrawArea.Dispose();
    }
    // paint event
    private void Form1_Paint(object sender,
    System.Windows.Forms.PaintEventArgs e)
    {
    Graphics xGraph;
    xGraph = e.Graphics;
    xGraph.DrawImage(DrawArea,0,0,DrawArea.Width,DrawArea.Height);
    xGraph.Dispose();
    }
    // save drawing in bitmap DrawArea as a jpeg file
    private void btnSave_Click(object sender, System.EventArgs e)
    {
    ImageFormat format = ImageFormat.Jpeg;
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "JPEG Files(*.jpg)|*.jpg";
    if (sfd.ShowDialog() == DialogResult.OK)
    {
    // now save the image in the DrawArea
    DrawArea.Save( sfd.FileName, format );
    }
    }
    // clear the DrawArea
    private void btnClear_Click(object sender, System.EventArgs e)
    {
    Graphics xGraph;
    xGraph = Graphics.FromImage(DrawArea);
    // clear the drawing area to bg color
    xGraph.Clear(Color.LightYellow);
    // free up resource
    xGraph.Dispose();
    // update
    this.Invalidate();
    }
    }
    }

    Continue reading »
  6. Dizideki tekrarlanan elemanları silme

    // Fonksiyon Kodları
    public static Array DeleteDuplicates(Array arr)
    {
    // this procedure works only with vectors
    if (arr.Rank != 1 )
    throw new ArgumentException("Multiple-dimension arrays are not supported");
    // we use a hashtable to track duplicates
    // make the hash table large enough to avoid memory re-allocations
    Hashtable ht = new Hashtable(arr.Length * 2);
    // we will store unique elements in this ArrayList
    ArrayList elements = new ArrayList();
    foreach (object Value in arr)
    {
    if ( !ht.Contains(Value) )
    {
    // we've found a non duplicate
    elements.Add(Value);
    // remember it for later
    ht.Add(Value, null);
    }
    }
    // return an array of same type as the original array
    return elements.ToArray(arr.GetType().GetElementType());
    }
    // Örnek Kullanım:
    int[] numbers = new int[] {1, 3, 5, 2, 3, 1, 4};
    int[] result = (int[]) DropDuplicates(numbers);
    foreach (int num in result)
    Console.WriteLine(num);

    Continue reading »