Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay leading zeroes in numericupdown control (C#)
    text
    copied!<p>I have a numericupdown control on a C# Windows Form, and am interested in adding a leading zero to its value if it is &lt; 10. (It is for the user to enter the minutes value of a time.)</p> <p>I am not very familiar with overrides/inheriting yet in C# but it is looking as though I may have to do that.</p> <p>It looks like <a href="http://www.eggheadcafe.com/conversation.aspx?messageid=33748685&amp;threadid=31842634" rel="nofollow noreferrer">this post</a> on EggheadCafe has the answer I need. Is it as simple as making a new class and then creating a control of that new class?</p> <pre><code>public class TestNum : NumericUpDown { protected override void ValidateEditText() { if (base.UserEdit) { base.ValidateEditText(); } } protected override void UpdateEditText() { Text = Convert.ToInt32(base.Value).ToString("00"); } } </code></pre> <p>When I try this, I am not sure how to create the new control that takes advantage of this class. I am using Visual Studio 2008. Still very new to windows forms. Thanks for any advice.</p> <p><strong>EDIT</strong></p> <p>I was able to make this work by editing the Designer-created code so that instead of the new control being of the original class, it was of the new one. So after adding the class above, I did the following (these changes are in two different locations, but I am only showing the lines that mattered):</p> <p>Changed:</p> <pre><code>this.numTest = new System.Windows.Forms.NumericUpDown(); private System.Windows.Forms.NumericUpDown numTest; </code></pre> <p>To:</p> <pre><code>this.numTest = new SampleForm.TestNum(); private TestNum numTest; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload