PDA

View Full Version : سوال: استفاده از متقير سراسري در DataGridViewPrinter.cs



hallaji
پنج شنبه 03 مرداد 1387, 23:43 عصر
با عرض سلام خدمت دوستان عزيز و برنامه نويسان گرامي

كلاس DataGridViewPrinter.cs من كه عمل چاپ كردن ديتا گريدويو را انجام ميدهد بصورت زير است


using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
class DataGridViewPrinter
{
private DataGridView TheDataGridView; // The DataGridView Control which will be printed
private PrintDocument ThePrintDocument; // The PrintDocument to be used for printing
private bool IsCenterOnPage; // Determine if the report will be printed in the Top-Center of the page
private bool IsWithTitle; // Determine if the page contain title text
private string TheTitleText; // The title text to be printed in each page (if IsWithTitle is set to true)
private Font TheTitleFont; // The font to be used with the title text (if IsWithTitle is set to true)
private Color TheTitleColor; // The color to be used with the title text (if IsWithTitle is set to true)
private bool IsWithPaging; // Determine if paging is used
private bool IsWithz;
static int CurrentRow; // A static parameter that keep track on which Row (in the DataGridView control) that should be printed
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY; // A parameter that keep track on the y coordinate of the page, so the next object to be printed will start from this y coordinate
private float CurrentZ;
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
// Maintain a generic list to hold start/stop points for the column printing
// This will be used for wrapping in situations where the DataGridView will not fit on a single page
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
// The class constructor

public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging,bool WithZ)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
IsWithz = WithZ;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
// Claculating the PageWidth and the PageHeight
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Wid th;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Hei ght;
}
// Claculating the page margins
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right ;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Botto m;
// First, the current row to be printed is the first row in the DataGridView control
CurrentRow = 0;
}
// The function that calculate the height of each row (including the header row), the width of each column (according to the longest text in all its cells including the header cell), and the whole DataGridView width
private void Calculate(Graphics g)
{
if (PageNumber == 0) // Just calculate once
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView.Columns.Count; i++)
{
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font ;
if (tmpFont == null) // If there is no special HeaderFont style, then use the default DataGridView font style
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
tmpWidth = 220; ////tmpWidth = tmpSize.Width;
RowHeaderHeight = 40;////RowHeaderHeight = tmpSize.Height;
for (int j = 0; j < TheDataGridView.Rows.Count; j++)
{
tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
if (tmpFont == null) // If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString("Anything", tmpFont);
RowsHeight.Add(tmpSize.Height);
tmpSize = g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
if (tmpSize.Width > tmpWidth)
tmpWidth = tmpSize.Width;
}
if (TheDataGridView.Columns[i].Visible)
TheDataGridViewWidth += tmpWidth;
ColumnsWidth.Add(tmpWidth);
}
// Define the start/stop column points based on the page width and the DataGridView Width
// We will use this to determine the columns which are drawn on each page and how wrapping will be handled
// By default, the wrapping will occurr such that the maximum number of columns for a page will be determine
int k;
int mStartPoint = 0;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
if (TheDataGridView.Columns[k].Visible)
{
mStartPoint = k;
break;
}
int mEndPoint = TheDataGridView.Columns.Count;
for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
if (TheDataGridView.Columns[k].Visible)
{
mEndPoint = k + 1;
break;
}
float mTempWidth = TheDataGridViewWidth;
float mTempPrintArea = (float)PageWidth - (float)LeftMargin - (float)RightMargin;
// We only care about handling where the total datagridview width is bigger then the print area
if (TheDataGridViewWidth > mTempPrintArea)
{
mTempWidth = 0.0F;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
{
if (TheDataGridView.Columns[k].Visible)
{
mTempWidth += ColumnsWidth[k];
// If the width is bigger than the page area, then define a new column print range
if (mTempWidth > mTempPrintArea)
{
mTempWidth -= ColumnsWidth[k];
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mStartPoint = k;
mTempWidth = ColumnsWidth[k];
}
}
// Our end point is actually one index above the current index
mEndPoint = k + 1;
}
}
// Add the last set of columns
mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
mColumnPointsWidth.Add(mTempWidth);
mColumnPoint = 0;
}
}
// The funtion that print the title, page number, and the header row

private void DrawHeader(Graphics g)
{
CurrentY = (float)TopMargin;
CurrentZ = (float) TopMargin;
// Printing the page number (if isWithPaging is set to true)
if (IsWithPaging)
{
PageNumber++;
string PageString = "شركت سهامي .......
StringFormat PageStringFormat = new StringFormat();
PageStringFormat.Trimming = StringTrimming.Word;
PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
PageStringFormat.Alignment = StringAlignment.Center;
Font PageStringFont = new Font("Zar Mazar", 20, FontStyle.Bold, GraphicsUnit.Point);
RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, PageStringFont).Height);
g.DrawString(PageString, PageStringFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);
CurrentY += g.MeasureString(PageString, PageStringFont).Height;
}

/////////////////////////////////////////////
if (IsWithz)
{
string ZPageString ="\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n: صاحب جمع اموال : امور اداري ناظر: تحويل گيرنده ";

StringFormat ZStringFormat = new StringFormat();
ZStringFormat.Trimming = StringTrimming.Word;
ZStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
ZStringFormat.Alignment = StringAlignment.Far;
Font ZPageStringFont = new Font("Zar Mazar", 14, FontStyle.Bold, GraphicsUnit.Point);

RectangleF ZPageSringRectangle = new RectangleF((float)LeftMargin, CurrentZ, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(ZPageString,ZPageStringFont).Heigh t);
g.DrawString(ZPageString, ZPageStringFont, new SolidBrush(Color.Black), ZPageSringRectangle, ZStringFormat);
CurrentZ -= g.MeasureString(ZPageString, ZPageStringFont).Height;
}
////////////////////////////////////////////////
// Printing the title (if IsWithTitle is set to true)
if (IsWithTitle)

حالا سوالي كه داشتم اينه چطور ميتوانم مقدار pagestring را از textbox1.text فرم 1 بگيرم
(نتيجه جستجوهاي بنده استفاده از متغيير سراسري بين چند فرم بود)
--------------------------------------------------------------------------------------
هيچ جايي مثله برنامه نويسي نميشه

hallaji
جمعه 04 مرداد 1387, 18:02 عصر
با سلام مجدد خدمت دوستان
دوستان عزيز مشكل بنده حل شدني است ؟
من هنوز منتظر كمك و راهنمايي از دوستان عزيز هستم
+----------------------------------------------------------+
هيج جايي مثله برنامه نويسي نميشه

hdy_rasool
شنبه 05 مرداد 1387, 02:02 صبح
من برنامت رو نخوندم ولی برای استفاده از متغیر سراسری می تونی مقدار TextBox رو داخل یه متغیر static بریزی بعد از هر جا که خواستی با نام کلاسی که عضوش به فرم ClassName.staticVariable بهش دسترسی پیداکنی
این تعریف متغییر static


public partial class Form1 : Form
{
public static string textboxVal;
public Form1()
{
InitializeComponent();
}
}

hallaji
شنبه 05 مرداد 1387, 06:52 صبح
با سلام مجدد خدمت دوستان و برنامه نويسان عزيز

حالا با چه دستوري (چطوري) ميتونم از textboxval در DataGridViewPrinter.cs (همون كدي كه بالا نوشتم) استفاده كنم ؟

+-------------------------------------------------------------------------------------------+
آنچه كه شما را از ديگران متمايز ميسازد قدرت اراده تان است

hallaji
شنبه 05 مرداد 1387, 18:07 عصر
با سلام
دوستان عزيز منتظر راهنمايي تان هستم
اگه لطف كنيد خيلي ممنون ميشم
-----------------------------------------------------

linux
شنبه 05 مرداد 1387, 18:36 عصر
با سلام مجدد خدمت دوستان
دوستان عزيز مشكل بنده حل شدني است ؟
من هنوز منتظر كمك و راهنمايي از دوستان عزيز هستم
+----------------------------------------------------------+
هيج جايي مثله برنامه نويسي نميشه
چرا پروپرتی تعریف نمی کنی که بتوانی قبل از اجرای متد اصلی کلاس مقدار دهی کنی؟

hallaji
یک شنبه 06 مرداد 1387, 11:39 صبح
با سلام مجدد خدمت برنامه نويسان عزيز

ميشه بفرمايين با چه كدي اين كار رو انجام بدم؟
(يعني تعريف پروپرتي را چطور انجام بدم ؟)

+++++++++++++++++++++++++++++++++++++
يج جايي مثله برنامه نويسي نميشه

nilmil_nil
سه شنبه 08 مرداد 1387, 12:06 عصر
دوست عزیز شما می تونی خاصیت Modifier تکست باکس رو به Public تغییر دهید و بعد از اون مقدارش رو به هر جا که خواستی بفرست یا بگیر




frm.txt.text="dddd"
string str;
str=frm.txt.text;

hallaji
چهارشنبه 09 مرداد 1387, 22:02 عصر
با سلام خدمت برنامه نويسان گرامي
---------------------------------------------
بنده با تغيير خاصیت Modifier تكست باكس1 به public انو ميتونم در بقيه فرم ها با استفاده از كد زير دسترسي داشته باشم .


form1 p = new form1();
string kol= p.txtbox1.Text;

ولي نميتونم در يك كلاس ارزش استفاده كنم
بطور مثال در كلاس m :


public class m
{
string title = "aaaa";
string kol = ????

}

حالا ميخواهم مقدار kol را در اينجا را هم برابر textbox1.text قرار بدهم
دوستان عزيز اگه لطف كنند و راهنمايي كنند ممنون ميشم
------------------------------------------------------------
هيچ جايي مثله برنامه نويسي نمشه.

hallaji
شنبه 12 مرداد 1387, 00:38 صبح
با سلام
دوستان تايپك رو بالا آوردم كه در صورت امكان كمك كنيد

--------------------------------------------------------------------
هيج جايي مثله برنامه نويسي نمشه