C# ToolStrip System.StackOverflowException was unhandled
本文关键字:was unhandled StackOverflowException System ToolStrip | 更新日期: 2023-09-27 18:20:55
我使用toolstrip来控制显示PDF文档。但我有按键按下或任何关于键盘的东西。我的应用程序显示错误消息:
[System.StackOverflowException was unhandled]
[An handled exception of type 'System.stackOverflowException' occurred in
System.Windows.Forms.dll]
我使用pdf工具进行.net
using MigraDoc;
using PdfSharp.Pdf;
using PDFLibNet;
救命!求你了,谢谢你。
我的代码公共分部类frm_showpdf:表单{
int pageindex = 0;
int pagecount = 0;
int def_pagewidth, def_pageheight = 0;
public string filename = "";
int workingnow = 0;
public frm_showpdf()
{
InitializeComponent();
}
private void updateUI()
{
ts_pageindex.Text = Convert.ToString(pageindex);
ts_pagetotal.Text = Convert.ToString(pagecount);
}
private void show_pagepdf(int indexpage)
{
PDFLibNet.PDFWrapper pdfview = new PDFLibNet.PDFWrapper();
workingnow = 1;
pdfview.LoadPDF(filename.ToString());
pdfview.CurrentPage = indexpage;
pageindex = pdfview.CurrentPage;
pagecount = pdfview.PageCount;
pdfview.UseMuPDF = true;
pdfview.FitToWidth(pictureBox1.Handle);
pdfview.RenderPage(pictureBox1.Handle);
pdfview.CurrentX = 0;
pdfview.CurrentY = 0;
if (def_pagewidth == 0)
{
def_pagewidth = pdfview.PageWidth;
def_pageheight = pdfview.PageHeight;
}
pdfview.ClientBounds = new Rectangle(0, 0, def_pagewidth, def_pageheight);
pagecount = pdfview.PageCount;
updateUI();
Bitmap _backbuffer = new Bitmap(def_pagewidth, def_pageheight);
using (Graphics g = Graphics.FromImage(_backbuffer))
{
//pdfview.RenderHDC(g.GetHdc);
pdfview.DrawPageHDC(g.GetHdc());
g.ReleaseHdc();
}
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = _backbuffer;
workingnow = 0;
pdfview.Dispose();
}
private void showfile(object sender, EventArgs e)
{
Boolean hasfilename = false;
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
if (filename.ToString().Trim() != "") hasfilename = true;
if (hasfilename == false)
{
openFileDialog1.Filter = "PDF files|*.pdf";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
hasfilename = true;
pgpdf1.Text = openFileDialog1.FileName;
filename = pgpdf1.Text.ToString();
}
else
{
//pgpdf1.Text = "";
filename = pgpdf1.Text.ToString();
}
}
if (hasfilename == true)
{
show_pagepdf(1);
}
}
private void frm_showpdf_Load(object sender, EventArgs e)
{
toolStrip1.Cursor = System.Windows.Forms.Cursors.Hand;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.Focus();
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
panel1.Focus();
}
private void ts_first_Click(object sender, EventArgs e)
{
show_pagepdf(1);
}
private void ts_back_Click(object sender, EventArgs e)
{
if (pageindex > 1) { show_pagepdf(pageindex - 1); };
}
private void ts_next_Click(object sender, EventArgs e)
{
if (pageindex < pagecount) { show_pagepdf(pageindex + 1); };
}
private void ts_last_Click(object sender, EventArgs e)
{
show_pagepdf( pagecount) ;
}
private void ts_pageindex_TextChanged(object sender, EventArgs e)
{
}
private void frm_showpdf_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up :
ts_back.PerformClick();
e.Handled = true;
break;
case Keys.Left:
ts_back.PerformClick();
e.Handled = true;
break;
case Keys.Right:
ts_next.PerformClick();
e.Handled = true;
break;
case Keys.Down:
ts_next.PerformClick();
e.Handled = true;
break;
}
if (!e.Handled)
base.OnKeyDown(e);
}
private void frm_showpdf_Shown(object sender, EventArgs e)
{
pgpdf1.Text = filename.ToString().Trim();
pgpdf1.Text = filename.Replace(@"''", @"'");
showfile(sender,e) ;
}
private void toolStrip1_TextChanged(object sender, EventArgs e)
{
// Hello Man
}
}
我发现错误并将代码更改为:
private void frm_showpdf_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
ts_back.PerformClick();
e.Handled = true;
break;
case Keys.Left:
ts_back.PerformClick();
e.Handled = true;
break;
case Keys.Right:
ts_next.PerformClick();
e.Handled = true;
break;
case Keys.Down:
ts_next.PerformClick();
e.Handled = true;
break;
}
}
为什么C#在生产线出错:
if (!e.Handled)
base.OnKeyDown(e);
请给我建议。