How can I reverse code around an equal sign in Visual Studio?

后端 未结 4 843
一个人的身影
一个人的身影 2020-12-12 20:29

After writing code to populate textboxes from an object, such as:

txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
txtAddress.Te         


        
相关标签:
4条回答
  • 2020-12-12 20:56

    Before VS2012:

    • Copy and paste the original block of code
    • Select it again in the place you want to switch
    • Press Ctrl-H to get the "Replace" box up
    • Under "Find what" put: {[a-zA-Z\.]*} = {[a-zA-Z\.]*};
    • Under "Replace with" put: \2 = \1;
    • Look in: "Selection"
    • Use: "Regular expressions"
    • Hit Replace All

    With VS2012 (and presumably later) which uses .NET regular expressions:

    • Copy and paste the original block of code
    • Select it again in the place you want to switch
    • Press Ctrl-H to get the "Replace" box up
    • Under "Find what" put: ([a-zA-Z\.]*) = ([a-zA-Z\.]*);
    • Under "Replace with" put: ${2} = ${1};
    • Make sure that the .* (regular expressions) icon is selected (the third one along under the replacement textbox)
    • Hit Replace All
    0 讨论(0)
  • 2020-12-12 20:59

    None that I know of. Of course, if you use one of the many binding approaches available, then you won't have to - the binding will do the update in both directions (including change via notifications).

    So for winforms:

    txtFirstName.DataBindings.Add("Text", customer, "FirstName");
    

    etc

    0 讨论(0)
  • 2020-12-12 21:03

    An option to get them in there that way in the first place with Resharper would be to define a live template similar to:

    $uiElement$ = $dto$;
    $dto$ = $uiElement$;
    

    This will allow you to type them once and it will duplicate it for you and then you can cut and paste the save version to the other method.

    0 讨论(0)
  • 2020-12-12 21:06

    I had the same need but I had to accept more characters than a-zA-Z\. in the solution provided by John so I slightly modified its regular exception like this :

    Find what : {^[^\=]*} = {.*}

    Replace with : \2 = \1

    This will reverse anything arround the first equal sign found on a line

    0 讨论(0)
提交回复
热议问题