某些小技巧的技术分享
分享1:
需求:输入十进制数x,要求输出三十六进制数y,要求y至少是两位数,如:x=0,1…9,10,y=00,01…09,0z;
分析:
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是取整)
string temp = "";
if (vl < (36 * 36))//count = 2
temp = chars[vl / 36] + chars[vl % 36];
else if (vl < (36 * 36 * 36))//count = 3
temp = chars[vl / (36 * 36)] + chars[(vl / 36) % 36] + chars[vl % 36];
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控件,就可以看到水印提示了。