Winform从入门到精通(3)——Form(史上最全)

本文详细介绍了Winform中Form的各种属性和事件,包括AutoscaleMode、AutoSize、Location、Size、WindowState、ControlBox、IsMdiContainer、Opacity、RightToLeft等关键属性及其用法。同时讲解了ResizeBegin、Layout、FormClosing、HelpRequested等事件,帮助开发者深入理解Form在Winform程序中的应用。
摘要由CSDN通过智能技术生成

文章目录

前言

我们在开发winform程序的过程中首先要知道在哪种情况下用哪种控件,那么我们什么时候使用Form呢?Form在winform中称为窗体,当我们新建一个winform程序时,系统会自动帮我们新建一个Form,我们可以在这个Form上放置各种各种的控件,所以Form是相当于一个可以容纳很多控件的容器,除此之外,我们还可以单独的弹出一个Form窗体来展示给用户,用户也可以在窗体上进行确定、取消等交互操作,在这些场景下我们才会使用到Form。
在这里插入图片描述

一、属性

1、autoscalemode

确定当屏幕分辨率或字体更改时窗体和控件的缩放模式,这里的字体是窗体的字体,实际测试时将autoscalemode设置为dpi,当改变屏幕分辨率时,窗体和控件的变化并不明显;但是将autoscalemode设置为font时,当改变窗体的字体时,会发现窗体和控件的大小都跟着字体的大小变化而变化,该属性默认为font;

2、autoscroll

当控件的超出窗体大小,也就是控件的显示区域超出窗体的范围时,窗体是否自动显示滚动条,为true时自动显示,该属性为true时,ismdicontainer不能为true。

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用XML序列化将WinForm控件的属性保存到XML文件中。具体步骤如下: 1. 创建一个WinForm应用程序,添加需要保存属性的控件,例如TextBox、CheckBox等。 2. 在项目中创建一个保存控件属性的类。该类需要实现ISerializable接口,以便在XML文件中序列化和反序列化对象。 3. 在类中定义需要保存的控件属性,并添加属性的get和set方法。 4. 在WinForm的窗体代码中,添加保存和读取控件属性的方法。保存方法将控件属性序列化到XML文件中,读取方法从XML文件中反序列化控件属性并设置控件的值。 以下是一个简单的示例代码: ``` using System; using System.IO; using System.Windows.Forms; using System.Xml.Serialization; // 定义一个保存控件属性的类 [Serializable()] public class ControlProperties : ISerializable { public string Name { get; set; } public string Text { get; set; } public bool Checked { get; set; } // 构造函数 public ControlProperties() { } // 反序列化构造函数 public ControlProperties(SerializationInfo info, StreamingContext context) { Name = info.GetString("Name"); Text = info.GetString("Text"); Checked = info.GetBoolean("Checked"); } // 序列化方法 public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", Name); info.AddValue("Text", Text); info.AddValue("Checked", Checked); } } public partial class MainForm : Form { private string propertiesFile = "control_properties.xml"; public MainForm() { InitializeComponent(); } // 保存控件属性到XML文件 private void SaveControlProperties() { ControlProperties[] properties = new ControlProperties[2]; properties[0] = new ControlProperties { Name = textBox1.Name, Text = textBox1.Text }; properties[1] = new ControlProperties { Name = checkBox1.Name, Checked = checkBox1.Checked }; XmlSerializer serializer = new XmlSerializer(typeof(ControlProperties[])); using (TextWriter writer = new StreamWriter(propertiesFile)) { serializer.Serialize(writer, properties); } } // 从XML文件中读取控件属性并设置控件的值 private void LoadControlProperties() { if (File.Exists(propertiesFile)) { XmlSerializer serializer = new XmlSerializer(typeof(ControlProperties[])); using (TextReader reader = new StreamReader(propertiesFile)) { ControlProperties[] properties = (ControlProperties[])serializer.Deserialize(reader); foreach (ControlProperties property in properties) { Control control = Controls.Find(property.Name, true)[0]; if (control is TextBox) ((TextBox)control).Text = property.Text; else if (control is CheckBox) ((CheckBox)control).Checked = property.Checked; } } } } // 窗体加载时读取控件属性 private void MainForm_Load(object sender, EventArgs e) { LoadControlProperties(); } // 窗体关闭时保存控件属性 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { SaveControlProperties(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c#上位机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值