用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

VB 代码格式化工具

2016-01-05 作者: 小章举报

[c#]代码库

using System;
using System.Text;
using System.Windows.Forms;

namespace VBcodeFormating
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        public string IndentVBCode(string source)
        {
            string[] lines = source.Split('\n');
            StringBuilder sb = new StringBuilder();
            string sTabs = "";
            int count = 0;
            bool isCaseBlock = false;
            foreach (string line in lines)
            {
                string tmpLine = line.Trim();
                switch (tmpLine)
                {
                    case "End Sub":
                    case "End Function":
                    case "End Type":
                    case "End Enum":
                    case "End Property":
                        sTabs = "";
                        break;
                    case "End If":
                    case "#End If":
                    case "End Select":
                    case "Else":
                    case "#Else":
                    case "End With":
                    case "Loop":
                        if (sTabs.StartsWith("\t"))
                            sTabs = sTabs.Substring(1);
                        break;
                }
                if (sTabs.StartsWith("\t") && ((tmpLine.StartsWith("Next") || tmpLine.StartsWith("Loop Until") || tmpLine.StartsWith("Loop While") || tmpLine.StartsWith("Wend") || tmpLine.StartsWith("ElseIf"))))
                    sTabs = sTabs.Substring(1);
                if (tmpLine.StartsWith("End Select"))
                    isCaseBlock = false;
                if (isCaseBlock && !tmpLine.StartsWith("Case "))
                    sb.Append("\t");
                sb.Append(sTabs);
                sb.AppendLine(tmpLine);
                if ((tmpLine.StartsWith("Sub ") || tmpLine.StartsWith("Private Sub ") || tmpLine.StartsWith("Public Sub ") || tmpLine.StartsWith("Function ") || tmpLine.StartsWith("Private Function ") || tmpLine.StartsWith("Public Function ") || tmpLine.StartsWith("Type ") || tmpLine.StartsWith("Private Type ") || tmpLine.StartsWith("Public Type ") || tmpLine.StartsWith("Enum ") || tmpLine.StartsWith("Private Enum ") || tmpLine.StartsWith("Public Enum ") || tmpLine.StartsWith("Property ") || tmpLine.StartsWith("Private Property ") || tmpLine.StartsWith("Public Property ")))
                    sTabs += "\t";
                if (tmpLine.Contains(" "))
                {
                    string sCodeString = tmpLine.Split(' ')[0];
                    switch (sCodeString)
                    {
                        case "For":
                        case "With":
                        case "Select":
                        case "Do":
                            sTabs += "\t";
                            break;
                        case "ElseIf":
                        case "If":
                        case "#If":
                            if (tmpLine.EndsWith("Then") || (tmpLine.EndsWith("_") && lines[++count].Trim().EndsWith("Then")))
                                sTabs += "\t";
                            break;
                        case "Case":
                            isCaseBlock = true;
                            break;
                    }
                }
                else if (tmpLine == "Else" || tmpLine == "#Else")
                    sTabs += "\t";
            }
            return sb.ToString();
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            syntaxEditor1.Text = IndentVBCode(Clipboard.GetText());
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(syntaxEditor1.Text);
        }

        private void syntaxEditor1_PasteDragDrop(object sender, ActiproSoftware.SyntaxEditor.PasteDragDropEventArgs e)
        {
            syntaxEditor1.Text = IndentVBCode(e.Text);
        }

        private void btnCodeFormat_Click(object sender, EventArgs e)
        {
            syntaxEditor1.Text = IndentVBCode(syntaxEditor1.Text);
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...