C#应用1——DataGridView控件、socket通讯、获得系统时间并计算程序运行时间、如何在texbox中输出运行日志信息、使用状态栏、让控件跟随界面变大变小

    技术2022-07-16  76

    socket通讯: https://www.jianshu.com/p/249e3f6bcd3d

    DataGridView控件使用大全: https://www.cnblogs.com/cnote/p/9062001.html

    两个窗体之间调用DataGridView控件

    Form1里声明 public static Form1 f1; 构造函数里 f1 = this; 将控件属性设置为public Form2 Form1.f1.控件名

    public partial class Form1 : Form { public static Form1 f1; public Form1() { InitializeComponent(); f1 = this; } private void Form1_Load(object sender, EventArgs e) { Form1.f1.button1.Text = "ok"; } }

    获得系统时间并实时显示到界面上

    创建一个label控件和一个timer控件。在Form1中添加代码: this.timer1.Interval=1000; this.timer1.Start(); 在timer1_Tick中添加代码: label1.Text = Datetime.Now.ToString(); Application.DoEvents();

    获得系统时间并计算程序运行时间

    在Form1函数中添加以下代码: DateTime s1; public Form1() { InitializeComponent(); s1 = DateTime.Now; this.timer1.Interval=1000; this.timer1.Start(); } 在timer1_Tick函数中添加以下代码: label1.Text = Datetime.Now.ToString(); DateTime s2 = DateTime.Now; TimeSpan ts = s2.Subtract(s1); label2.Text = ts.ToString(@"hh\:mm\:ss"); Application.DoEvents();

    如何在texbox中输出运行日志信息

    记textbox的名称为textboxlog,将其multiline属性设置为true。 将中间信息作为日志输出时,代码如下:

    DateTime d1; this.textBoxLog.AppendText(d1+"对于需求文件,忽略了已经存在于数据库中的横断面" + Environment.NewLine);

    Environment.NewLine为换行。

    使用状态栏

    https://jingyan.baidu.com/article/851fbc37e7004e3e1f15ab3d.html

    让控件跟随界面变大变小

    public Form1() { InitializeComponent(); int count = this.Controls.Count * 2+2; float[] factor = new float[count]; int i = 0; factor[i++] = Size.Width; factor[i++] = Size.Height; foreach(Control ctrl in this.Controls) { factor[i++] = ctrl.Location.X / (float)Size.Width; factor[i++] = ctrl.Location.Y / (float)Size.Height; ctrl.Tag = ctrl.Size; } Tag = factor; } private void Form1_Resize(object sender, EventArgs e) { float[] scale = (float[])Tag; int i = 2; foreach (Control ctrl in this.Controls) { ctrl.Left = (int)(Size.Width * scale[i++]); ctrl.Top = (int)(Size.Height * scale[i++]); ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width); ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height); //每次使用的都是最初始的控件大小,保证准确无误。 } }

    控件变化及布局有变化,但有局限性。

    让控件跟随界面变大变小可行的方案

    第一种

    http://www.manongjc.com/article/118757.html 在setControls中缺少一句话: if (con.Tag == null) continue;

    //设置全局变量 private float X;//当前窗体的宽度 private float Y;//当前窗体的高度 private bool IsFirst = true; private void Form_Load(object sender, EventArgs e) { X = this.Width;//获取窗体的宽度 Y = this.Height;//获取窗体的高度 setTag(this);//调用方法 } private void setTag(Control cons) {   foreach (Control con in cons.Controls)   {     con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;     if (con.Controls.Count > 0)     setTag(con);   } } private void setControls(float newx, float newy, Control cons) {   //遍历窗体中的控件,重新设置控件的值   foreach (Control con in cons.Controls)   { if (con.Tag == null) continue; //没有程序会报错     string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//获取控件的Tag属性值,并分割后存储字符串数组     float a = System.Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值,宽度     con.Width = (int)a;//宽度     a = System.Convert.ToSingle(mytag[1]) * newy;//高度     con.Height = (int)(a);     a = System.Convert.ToSingle(mytag[2]) * newx;//左边距离     con.Left = (int)(a);      a = System.Convert.ToSingle(mytag[3]) * newy;//上边缘距离     con.Top = (int)(a);     Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小     con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);     if (con.Controls.Count > 0)     {       setControls(newx, newy, con);     }   } } private void Form_Resize(object sender, EventArgs e) { //如果是第一次运行,需要把下面的if语句取消注释,否则会没反应,其以后再运行或调试的时候,就把它注释即可   //if (IsFirst) { IsFirst = false; return; }   float newx = (this.Width) / X; //窗体宽度缩放比例   float newy = (this.Height) / Y;//窗体高度缩放比例   setControls(newx, newy, this);//随窗体改变控件大小 }

    第二种

    在Resize类中添加using引用: using System.Windows.Forms; using System.Drawing;

    https://www.cnblogs.com/PER10/p/11541568.html

    Processed: 0.030, SQL: 9