-3
我想用指定的字體打印一個變量,但字符串爲空,因此在輸出中沒有任何內容可見。請通過代碼,並幫助我找到錯誤無法訪問派生類中的公共變量
class BasicClass
{
public string str;
public Font fnt;
}
class BasicMethod:BasicClass
{
public void changevalues(string newstr,Font newfnt)
{
str = newstr;
fnt = newfnt;
}
}
class PrintClass:BasicClass
{
public void print()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
PaperSize psize = new PaperSize();
pdoc.DefaultPageSettings.Landscape = true;
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psize;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult result = pd.ShowDialog();
if (result == DialogResult.OK)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pdoc;
ppd.PrintPreviewControl.Zoom = 1.5;
((Form)ppd).WindowState = FormWindowState.Maximized;
DialogResult ppdResult = ppd.ShowDialog();
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//string str1 = "XYZ";
//Font fnt1 = new Font("Arial", 12.5f);
g.DrawString(str, fnt, new SolidBrush(Color.Black), 10, 10);
}
}
按鈕單擊事件
private void button1_Click(object sender, EventArgs e)
{
BasicMethod bm = new BasicMethod();
PrintClass pc = new PrintClass();
Font ft = new System.Drawing.Font("Arial", 12.5f);
bm.changevalues("Hello", ft);
pc.print();
}
我需要得到輸出Hello
恭喜回答!你應該去你以前的問題,並添加一個答案鏈接到這個頁面上的答案。 – 2014-11-05 19:42:37