某些小技巧的技術分享

分享1:ui

需求:輸入十進制數x,要求輸出三十六進制數y,要求y至少是兩位數,如:x=0,1…9,10,y=00,01…09,0z;this

         分析:spa

                 string[] chars = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };設計

         int vl = (int)value;(注:vl爲整數,vl/36是取整)orm

         string temp = "";ip

         if (vl < (36 * 36))//count = 2get

             temp = chars[vl / 36] + chars[vl % 36];string

         else if (vl < (36 * 36 * 36))//count = 3it

             temp = chars[vl / (36 * 36)] + chars[(vl / 36) % 36] + chars[vl % 36];io

         else if (vl < (36 * 36 * 36 * 36))//count = 4

             temp = chars[vl / (36 * 36 * 36)] + chars[(vl / (36 * 36)) % 36] + chars[(vl / 36) % 36] + chars[vl % 36];

         else if (vl < (36 * 36 * 36 * 36 * 36))//count = 5

             temp = chars[vl / (36 * 36 * 36 * 36)] + chars[(vl / (36 * 36 * 36)) % 36]+ chars[(vl / (36 * 36)) % 36] + chars[(vl / 36) % 36] + chars[vl % 36];

         .......依次類推

根據上面的規律,咱們能夠使用一個while循環進行實現:

int n=vl;

         int count =1;//記錄位數+1

         while (true)

         {

                   if ((n = n / 36) < 36)//計算週期內的位數,直到n的值在0~36內(不包含36)

                   {

                            temp = chars[vl / (int)(Math.Pow(36, count))];//最左邊位數值

                            for (int i = count - 1; i > -1; i--)

                                     temp += chars[(vl / (int)(Math.Pow(36, i))) % 36];//從左向右追加位數值

                            break;

                   }

                   count++;

         }

最後,temp就是所需的結果。(語言:C#)

 

分享2:

         需求:在winForm中,須要對TextBox輸入框進行水印提示;

         分析:對TextBox進行重寫操做:

         public partial class WatermarkTextBox : TextBox

    {

        private const uint ECM_FIRST = 0x1500;

        private const uint EM_SETCUEBANNER = ECM_FIRST + 1;

 

        public WatermarkTextBox()

        {

            InitializeComponent();

        }

        [DllImport("user32.dll",CharSet = CharSet.Auto,SetLastError = false)]

        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg ,uint wParam,string lParam);

        private string watermarkText;

        [Description("文本提示")]

        public string WatermarkText

        {

            get { return watermarkText; }

            set

            {

                watermarkText = value;

                SetWatermark(watermarkText);

            }

        }

        private void SetWatermark(string watermarkText)

        {

            SendMessage(this.Handle, EM_SETCUEBANNER, 0, watermarkText);

        }

}

對此重寫方法編譯後,咱們在設計界面的屬性框中就能夠找到WatermarkText,直接填寫值,使用的WatermarkTextBox控件,就能夠看到水印提示了。

相關文章
相關標籤/搜索