我试图从文本框中捕获值输入(是的,我已经验证它有一个niput并且不为null)。我声明了一个类变量,以确保我正在关联正确的表单。但是,我的语法似乎永远不会分配值,它会到达赋值行然后跳到PrintPreviewDlg.ShowDialog();
并抛出错误
未处理的类型' System.ArgumentNullException'发生在System.Windows.Forms.dll中 附加信息:值不能为空。
这是我的语法
public static LiveBait MainInstance { get; set; }
private void btnPreview_Click(object sender, EventArgs e)
{
PrintPreviewDialog PrintPreviewDlg = new PrintPreviewDialog();
PrintPreviewDlg.ClientSize = new System.Drawing.Size(400, 300);
PrintPreviewDlg.Location = new System.Drawing.Point(29, 29);
PrintPreviewDlg.Name = "PrintPreviewDlg";
PrintPreviewDlg.MinimumSize = new System.Drawing.Size(375, 250);
PrintPreviewDlg.WindowState = FormWindowState.Maximized;
PrintPreviewDlg.UseAntiAlias = true;
dynamic PD = CreatePrintDocument();
PD.DefaultPageSettings.Landscape = true;
PrintPreviewDlg.Document = PD;
PrintPreviewDlg.ShowDialog();
}
PD CreatePrintDocument()
{
PD document = new PD();
document.SetParentCtrl(this);
document.PrintData.MagRead = MainInstance.txtMagazineRead.Text;
//More captures here
return document;
}
class PraDa
{
public string MagRead;
}
为什么我的变量永远不会被设置,我必须做什么才能确保它被设置为使btn click事件不会出错?
修改
COpying到剪贴板产生了这个堆栈跟踪
System.ArgumentNullException未处理 的HResult = -2147467261 Message = Value不能为null。 参数名称:args PARAMNAME = ARGS 来源= mscorlib程序 堆栈跟踪: 在System.String.Format(String format,Object [] args) 在ProdData.PD.OnPrintPage(PrintPageEventArgs e)中的C:\ Users \ Owner \ Dropbox \ ProdData \ PD.cs:第234行 在System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e) 在System.Drawing.Printing.PrintController.PrintLoop(PrintDocument文档) 在System.Drawing.Printing.PrintController.Print(PrintDocument文档) 在System.Drawing.Printing.PrintDocument.Print() 在System.Windows.Forms.PrintPreviewControl.ComputePreview() 在System.Windows.Forms.PrintPreviewControl.CalculatePageInfo() 在System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) 在System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) 在System.Windows.Forms.Control.InvokeMarshaledCallbacks() 在System.Windows.Forms.Control.WndProc(消息& m) 在System.Windows.Forms.PrintPreviewControl.WndProc(消息& m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.RunDialog(表单表单) 在System.Windows.Forms.Form.ShowDialog(IWin32Window所有者) 在System.Windows.Forms.Form.ShowDialog() 在C:\ Users \ Owner \ Dropbox \ Drl.cs中的ProdData.Controls.DrawingStageCtrl.btnPreview_Click(Object sender,EventArgs e):第889行 在System.Windows.Forms.Control.OnClick(EventArgs e) 在System.Windows.Forms.Button.OnClick(EventArgs e) 在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击) 在System.Windows.Forms.Control.WndProc(消息& m) 在System.Windows.Forms.ButtonBase.WndProc(消息& m) 在System.Windows.Forms.Button.WndProc(消息& m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.Run(Form mainForm) 在C:\ Users \ Owner \ Dropbox \ Program.cs中的ProdData.Program.Main():第18行 InnerException:
编辑2
这是我所要求的方法的语法,希望这将有助于解决问题所在。
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
int printHeight = 0;
int printWidth = 0;
int rightMargin = 0;
PaperSize ps = default(PaperSize);
for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++)
{
if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4)
{
ps = PrinterSettings.PaperSizes[ix];
DefaultPageSettings.PaperSize = ps;
break;
}
}
DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN;
DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN;
DefaultPageSettings.Landscape = true;
var CurrentPageSettings = base.DefaultPageSettings;
printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom;
printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right;
m_leftMargin = CurrentPageSettings.Margins.Left; //X
rightMargin = CurrentPageSettings.Margins.Top; //Y
//Create a rectangle printing are for our document
m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight);
// Get Normal Row Height
int charactersFitted = 0;
int linesFilled = 0;
SizeF TextSize = new SizeF();
StringFormat textFormat = new StringFormat();
//Tell it to Alignment Text in its rectangle
textFormat.Alignment = StringAlignment.Near;
textFormat.FormatFlags = StringFormatFlags.NoClip;
TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
m_NormalRowHeight = (int)TextSize.Height + 3; //Row is bigger than text
//Draw First Heading
Rectangle MainHeaderRect = new Rectangle();
DrawMainHeading(e, LogoRect, ref MainHeaderRect);
m_iCurrentLocationY = MainHeaderRect.Bottom + (m_NormalRowHeight);
int LeftSubHeadingWidth = 200;
m_SubHeaderTextFieldSize = new Size(LeftSubHeadingWidth, m_NormalRowHeight);
m_TextValuePairSize = new Size(m_SubHeaderTextFieldSize.Width / 30, m_SubHeaderTextFieldSize.Height);
bool IPN = !string.IsNullOrEmpty(MainInstance.txtPN.Text);
bool IPJN = !string.IsNullOrEmpty(MainInstance.txtPJN.Text);
bool IDt = !string.IsNullOrEmpty(MainInstance.dateIDt.Text);
bool IPB = !string.IsNullOrEmpty(MainInstance.txtIPB.Text);
bool ISS = !string.IsNullOrEmpty(MainInstance.txtISS.Text);
string ST = !IPN ? String.Format("IPN:: ", MainInstance.txtPN.Text) : String.Format("IPN:: ", null);
string pnt = !IPJN ? String.Format( MainInstance.txtPJN.Text) : String.Format(null);
string dt = !IDt ? String.Format(MainInstance.dateIDt.Text) : String.Format(null);
string pbt = !IPB ? String.Format(MainInstance.txtIPB.Text) : String.Format(null);
string ST = !ISS ? String.Format(MainInstance.txtISS.Text) : String.Format(null);
m_iCurrentLocationY += (int)(1.5 * m_NormalRowHeight);
int iAlphaStart_Y = m_iCurrentLocationY;
DrawSubHeading(e, ST, new Rectangle(new Point(m_leftMargin, m_iCurrentLocationY), m_SubHeaderTextFieldSize), StringAlignment.Near);
DrawPairsToPage(e, "IPJN: ", pnt, true, m_leftMargin);
DrawPairsToPage(e, "Date: ", dt, true, m_leftMargin);
DrawPairsToPage(e, "IPB: ", pbt, true, m_leftMargin);
DrawPairsToPage(e, "ISS: ", ST, true, m_leftMargin);
Panel DrawingArea = m_ParentCtrl.GetDrawingArea();
Bitmap pota = new Bitmap(DrawingArea.Width, DrawingArea.Height);
m_ParentCtrl.ShowDetails(true);
foreach (Control c in DrawingArea.Controls)
{
c.BringToFront();
}
DrawingArea.DrawToBitmap(pota, new Rectangle(0, 0, DrawingArea.Width, DrawingArea.Height));
foreach (Control c in DrawingArea.Controls)
{
c.BringToFront();
}
m_ParentCtrl.ShowDetails(false);
int iDrawingY = iAlphaStart_Y + (5 * m_NormalRowHeight);
e.Graphics.DrawImage(pota, new Rectangle(m_leftMargin, iDrawingY, printWidth, printHeight - iDrawingY));
}
}
答案 0 :(得分:1)
stacktrace告诉你ArgumentNull异常的确切位置:
C:\ Users \ Owner \ Dropbox \ ProdData \ PD.cs:第234行
它还告诉你它正在调用String.Format
我的orbuculum让我相信它就在这一行(为了便于阅读而格式化):
string ST = !IPN ?
String.Format("IPN:: ", MainInstance.txtPN.Text) :
String.Format("IPN:: ", null);
documenation也列出了这些例外情况。
这条线有几个问题。
首先String.Format("IPN:: ", null);
是问题的原因,也没有多大意义。为什么要格式化null的东西呢?只需返回纯字符串。
其次String.Format("IPN:: ", MainInstance.txtPN.Text)
不会抛出,但会始终输出IPN::
,因为您忘记在格式字符串中包含{0}
以指示参数应该去的位置。
最后String.Format(null)
也会抛出异常。
我建议你介绍一个帮助器方法,这样就可以替换整个布尔值juggle
// takes any Control and return an emtpy string if Text is null or empty
string GetTextOrEmpty(Control ctl)
{
var txt = ctl.Text; // base implementation of Text doesn't return null
if (String.IsNullOrEmpty(txt)) {
return String.Empty;
} else {
return txt;
}
}
更像这样的东西:
string ST = String.Format("IPN:: {0}", GetTextOrEmpty(MainInstance.txtPN));
string pnt = GetTextOrEmpty(MainInstance.txtPJN.Text);
string dt = GetTextOrEmpty(MainInstance.dateIDt.Text);
string pbt = GetTextOrEmpty(MainInstance.txtIPB.Text);
string ST = GetTextOrEmpty((MainInstance.txtISS.Text);
这将解决参数args的ArgumentNull异常以及参数格式,但是你仍然需要遇到它。