0

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
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • Does this help https://stackoverflow.com/questions/31920525/vb-instr-equivalent-in-c-sharp – eglease Oct 26 '21 at 14:53
  • I do not think much has changed since '08? see: [Is it possible to convert VBA to C#?](https://stackoverflow.com/questions/388819/is-it-possible-to-convert-vba-to-c) – Luuk Oct 26 '21 at 15:01
  • I’m voting to close this question because converting code from one language to another is considered out of scope for Stack Overflow. – braX Oct 26 '21 at 16:58
  • @braX I was simply asking what the equivalent of the VBA code is in C#., which has been partially answered. I have included my code in this question for any users faced with this challenge in the future.. – Shane Nicholson Oct 28 '21 at 09:32

2 Answers2

1

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

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
eglease
  • 2,445
  • 11
  • 18
  • 28