c# sample Coding
Saturday, 1 August 2015
Monday, 6 July 2015
Remove duplicate strings from a string or int array in C#
string[] spdfIn =
{ "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
spdfIn = spdfIn.Distinct().ToArray();
int[] int1={1,2,3,4,4,5,1};
int1=int1
.Distinct().ToArray();
Friday, 24 April 2015
Panel printing
private void btnPrint_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new PrintDocument();
doc.PrintPage += this.Doc_PrintPage1;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
//if (dlgSettings.ShowDialog() == DialogResult.OK) // hide this line default Printer is select
{
doc.Print();
}
}
private void Doc_PrintPage1(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(this.pnlPrint.Width, this.pnlPrint.Height);
this.pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, this.pnlPrint.Width, this.pnlPrint.Height));
e.Graphics.DrawImage((Image)bmp, 0, 0);
}
{
System.Drawing.Printing.PrintDocument doc = new PrintDocument();
doc.PrintPage += this.Doc_PrintPage1;
PrintDialog dlgSettings = new PrintDialog();
dlgSettings.Document = doc;
//if (dlgSettings.ShowDialog() == DialogResult.OK) // hide this line default Printer is select
{
doc.Print();
}
}
private void Doc_PrintPage1(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(this.pnlPrint.Width, this.pnlPrint.Height);
this.pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, this.pnlPrint.Width, this.pnlPrint.Height));
e.Graphics.DrawImage((Image)bmp, 0, 0);
}
Tuesday, 24 March 2015
Get Default Printer paper size and Printing Text
public void getPapersize() //Get Default Printer paper size
{
using (System.Drawing.Printing.PrintDocument PD = new System.Drawing.Printing.PrintDocument())
{
foreach (System.Drawing.Printing.PaperSize P in PD.PrinterSettings.PaperSizes)
{
Console.Write(P.PaperName + " ");
double W = P.Width / 100.0;
double H = P.Height / 100.0;
double[] M = { 1, 72, 200 };
for (int I = 0; I <= 2; I++)
{
MessageBox.Show((W * M[I]).ToString() + " " + (H * M[I]).ToString() + " ");
}
}
}
}
------------------------------
public void Print(string s)
{
System.Drawing.Printing.PrintDocument PD = new System.Drawing.Printing.PrintDocument();
PD.PrintController = new System.Drawing.Printing.StandardPrintController();
PD.PrintPage += delegate(object sender1, System.Drawing.Printing.PrintPageEventArgs e1)
{
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, PD.DefaultPageSettings.PrintableArea.Width, PD.DefaultPageSettings.PrintableArea.Height));
};
try
{
PD.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
{
using (System.Drawing.Printing.PrintDocument PD = new System.Drawing.Printing.PrintDocument())
{
foreach (System.Drawing.Printing.PaperSize P in PD.PrinterSettings.PaperSizes)
{
Console.Write(P.PaperName + " ");
double W = P.Width / 100.0;
double H = P.Height / 100.0;
double[] M = { 1, 72, 200 };
for (int I = 0; I <= 2; I++)
{
MessageBox.Show((W * M[I]).ToString() + " " + (H * M[I]).ToString() + " ");
}
}
}
}
------------------------------
public void Print(string s)
{
System.Drawing.Printing.PrintDocument PD = new System.Drawing.Printing.PrintDocument();
PD.PrintController = new System.Drawing.Printing.StandardPrintController();
PD.PrintPage += delegate(object sender1, System.Drawing.Printing.PrintPageEventArgs e1)
{
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, PD.DefaultPageSettings.PrintableArea.Width, PD.DefaultPageSettings.PrintableArea.Height));
};
try
{
PD.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
Monday, 1 September 2014
If string is not null or empty else
string.IsNullOrEmpty(someString) ? "<NULL>" : "<not NULL / Empty>";
Tuesday, 15 July 2014
How to hide Close (x) button for Windows Forms?
Method 1
this.ControlBox=false;
Method 2
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
return myCp;
}
}
Tuesday, 1 April 2014
Configure Internet Explorer to open Ms Office (Word,Excel,Power Point Etc)documents
Method 1
Open My Computer and On the Tools menu click Folder Options.
Click the File Types tab.In the Registered file types list, click the specific Office document type (for example, Excel), and then click Advanced .
In the Edit File Type dialog box, click the Browse in same window check box .and click clear other check box
Click OK 2 times.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(@"C:\sample.xls");
}
Open My Computer and On the Tools menu click Folder Options.
Click the File Types tab.In the Registered file types list, click the specific Office document type (for example, Excel), and then click Advanced .
In the Edit File Type dialog box, click the Browse in same window check box .and click clear other check box
Click OK 2 times.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(@"C:\sample.xls");
}
Subscribe to:
Posts (Atom)