Page setup preview implementation

First look at the renderings. The controls used
write picture description here
here are label, TextBox, PictureBox, tabControl, groupBox, and Button.
The event handler used by TextBox is KeyUp. TextChanged is not used. The reason is that TextChanged needs to be updated after the cursor is removed, and KeyUp is updated in real time.
write picture description here
Xie's simple judgment is also made here, such as checking for non-numbers input, and setting a reasonable range. See the code in detail:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TextBox_Check
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private UInt16 d_width = 50;
private UInt16 d_height = 24;
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
UInt32 i = 0;
string text = ((TextBox)sender).Text;
if (-1 == Integer_Check(text, out i))
{
MessageBox.Show(“输入错误! \n请输入正确的数字!!!\n”);
((TextBox)sender).Text = “”;
return;
}
if (text == “”)
{
d_width = 1;
}
else
{
d_width = (ushort)(Convert.ToInt32(text));
}
if (d_width == 0 || d_width > 108)
{
MessageBox.Show(“输入值过大!!!\n请输入1-108之间的整数!!!\n”);
d_width = 50;
return;
}
this.pictureBox1.Size = new Size(d_width * 2, d_height * 2);
this.Refresh();
return;
}
private int Integer_Check(string text, out UInt32 Var)
{
Var = 0;
if (text.Length > 0)
{
foreach (char c in text)
{
if (c > 47 && c < 58)
{
continue;
}
else
{
return -1;
}
}
Var = (Convert.ToUInt32(text));
if (Var > 65535)
{
return -2;
}
}
else
{
Var = 0;
}
return 0;
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
UInt32 i = 0;
string text = ((TextBox)sender).Text;

        if (-1 == Integer_Check(text, out i))
        {
            MessageBox.Show("输入错误! \n请输入正确的数字!!!\n");
            ((TextBox)sender).Text = "";
            return;
        }
        if (text == "")
        {
            d_height = 1;
        }
        else
        {
            d_height = (ushort)(Convert.ToInt32(text));
        }
        if (d_height == 0 || d_width > 108)
        {
            MessageBox.Show("输入值过大!!!\n请输入1-108之间的整数!!!\n!!!\n");
            d_height = 50;
            return;
        }
        this.pictureBox1.Size = new Size(d_width * 2, d_height * 2);
        this.Refresh();
        return;
    }
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325626371&siteId=291194637