Home > Back-end >  How do I convert my VBA macro to C# to use in windows form application?
How do I convert my VBA macro to C# to use in windows form application?

Time:10-28

Good afternoon,

I am an absolute beginner in C# so please be gentle. I have created a windows form in C# but I need the following VBA code converted so I can use it in my form. Any ideas where I start? I cant find the equivalent of Instr for C#

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

CodePudding user response:

For InStr(1, strContents, "TFR") > 0, you can either do

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

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

CodePudding user response:

all thank you for taking the time respond to my question. I have figured it out using the code below for anyone the stumbles across this type of work.

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(' ');
  • Related