什么是这个函数中的问题C#

时间:2009-09-15 16:42:00

标签: c# reference webbrowser-control

我编写了以下函数来选择数字字符串,例如1,23,000.00 在WebBrowser中,我捕获Double_Click事件,然后将所选范围传递给下面的函数。

假设初始选择是000,我的目标是选择上面提到的整个字符串。

myRange=doc.selection.createRange()
myRange=SelectCSNumbers(myRange)

我从下面的函数返回一个Range对象。这里的问题是

return tmpRange.duplicate();//here it should terminate
count++;

当我返回最终范围时,此方法再次被调用 我怎么也不知道,任何人都可以指出我的错误。

private mshtml.IHTMLTxtRange SelectCSNumbers(mshtml.IHTMLTxtRange myRange)
        {
            mshtml.IHTMLTxtRange tmpRange = myRange.duplicate();
            string[] strInt = tmpRange.text.Split(',');
            bool result = false;
            result = CheckText(tmpRange, strInt, result);

            if (result && count==0)//
            {

                //Expand the Range with a single Character
                tmpRange.expand("character");

                if (tmpRange.text.Length > myRange.text.Length)
                {
                    if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                    {
                        //Check for ,/.
                        if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
                        {
                            if (tmpRange.text.IndexOf('.') == -1)
                            {
                                //EOS
                            }
                            else
                            {
                                //. is found

                                SelectCSNumbers(tmpRange.duplicate());
                            }
                        }
                        else
                        {

                            SelectCSNumbers(tmpRange.duplicate());
                        }

                    }
                    else if (tmpRange.text.IndexOf(' ') != -1)
                    {
                        tmpRange = myRange.duplicate();
                        tmpRange.moveStart("character", -1);
                        if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                        {
                            //Check for ,/.
                            if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
                            {
                                if (tmpRange.text.IndexOf('.') == -1)
                                {
                                    //EOS
                                }
                                else
                                {
                                    //. is found

                                    SelectCSNumbers(tmpRange.duplicate());
                                }
                            }
                            else
                            {

                                SelectCSNumbers(tmpRange.duplicate());
                            }
                        }

                    }

                }
                else if (tmpRange.text.Length == myRange.text.Length)
                {
                    tmpRange = myRange.duplicate();
                    tmpRange.moveStart("character", -1);
                    if (tmpRange.text.Length == myRange.text.Length)
                    {
                        //tmpRange = null;
                        return tmpRange.duplicate();//here it should terminate
                        count++;
                    }
                    else if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                    {
                        if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
                        {
                            if (tmpRange.text.IndexOf('.') == -1)
                            {
                                //EOS
                            }
                            else
                            {
                                //. is found

                                SelectCSNumbers(tmpRange.duplicate());
                            }
                        }
                        else
                        {

                            SelectCSNumbers(tmpRange.duplicate());
                        }
                    }

                }
            }
            return tmpRange.duplicate();
        }

2 个答案:

答案 0 :(得分:2)

这没有立即帮助,但解决了更大的问题

此代码需要重构。它会给你带来问题。你有复制粘贴的代码,这将是一个很难处理。而且,这使得其他人更难以帮助。

以下是重构(未经测试)

的建议
    private mshtml.IHTMLTxtRange SelectCSNumbers(mshtml.IHTMLTxtRange myRange)
    {
        mshtml.IHTMLTxtRange tmpRange = myRange.duplicate();
        string[] strInt = tmpRange.text.Split(',');
        bool result = false;
        result = CheckText(tmpRange, strInt, result);

        if (result && count==0)//
        {

            //Expand the Range with a single Character
            tmpRange.expand("character");

            if (tmpRange.text.Length > myRange.text.Length)
            {
                if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                {

                    SomeOtherFunction(tmpRange);
                }
                else if (tmpRange.text.IndexOf(' ') != -1)
                {
                    tmpRange = myRange.duplicate();
                    tmpRange.moveStart("character", -1);

                    SomeOtherFunction(tmpRange);

                }

            }
            else if (tmpRange.text.Length == myRange.text.Length)
            {
                tmpRange = myRange.duplicate();
                tmpRange.moveStart("character", -1);
                if (tmpRange.text.Length == myRange.text.Length)
                {
                    //tmpRange = null;
                    return tmpRange.duplicate();//here it should terminate
                    count++;
                }
                else if (tmpRange.text.IndexOf(' ') == -1)  //if no space is found that means the selection is not proper 
                {
                    SomeOtherFunction(tmpRange);
                }

            }
        }
        return tmpRange.duplicate();
    }


    private void SomeOtherFunction(mshtml.IHTMLTxtRange tmpRange)
    {
        if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
        {
            if (tmpRange.text.IndexOf('.') == -1)
            {
                //EOS
            }
            else
            {
                //. is found

                SelectCSNumbers(tmpRange.duplicate());
            }
        }
        else
        {

            SelectCSNumbers(tmpRange.duplicate());
        }
    }

答案 1 :(得分:1)

随机猜测:

if (tmpRange.text.Length == myRange.text.Length)
{
    count++;
    return tmpRange.duplicate();
}

如果您在count++语句之后放置return,它将永远不会被执行。