When debugging I feel a need to popup values from variables.
I know that Visual Studio is cool but I feel the need to use it just typing.
So I design an extension method to make it easy.
You can add your custom objects, or Windows objects, and make a clause to show it or not.
The message is shown only in DEBUG time.
See this sample,
  1. using System;
  2. using System.Net.Mail;
  3. using System.Windows.Forms;
  4. namespace DebugPopup
  5. {
  6. public partial class FrmSample : Form
  7. {
  8. public FrmSample() => InitializeComponent();
  9. private void Form1_Load(object sender, EventArgs e)
  10. {
  11. var eml = new MailAddress("[email protected]", "test");
  12. eml.PopupBox();
  13. var n = 0;
  14. n.PopupBox();
  15. Handle.PopupBox();
  16. decimal.Zero.PopupBox(n==0, "n is equals zero!");
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. ((Button)sender).PopupBox();
  21. }
  22. }
  23. }
It 's an extension method that you only need to add.PopupBox() from the Visual Studio property menu.
You can add custom types and the message will be shown only in DEBUG mode, I mean that in production there will be no message.
This is the main code,
  1. using System.Windows.Forms;
  2. using System.Net.Mail;
  3. using System.Diagnostics;
  4. /// <summary>
  5. /// Author: Jefferson Saul G. Motta
  6. /// 10-24-2019 20:50
  7. /// Uses the System
  8. /// </summary>
  9. namespace System
  10. {
  11. /// <summary>
  12. /// This is my real code that I make to debug easly
  13. /// You can copy to .NET Core as Well
  14. /// If you are debugging in a local IIS (ASP.NET WebForms)
  15. /// The popup will raises too
  16. /// C# 7.3 and C# 8.0
  17. /// About extensions: http://www.c-sharpcorner.com/blogs/extension-method-in-c-sharp3
  18. /// </summary>
  19. public static class ExtensionMethodStrings
  20. {
  21. /// <summary>
  22. /// Popup for int value
  23. /// </summary>
  24. /// <param name="value"></param>
  25. /// <param name="showIf"></param>
  26. /// <param name="message"></param>
  27. /// <returns></returns>
  28. public static bool PopupBox(this int value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  29. /// <summary>
  30. /// Popup for decimal value
  31. /// </summary>
  32. /// <param name="value"></param>
  33. /// <param name="showIf"></param>
  34. /// <param name="message"></param>
  35. /// <returns></returns>
  36. public static bool PopupBox(this decimal value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  37. /// <summary>
  38. /// Popup for IntPtr value
  39. /// </summary>
  40. /// <param name="value"></param>
  41. /// <param name="showIf"></param>
  42. /// <param name="message"></param>
  43. /// <returns></returns>
  44. public static bool PopupBox(this IntPtr value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  45. /// <summary>
  46. /// Popup for double value
  47. /// </summary>
  48. /// <param name="value"></param>
  49. /// <param name="showIf"></param>
  50. /// <param name="message"></param>
  51. /// <returns></returns>
  52. public static bool PopupBox(this double value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  53. /// <summary>
  54. /// Popup for long value
  55. /// </summary>
  56. /// <param name="value"></param>
  57. /// <param name="showIf"></param>
  58. /// <param name="message"></param>
  59. /// <returns></returns>
  60. public static bool PopupBox(this long value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  61. /// <summary>
  62. /// Popup for string value
  63. /// </summary>
  64. /// <param name="value"></param>
  65. /// <param name="showIf"></param>
  66. /// <param name="message"></param>
  67. /// <returns></returns>
  68. public static bool PopupBox(this string value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  69. /// <summary>
  70. /// Popup for string value if contem a string
  71. /// </summary>
  72. /// <param name="value"></param>
  73. /// <param name="contem"></param>
  74. /// <param name="message"></param>
  75. /// <returns></returns>
  76. public static bool PopupBox(this string value, in string contem, in string message = "") => PopupIt($"Value: {value}", value.ContemUpper(contem), message);
  77. /// <summary>
  78. /// Popup for bool value
  79. /// </summary>
  80. /// <param name="value"></param>
  81. /// <param name="showIf"></param>
  82. /// <param name="message"></param>
  83. /// <returns></returns>
  84. public static bool PopupBox(this bool value, in bool showIf = true, in string message = "") => PopupIt($"Value: {value}", showIf, message);
  85. /// <summary>
  86. /// Check if exist comparing uppper text
  87. /// </summary>
  88. /// <param name="value"></param>
  89. /// <param name="text"></param>
  90. /// <returns></returns>
  91. private static bool ContemUpper(this string value, string text) => string.IsNullOrEmpty(value) || string.IsNullOrEmpty(text) ? false : value.ToUpper().IndexOf(text.ToUpper()) != -1;
  92. /// <summary>
  93. /// Sample
  94. /// You can add another controls or objects
  95. /// </summary>
  96. /// <param name="button"></param>
  97. /// <param name="showIf"></param>
  98. /// <returns></returns>
  99. public static bool PopupBox(this Button button, in bool showIf = true) => PopupIt(button.Text, showIf);
  100. /// <summary>
  101. /// Sample
  102. /// You can add another controls or objects
  103. /// Test with MailAddress
  104. /// </summary>
  105. /// <param name="eml"></param>
  106. /// <param name="showIf"></param>
  107. /// <returns></returns>
  108. public static bool PopupBox(this MailAddress eml, in bool showIf = true) => PopupIt(eml.Address, showIf);
  109. /// <summary>
  110. /// Add the label if value not is empty
  111. /// </summary>
  112. /// <param name="value"></param>
  113. /// <param name="label"></param>
  114. /// <returns></returns>
  115. private static string LabelIfNotEmpty(this string value, string label) => string.IsNullOrEmpty(value) ? "" : $"{label}:{value}";
  116. /// <summary>
  117. /// Show popup only for DEBUG
  118. /// </summary>
  119. /// <param name="message"></param>
  120. /// <param name="showIf"></param>
  121. /// <param name="messageExtra"></param>
  122. /// <returns></returns>
  123. private static bool PopupIt(string message, in bool showIf = true, in string messageExtra = "")
  124. #if (DEBUG)
  125. {
  126. // Show popup if true
  127. if (showIf)
  128. {
  129. Debug.WriteLine($"{messageExtra.LabelIfNotEmpty("Extra message:")}{message}");
  130. // Optional:
  131. MessageBox.Show($"{messageExtra.LabelIfNotEmpty("Extra message:")}{message}");
  132. }
  133. // showIf
  134. return showIf;
  135. }
  136. #else
  137. // on Releases returns false
  138. => false;
  139. #endif
  140. }
  141. }
You can contribute with the community on the GitHub
This is my first GitHub free project, I hope you enjoy it.
Happy coding.