public static void ApplyPrefeence(DTE2 dte, string region, string controller, string field, string property, string preferredValue) { //string preferredValue = GetMostPreferredValue(region, controller, field, property); List <PreviousPropertyValue> listDefault = new List <PreviousPropertyValue>(); foreach (ProjectItem pi in dte.Solution.Projects.Item(1).ProjectItems) { if (pi.ProjectItems != null) { foreach (ProjectItem p in pi.ProjectItems) { if (p.Name.EndsWith(".Designer.cs")) { p.Open(EnvDTE.Constants.vsViewKindCode); p.Document.Activate(); TextSelection ts = (TextSelection)dte.ActiveDocument.Selection; TextSelection ts2 = (TextSelection)dte.ActiveDocument.Selection; string srchPattern1 = "new System.Windows.Forms.Button();"; EnvDTE.TextRanges textRanges = null; ts.StartOfDocument(false); int count = 0; string nameLine = ""; string name = ""; string[] np = new string[50]; while (ts.FindPattern(srchPattern1, 0, ref textRanges)) { ts.SelectLine(); nameLine = ts.Text; count++; string[] sp = nameLine.Split('.'); string spi = sp[1]; string[] sp2 = spi.Split('='); name = sp2[0]; np[count] = name; } int i = 1; while (ts2.FindPattern(".BackColor = System.Drawing.Color", 0, ref textRanges)) { PreviousPropertyValue def = new PreviousPropertyValue(); ts2.SelectLine(); string codeLine = ts2.Text; codeLine = codeLine.Trim(); foreach (string s in np) { string ss = s; if (ss != null) { ss = ss.Trim(); if (codeLine.Contains(ss) == true) { ts2.ReplacePattern(codeLine, "this." + s + ".BackColor = System.Drawing.Color." + preferredValue + ";", 0, ref textRanges); np = np.Where(w => w != s).ToArray(); def.FileName = p.Name; def.ControllerType = controller; def.Property = property; def.ControllerName = ss; def.DefaultValue = codeLine; listDefault.Add(def); } //else //{ // ts2.LineDown(); // ts2.NewLine(); // ts2.Insert("this." + np[i] + ".BackColor = System.Drawing.Color." + preferredValue + ";"); //} //def.FileName = p.Name; //def.ControllerType = controller; //def.Property = property; //def.ControllerName = ss; //def.DefaultValue = codeLine; //listDefault.Add(def); } } //i++; } if (np != null) { foreach (string s in np) { if (s != null) { ts2.EndOfLine(); ts2.NewLine(); ts2.Insert("this." + np[i] + ".BackColor = System.Drawing.Color." + preferredValue + ";"); np = np.Where(w => w != s).ToArray(); } } } SaveDefaultValues(listDefault); dte.ActiveDocument.Save(p.Document.FullName); dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo); } } } } }
public static bool ChangeToPreveiousValue(DTE2 dte, string controller, string property) { List <PreviousPropertyValue> listDefault = new List <PreviousPropertyValue>(); List <PreviousPropertyValue> list = LoadDefaultValues(); if (list == null) { return(false); } else { foreach (ProjectItem pi in dte.Solution.Projects.Item(1).ProjectItems) { if (pi.ProjectItems != null) { foreach (ProjectItem p in pi.ProjectItems) { if (p.Name.EndsWith(".Designer.cs")) { p.Open(EnvDTE.Constants.vsViewKindCode); p.Document.Activate(); TextSelection ts2 = (TextSelection)dte.ActiveDocument.Selection; EnvDTE.TextRanges textRanges = null; ts2.StartOfDocument(false); //Find2 findWin = (Find2)dte.Find; int count = 0; //c = findWin.FindReplace(vsFindAction.vsFindActionFindAll, "button1", 0); string s = ""; string name = ""; string[] np = new string[50]; // Advance to the next Visual Basic function beginning or end by // searching for "Sub" with white space before and after it. //while //while (ts.FindPattern(srchPattern1, 0, ref textRanges)) //{ // // // Select the entire line. // count++; // ts.SelectLine(); // s = ts.Text; // string[] sp = s.Split('.'); // string spi = sp[1]; // string[] sp2 = spi.Split('='); // name = sp2[0]; // np[count] = name; // //ts.FindPattern("this." + name + ".BackColor = System.Drawing.Color", 0, ref textRanges); // //ts.SelectLine(); // //s = ts.Text; // //ts2.StartOfDocument(false); // //while (ts.FindText("this." + name + ".BackColor = System.Drawing.Color", 0)) // //{ // // ts.SelectLine(); // // string sd = ts.Text; // // bool t = ts.ReplacePattern(sd, "this.button1.BackColor = System.Drawing.Color.Red;", 0, ref textRanges); // //} //} int i = 1; //for(int i=1; i<=5;i++) //{ //ts2 = null; while (ts2.FindPattern(".BackColor = System.Drawing.Color", 0, ref textRanges)) { ts2.SelectLine(); string sd = ts2.Text; sd = sd.Trim(); foreach (PreviousPropertyValue dfcon in list) { if (dfcon.FileName == p.Name && sd.Contains(dfcon.ControllerName) && dfcon.Property == property && dfcon.ControllerType == controller) { ts2.ReplacePattern(sd, dfcon.DefaultValue, 0, ref textRanges); } i++; //} } } //ts.NewLine(1); dte.ActiveDocument.Save(p.Document.FullName); dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesNo); } } } } return(true); } }