Problem with compare two controls by properties | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem with compare two controls by properties

Hi:) I have two buttons, first is on Controlcollection and his Text is changed, second is... Default... Only new Button();. Ok... I want to get property which is changed, because in future i will not know which will be changed, i compare one by one in loop properties... Everything is ok but... Why i get much more than only Text? I get including Padding or Margin?? Do you need code to check where is a problem?

28th Dec 2017, 8:26 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
7 Answers
+ 1
@Kacper... Can you post a snippet of the code so I can see how you are doing the comparisons?
28th Dec 2017, 8:36 PM
David Carroll
David Carroll - avatar
+ 1
CODE using System; using System.Windows.Forms; using static System.Windows.Forms.Control; static class Program { static void Main(string[] args) { var cc = new ControlCollection(new Control()) { new Button { Text = "change" }, }; var btn = new Button(); var propBtn = btn.GetType().GetProperties(); foreach (Control item in cc) { var propItem = item.GetType().GetProperties(); for (int i = 0; i < propItem.Length; i++) { if (propItem[i].GetValue(item) != propBtn[i].GetValue(btn)) { Console.WriteLine(propItem[i].Name + " : " + propItem[i].GetValue(item)); Console.WriteLine(propBtn[i].Name + " : " + propBtn[i].GetValue(btn)); Console.WriteLine(); } } } } } OUTPUT AutoSizeMode : GrowOnly AutoSizeMode : GrowOnly DialogResult : None DialogResult : None AutoEllipsis : False AutoEllipsis : False AutoSize : False AutoSize : False BackColor : Color [Control] BackColor : Color [Control] FlatStyle : Standard FlatStyle : Standard FlatAppearance : System.Windows.Forms.FlatButtonAppearance FlatAppearance : System.Windows.Forms.FlatButtonAppearance ImageAlign : MiddleCenter ImageAlign : MiddleCenter ImageIndex : -1 ImageIndex : -1 ImeMode : Disable ImeMode : Disable Text : change Text : TextAlign : MiddleCenter TextAlign : MiddleCenter TextImageRelation : Overlay TextImageRelation : Overlay UseMnemonic : True UseMnemonic : True UseCompatibleTextRendering : True UseCompatibleTextRendering : True UseVisualStyleBackColor : True UseVisualStyleBackColor : True AccessibilityObject : ControlAccessibleObject: Owner = System.Windows.Forms.Button, Text: change AccessibilityObject : ControlAccessibleObject: Owner = System.Windows.Forms.Button, Text:
28th Dec 2017, 9:20 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
+ 1
Yes... But i changed only Text so... Why i got more than only Text?
29th Dec 2017, 8:09 AM
Kacper Piotrowski
Kacper Piotrowski - avatar
+ 1
so?
30th Dec 2017, 7:33 AM
Kacper Piotrowski
Kacper Piotrowski - avatar
+ 1
My guess is these are default properties that initial with all buttons created in your project.
30th Dec 2017, 8:39 AM
David Carroll
David Carroll - avatar
+ 1
but i compare all properties and I get this but i want to only Text because I change only this
30th Dec 2017, 8:41 AM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
Nice! Thanks for including the output. Where is the extra info like padding / margin? I see where Text is blank for one and "change" for the other.
29th Dec 2017, 1:46 AM
David Carroll
David Carroll - avatar