تاپیک جامع نکات در سی شارپ
Arrow خواندن و نوشتن رنگ یک پیکسل به کمک کلاس Bitmap
کد:
using System.Drawing.Imaging;
کد:
Bitmap bmp = new Bitmap(picturebox1.Image);
//read
Color c = bmp.GetPixel(1, 1);
//write
bmp.SetPixel(2, 2, c);
Arrow یک روش ساده برای افزودن تصویر به TextBox
کد:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pic = new PictureBox();
//pic.Image = Image.FromFile("آدرس فایل");
pic.Image = SystemIcons.Information.ToBitmap();;
textBox1.Controls.Add(pic);
}
Arrow Shuffle کردن لیست : (چینش تصادفی لیست)
کد:
List<int> list2 = new List<int> { 6, 7, 12, 18, 2 };
List<int> list3 = list2.OrderBy(i => (Guid.NewGuid())).ToList<int>();
اجتماع دو لیست :
کد:
List<int> list = new List<int> { 4, 5, 3, 6, 3, 5, 5, 5, 9, 7, 3 };
List<int> list2 = new List<int> { 6, 7, 12, 18, 2 };
List<int> list3 = list.Union(list2).ToList<int>();//4,5,3,6,9,7,12,18,2
Arrow پاک کردن هم زمان تمام TextBox ها
کد:
public void ClearTextBoxs()
{
foreach (Control txt in this.Controls)
{
if (txt.GetType().Name == "TextBox")
txt.Text = "";
}
}
Arrow چک کردن ورودی کاربر برای مقادیر عددی
کد:
Console.Write("Enter a number for check: ");
int i = 0;
string inputNumber = Console.ReadLine();
if (int.TryParse(inputNumber, out i))
{
Console.WriteLine("Valid format");
}
else
{
Console.WriteLine("Not valid format");
}
Console.ReadKey()
Arrow وارد کردن لیست باکس به اسکرول تا یک آیتم مشخص
کد:
listBox1.TopIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = listBox1.Items.Count - 1;
Arrow اضافه کردن چند واحد در progressBar از طریق trackBar
کد:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
progressBar1.Value = trackBar1.Value + 6;
}
}
}
Arrow Crop کردن یک تصویر (یک برش مستطیلی از تصویر)
کد:
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
Arrow بدست آوردن لیست چاپگرهای نصب شده در یک سیستم
کد:
using System.Drawing.Printing;
کد:
private void GetInstalledPrinters()
{
foreach (string printerName in PrinterSettings.InstalledPrinters)
MessageBox.Show(printerName);
}
Arrow پخش کردن برخی صداهای سیستمی
کد:
// Play a beep with default frequency
// and duration (800 and 200, respectively)
Console.Beep();
// Play a beep with frequency as 200 and duration as 300
Console.Beep(200, 300);
یا
کد:
SystemSounds.Asterisk.Play();
SystemSounds.Hand.Play();
SystemSounds.Exclamation.Play();
SystemSounds.Beep.Play();
SystemSounds.Question.Play();
Arrow برخی اعمال متدوال روی تاریخ میلادی
کد:
// Create a TimeSpan representing 2.5 days.
TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
// Create a TimeSpan representing 4.5 days.
TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
// Create a TimeSpan representing 1 week.
TimeSpan oneWeek = timespan1 + timespan2;
// Create a DateTime with the current date and time.
DateTime now = DateTime.Now;
// Create a DateTime representing 1 week ago.
DateTime past = now - oneWeek;
// Create a DateTime representing 1 week in the future.
DateTime future = now + oneWeek;
Arrow دستکاری خواص (Attribute) یک فایل
کد:
public static void ModifyFileTimestamps(string path)
{
File.SetCreationTime(path, DateTime.Parse(@"May 10, 2003"));
File.SetLastAccessTime(path, DateTime.Parse(@"May 10, 2003"));
File.SetLastWriteTime(path, DateTime.Parse(@"May 10, 2003"));
}
کد:
public static void ModifyTimestamps(FileInfo fileInfo, DateTime dt)
{
fileInfo.CreationTime = dt;
fileInfo.LastAccessTime = dt;
fileInfo.LastWriteTime = dt;
}
کد:
public static void MakeFileHidden(FileInfo fileInfo)
{
// Modify this file's attributes
fileInfo.Attributes |= FileAttributes.Hidden;
}
Arrow مینی مایز کردن تمامی پنجره های ویندوزی با استفاده از Invoke
کد:
Type typeS = null;
object os = Type.Missing;
typeS = Type.GetTypeFromProgID("Shell.Application");
os = Activator.CreateInstance(typeS);
typeS.InvokeMember
("MinimizeAll", System.Reflection.BindingFlags.InvokeMethod, null, os, null);
Arrow جلوه دادن به دکمه
وارد کلاس program میشیم و این دستورات رو وارد کنیم
کد:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppSoftwareHesabDarePoshak
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new frmLoad());
}
#region Just Style the Buttons
public static void MakebuttonStyle(Button button)
{
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
}
private static void button_MouseLeave(object sender, EventArgs e)
{
Button button = (Button)sender;
button.FlatStyle = FlatStyle.Standard;
button.Font = new System.Drawing.Font(button.Font.FontFamily, button.Font.Size);
button.ForeColor = System.Drawing.SystemColors.ControlText;
}
private static void button_MouseEnter(object sender, EventArgs e)
{
Button button = (Button)sender;
button.FlatStyle = FlatStyle.Flat;
button.Font = new System.Drawing.Font(button.Font.FontFamily, button.Font.Size, System.Drawing.FontStyle.Bold);
button.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
}
#endregion
}
}
Arrow پاک کردن مقادیر تمامی تکست باکس های درون فرم +label
کد:
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
label1.Visible = false;
}
}