在关闭窗口时获得异常 [英] While closing a window getting an exception

查看:101
本文介绍了在关闭窗口时获得异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



点按一下按钮就打开了一个窗口。当我尝试关闭该窗口时(单击十字标记),我收到一个异常,说明您的应用程序中出现了未处理的异常。如果单击继续,应用程序将忽略此错误并尝试继续。如果单击退出,应用程序将立即关闭。



字符串不能为零长度。

参数名称:旧值



提前谢谢。

解决方案

这可能是因为您的代码没有检查Form f 已经关闭。



试试这个:



 Form2 f =  new  Form2(); 
f.cmbColumnCombo.DataSource = cmbList;

// 请注意此处的更改。
// 我们正在检查表单是如何关闭的。
if (f.ShowDialog()== DialogResult.OK){
for int i = 0 ; i < = dataGridView1.Rows.Count - 1 ; i ++)
{
if (dataGridView1.Rows [i] .Cells [f.cmbColumnCombo.Text] .Value.ToString()。ToLower ().Contains(f.txtfind.Text.ToLower()))
{
// dataGridView1.Rows [i] .Cells [f.cmbColumnCombo.Text] .Value = dataGridView1.Rows [i] .Cells [f.cmbColumnCombo.Text] .Value.ToString()。ToLower()。Replace(f.txtfind .Text.ToLower(),f.txtreplace.Text);
// bulidDataRow( i);
dataGridView1.Rows [i] .Cells [f.cmbColumnCombo.Text] .Value = dataGridView1.Rows [i] .Cells [f.cmbColumnCombo.Text] .Value.ToString() .Replace(f.txtfind.Text,f.txtreplace.Text);
bulidDataRow(i);
}
}
}
其他 {
/ / 表单已退出而没有任何操作
MessageBox.Show( 未进行任何更改。);
}





为确保这一点有效,请确保Form2上的确定按钮(或等效按钮)具有DialogResult属性设置为DialogResult.OK。



让我知道你怎么去!


 尝试这个。 
此代码打开窗口从客户端重定向 new url 相同的打开窗口中,如果任何异常发生然后关闭窗口 4 秒。

代码:

受保护 Sub EditSchematic_click( ByVal 发​​件人作为 对象 ByVal e As EventArgs)句柄 EditSchematic.Click
尝试

Dim 网址正如 字符串 = Google.com
ScriptManager.RegisterStartupScript( Me Me GetType myUniqueKey OpenWebWindow('&网址& ',& ConfigurationManager.AppSettings( WindowWidth)& & ConfigurationManager.AppSettings( WindowHeight)& ); True
Catch ex As 异常
ScriptManager.RegisterStartupScript( GetType myUniqueKey CloseWindow( ) True
结束 尝试

结束 Sub

aspx代码:

< asp:Button CssClass = btn runat = server ID = EditSchematic OnClientClick = openblankwindow(); />

< script >
function openblankwindow(){
width = screen.width;
height = 0 87 * screen.height;
var st = toolbar = 0,location = 0,directories = 0,status = 1,menubar = 0,scrollbars = 1,resizable = 0,left = + 0 + < span class =code-string>,top = + 0 + < span class =code-string>,width = + width + ,height = + height;
win = window.open( Processing.htm' WindowName',st);
}
function CloseWindow(){
width = screen.width;
height = 0 87 * screen.height;
var st = toolbar = 0,location = 0,directories = 0,status = 1,menubar = 0,scrollbars = 1,resizable = 0,left = + 0 + < span class =code-string>,top = + 0 + < span class =code-string>,width = + width + ,height = + height;
win = window.open( errorMsgInOpenning.htm' WindowName',st);
setTimeout( function (){
win.close()
}, 4000 );
}
function OpenWebWindow(URL,width,height){
// screen.availWidth,screen.availHeight
var left = 0;
var top = 0;
if (width< = 0 ){
width = screen.width ;
}
else {
left =(screen.width / 2 ) - (width / 2 );
}
如果(高度< = 0 ){
height = 0 87 * screen.height;
}
else {
top =(screen.height / 2 ) - (height / 2 );
}

var st = toolbar = 0,location = 0,目录= 0,状态= 1,菜单栏= 0,滚动条= 1,可调整大小= 0,左= +左+ ,top = + top + ,width = + width + ,height = + height;
window.open(URL,' WindowName',st);

}
< / script >


Hi All,

I have a window opened on a button click. When i try to close that window (On clicking the cross mark), i'm getting an exception stating "Unhandled exception has occured in your aplication. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

String cannot be of Zero length.
Parameter name: old Value

Thanks in advance.

解决方案

This is probably occurring because your code is not checking to see how Form f was closed.

Try this instead:

Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;

//Notice the change here.
//We are checking to see how the form was closed.
if (f.ShowDialog() == DialogResult.OK) {
   for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
   {
      if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
      {
         //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
         //bulidDataRow(i);
         dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
         bulidDataRow(i);
      }
   }
}
else {
   //Form was quit without any action
   MessageBox.Show("No changes were made.");
}



To ensure that this works, make sure that the "OK" button (or equivalent) on Form2 has its DialogResult property set to DialogResult.OK.

Let me know how you go!


Try this.
This code open window fromclient side and redirect by new url in same open window, if any exception occures then close window after 4 sec.

code:

Protected Sub EditSchematic_click(ByVal sender As Object, ByVal e As EventArgs) Handles EditSchematic.Click
        Try
            
            Dim URL As String = "Google.com"
            ScriptManager.RegisterStartupScript(Me, Me.GetType, "myUniqueKey", "OpenWebWindow('" & URL & "'," & ConfigurationManager.AppSettings("WindowWidth") & "," & ConfigurationManager.AppSettings("WindowHeight") & ");", True)
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Me, Me.GetType, "myUniqueKey", "CloseWindow()", True)
        End Try

    End Sub

aspx code:

 <asp:Button CssClass="btn" runat="server" ID="EditSchematic" OnClientClick="openblankwindow();" />

<script>
function openblankwindow() {
                width = screen.width;
                height = 0.87 * screen.height;
                var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + 0 + ",top=" + 0 + ",width=" + width + ",height=" + height;
                win = window.open("Processing.htm", 'WindowName', st);
            }
function CloseWindow() {
            width = screen.width;
            height = 0.87 * screen.height;
            var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + 0 + ",top=" + 0 + ",width=" + width + ",height=" + height;
            win = window.open("errorMsgInOpenning.htm", 'WindowName', st);
            setTimeout(function () {
                win.close()
            }, 4000);
        }
function OpenWebWindow(URL, width, height) {
            //        screen.availWidth, screen.availHeight
            var left = 0;
            var top = 0;
            if (width <= 0) {
                width = screen.width;
            }
            else {
                left = (screen.width / 2) - (width / 2);
            }
            if (height <= 0) {
                height = 0.87 * screen.height;
            }
            else {
                top = (screen.height / 2) - (height / 2);
            }

            var st = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;
            window.open(URL, 'WindowName', st);
           
        }
</script>


这篇关于在关闭窗口时获得异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆