简体   繁体   English

如何将 VBA 宏转换为 C# 以在 Windows 窗体应用程序中使用?

[英]How do I convert my VBA macro to C# to use in windows form application?

Good afternoon,下午好,

I am an absolute beginner in C# so please be gentle.我是 C# 的绝对初学者,所以请保持温和。 I have created a windows form in C# but I need the following VBA code converted so I can use it in my form.我在 C# 中创建了一个 windows 窗体,但我需要转换以下 VBA 代码,以便我可以在我的窗体中使用它。 Any ideas where I start?我从哪里开始的任何想法? I cant find the equivalent of Instr for C#我找不到 C# 的 Instr 等价物

Sub clipboard()

    Dim clipboard As MSForms.DataObject
    Dim strContents As String

    Set clipboard = New MSForms.DataObject
    clipboard.GetFromClipboard
    strContents = clipboard.GetText


    If InStr(1, strContents, "TFR") > 0 Then
    ErrorType = "TFR"
    var1 = WorksheetFunction.Find("ERROR", strContents)
    var2 = WorksheetFunction.Find("TFR", strContents, var1)
    var3 = WorksheetFunction.Find("'", strContents, var2)
    var4 = WorksheetFunction.Find("'", strContents, var3 + 1)
    ErrorIdent = Trim(Mid(strContents, var3 + 1, var4 - var3 - 1))

    End If
    


    Sheet1.Cells(2, 4) = ErrorType
    Sheet1.Cells(2, 6) = ErrorIdent

End Sub

All, thank you for taking the time respond to my question.所有,感谢您花时间回答我的问题。 I have figured it out using the code below, for anyone that stumbles across this type of challenge.对于任何遇到此类挑战的人,我已经使用下面的代码弄清楚了。

private void paste_error_Click(object sender, EventArgs e)
    {
        string aosinformation = Clipboard.GetText();
        string errorType = "";
        string errorIdent = "";
        string laststring = "";
        string firststring = "";
        string notamerror = "";


        if (aosinformation.IndexOf("notam") > 0)
        {
            errorType = "NOTAM";
            int var1 = aosinformation.IndexOf("ERROR");
            int var2 = aosinformation.IndexOf("notam", var1);
            int var3 = aosinformation.IndexOf("'", var2);
            int var4 = aosinformation.IndexOf("'", var3 + 1);
            errorIdent = aosinformation.Substring(var3 + 1, var4 - var3 - 
            1).Trim(' ');

For InStr(1, strContents, "TFR") > 0 , you can either do对于InStr(1, strContents, "TFR") > 0 ,您可以执行

strContents.IndexOf("TFR") >= 0 // this need to be done since VB strings start at 1

or或者

Strings.InStr(strContents, "TFR", CompareMethod.Text) > 0

or you can just use string.Contains to get a true/false value或者你可以只使用string.Contains来获取真/假值

strContents.Contains("TFR") // this is the best since you don't care where it the substring is located

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何在Windows窗体应用程序中的字符串内部进行基本数学运算 - C# How do I do some basic math inside of a string in my Windows Form Application 如何从C#Windows窗体应用程序的数据库中检索信息 - How do I retrieve information from database for my C# Windows Form Application C# 如何为 Windows 窗体应用程序创建安装? - C# How do I create an installation for a windows form application? 如何在C#WIndows表单中附加视频? - How do I attached a video in my C# WIndows Form? 如何使用.SSK文件将主题/皮肤添加到C#表单/应用程序? - How do I use a .SSK file to add a theme/skin to my C# form/application? 如何在我的 c# 应用程序中专门使用设备? - How do I use a device exclusively in my c# application? 我应该如何编写C#Windows窗体应用程序的事件处理程序? - How should I code my event handler for my C# Windows Form Application? 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 如何使用ClickOnce部署我的C#(4.0)Visual Studio 2010基于Windows窗体的应用程序? - How to use ClickOnce to deploy my C#(4.0)Visual studio 2010 windows form based Application? 如何在我的C#Windows窗体应用程序中使用sendkey.send方法? - how to use sendkey.send method in my C# windows form application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM