C#里使用PerformLayout,强制控件将布局逻辑应用于其所有子控件。

前几天,使用DataGridView来进行动态数据显示,但是发现左边的滚动条会显示不正确。
比如设置显示第100行了,但是滚动条的位置还是在最顶端,
如果你去点击一下滚动条,它又立即更新,并且跳到正确的位置显示。

一开始认为界面没有更新,找了Refresh()函数,又调用窗口更新函数,
最后还使用下面代码反向映射获取滚动条:
ScrollBar vsbar = (ScrollBar)typeof(DataGridView).GetProperty("VerticalScrollBar", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(datagridview1, null);
并设置对应的位置,但是还是没有正确地显示。

到这个时候,已经找了一天半时间了,又去翻MSDN的文档,又去百度,还去bing。
但是还没有解决。

最后发现了一个函数PerformLayout(),它是立即强制界面刷新,并且进行重新设置。
后来通过调用这个函数,就可以解决滚动更新位置不正确的问题了。

例子如下:

using System.Windows.Forms;
using System.Drawing;public class LayoutForm :System.Windows.Forms.Form{public LayoutForm() : base(){InitializeComponent();}internal System.Windows.Forms.Button Button1;internal System.Windows.Forms.Button Button2;internal LayoutControl LayoutControl1;internal System.Windows.Forms.Button Button3;private void InitializeComponent(){this.Button1 = new System.Windows.Forms.Button();this.Button2 = new System.Windows.Forms.Button();this.Button3 = new System.Windows.Forms.Button();this.LayoutControl1 = new LayoutControl();this.SuspendLayout();this.Button1.Location = new System.Drawing.Point(16, 16);this.Button1.Name = "Button1";this.Button1.Size = new System.Drawing.Size(120, 32);this.Button1.TabIndex = 0;this.Button1.Text = "Center textbox on control";this.Button2.Location = new System.Drawing.Point(152, 16);this.Button2.Name = "Button2";this.Button2.Size = new System.Drawing.Size(104, 32);this.Button2.TabIndex = 3;this.Button2.Text = "Shrink user control";this.Button3.Location = new System.Drawing.Point(96, 232);this.Button3.Name = "Button3";this.Button3.TabIndex = 5;this.Button3.Text = "Reset";this.LayoutControl1.BackColor = System.Drawing.SystemColors.ControlDark;this.LayoutControl1.Location = new System.Drawing.Point(72, 64);this.LayoutControl1.Name = "LayoutControl1";this.LayoutControl1.Size = new System.Drawing.Size(150, 160);this.LayoutControl1.TabIndex = 6;this.ClientSize = new System.Drawing.Size(292, 266);this.Controls.Add(this.Button3);this.Controls.Add(this.Button2);this.Controls.Add(this.Button1);this.Controls.Add(this.LayoutControl1);this.Name = "Form1";this.Text = "Form1";this.ResumeLayout(false);this.Button1.Click += new System.EventHandler(Button1_Click);this.Button2.Click += new System.EventHandler(Button2_Click);this.Button3.Click += new System.EventHandler(Button3_Click);}[System.STAThread]public static void Main(){Application.Run(new LayoutForm());}// This method explicitly calls raises the layout event on // LayoutControl1, changing the Bounds property.private void Button1_Click(System.Object sender, System.EventArgs e){LayoutControl1.PerformLayout(LayoutControl1, "Bounds");}// This resize of LayoutControl1 implicitly triggers the layout event. //  Changing the size of the control affects its Bounds property.private void Button2_Click(System.Object sender, System.EventArgs e){LayoutControl1.Size = new System.Drawing.Size(100, 100);}// This method explicitly calls PerformLayout with no parameters, // which raises the Layout event with the LayoutEventArgs properties// equal to Nothing.private void Button3_Click(System.Object sender, System.EventArgs e){LayoutControl1.PerformLayout();}
}// This custom control has the Layout event implented so that when 
// PerformLayout(AffectedControl, AffectedProperty) is called 
// on the control, where AffectedProperty equals "Bounds" the 
// textbox is centered on the control.
public class LayoutControl :System.Windows.Forms.UserControl
{internal System.Windows.Forms.TextBox TextBox1;public LayoutControl() : base(){InitializeComponent();}private void InitializeComponent(){this.TextBox1 = new System.Windows.Forms.TextBox();this.SuspendLayout();this.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.TextBox1.Name = "TextBox1";this.TextBox1.TabIndex = 0;this.BackColor = System.Drawing.SystemColors.ControlDark;this.Controls.Add(this.TextBox1);this.Name = "LayoutControl";this.ResumeLayout(false);this.Layout += new LayoutEventHandler(LayoutControl_Layout);}// This method is called when the Layout event is fired. // This happens by during the initial load, by calling PerformLayout// or by resizing, adding or removing controls or other actions that // affect how the control is laid out. This method checks the // value of e.AffectedProperty and changes the look of the // control accordingly. private void LayoutControl_Layout(object sender,System.Windows.Forms.LayoutEventArgs e){if (e.AffectedProperty != null){if (e.AffectedProperty.Equals("Bounds")){TextBox1.Left = (this.Width - TextBox1.Width) / 2;TextBox1.Top = (this.Height - TextBox1.Height) / 2;}}else{this.Size = new System.Drawing.Size(150, 160);TextBox1.Location = new System.Drawing.Point(16, 24);}TextBox1.Text = "Left = " + TextBox1.Left + " Top = " + TextBox1.Top;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/55229.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

申请商家转账到零钱功能所需材料及必过攻略

商家转账到零钱功能的快速开通方法,可以归纳为以下几个步骤: 一、确认商户资格与账号状态 1. 商户类型:该功能通常只对公司性质的商户开放,个体工商户及小微商户暂时无法申请。商家需为微信支付认证用户。 2. 账号状态&#xff…

网络层及ip报头

★★★★★默写: A类:0~127 B类:128~191 C类:192~223 A类私网:10.0.0 - 10.255.255.255 B类私网:172.16.0.0 - 172.31.255.255 C类私网:19.168.0.0 - 192.168.255.255 特殊: 0.0.0…

Windows远程桌面到Ubuntu

在Ubuntu系统中,默认情况下root账户是被禁用的,为了安全起见,建议不要直接使用root账户登录图形界面。但是,如果出于特定的管理或维护需求,您可以按照以下步骤启用和使用root账户登录图形界面: 启用root账户…

新手爬虫DAY1

这个错误信息表明在你的Python程序中,re.search() 函数没有找到预期的匹配项,因此返回了 None。当你尝试在 None 对象上调用 group(1) 方法时,Python 抛出了一个 AttributeError。 具体来说,错误发生在 pc.py 文件的第6行&#x…

AI大模型与相对论的结合点的思考、应用及相对论原理与公式表达

大家好,我是微学AI,今天给大家介绍一下AI大模型与相对论的结合点的思考、应用及相对论原理与公式表达。在阐述相对论原理的基础上,通过数学复杂公式,分析了人工智能大模型在相对论领域的应用前景。文章深入挖掘了两大领域之间的联…

浏览器哪个好,占用cpu低。。电脑卡。流氓软件。。。火狐浏览器的使用。

用360安全浏览器打开b站,有时候占用CPU会升高,高达90%以上。一关闭b站就回落。 用谷歌浏览器打开b站,有时候占用CPU会升高,高达60%、70%,比360安全浏览器低一些。。一关闭b站就回落。 360安全浏览器、QQ浏览器&#xf…

厨房老鼠数据集:掀起餐饮卫生监测的科技浪潮

厨房老鼠数据集:掀起餐饮卫生监测的科技浪潮 摘要:本文深入探讨了厨房老鼠数据集在餐饮行业卫生管理中的重要性及其相关技术应用。厨房老鼠数据集通过收集夜间厨房图像、老鼠标注信息以及环境数据,为深度学习模型提供了丰富的训练样本。基于…

MongoDB 安装配置及配置和启动服务

MongoDB 安装配置 附:MongoDB官网下载地址: https://www.mongodb.com/download-center/community 注: 官网可以下载最新版的MongoDB安装包,有MSI安装版和ZIP安装版。我们课堂上使用4.4.4的ZIP安装版。安装版参考博客&#xff1…

Spark第一天

MapReduce过程复习 Spark由五部分组成 RDD五大特征 1、 Spark -- 代替MapReduce <<<<< scala是单机的&#xff0c;spark是分布式的。>>>>> 开源的分布式计算引擎 可以快速做计算 -- 因为可以利用内存来做一些计算 (1) 分为5个库(模块) : 1、…

安装指定node.js 版本 精简版流程

首先 我们本机上是否安装有node 如果有 需要先卸载 卸载完成后 使用命令查看是否卸载干净 打开WinR 输入cmd 然后输入如下名: where node 如果没有目录显示 说明node 很干净 本机没有相关安装 在输入命令: where npm 如果有相关目录 需要删除掉 要不然 后续安装的…

报错 - LangChain bind_tools NotImplementedError

使用 LangChain 的 bind_tools 方法一直报错&#xff0c;即使使用 ChatOpenAI 作为 llm 接口 根据这个issue 下的回答&#xff0c;修改了 ChatOpenAI 的 import 出处&#xff0c;解决了问题 https://github.com/langchain-ai/langchain/issues/21479#issuecomment-2105618237 …

基于华为昇腾910B,实战 InternLM2.5-7B-Chat 模型推理

本文将带领大家基于启智平台&#xff0c;使用 LMDeploy 推理框架在华为昇腾 910B 上实现 internlm2_5-7b-chat 模型的推理。 GitHub - InternLM/lmdeploy: LMDeploy is a toolkit for compressing, deploying, and serving LLMs.&#xff08;欢迎star&#xff09; GitHub - I…

Opencv库的安装与vs项目配置

目录 一、下载安装opencv 1、下载 2、减压安装 3、环境变量配置&#xff08;vs项目不是必须的&#xff0c;看后面&#xff09; 二、vs项目配置opencv 1、创建vs项目 2、包含opencv头文件 一、下载安装opencv 1、下载 OpenCV - Open Computer Vision Library 2、减压安…

k8s杂记

在node节点内部使用kubectl&#xff1a; rootmultinode-demo-m02:/# ps aux | grep kubelet root 218 3.1 1.6 2066316 62516 ? Ssl 07:35 0:29 /var/lib/minikube/binaries/v1.30.0/kubelet --bootstrap-kubeconfig/etc/kubernetes/bootstrap-kubelet.con…

phpstorm+phpstudy 配置xdebug(无需开启浏览器扩展)

今天又被xdebug折磨了&#xff0c;忘记了以前咋配置了现在百度发现好多都是各种浏览器扩展而且也没有真正的用到项目上的都是测试的地址怎么样的 我就简单写一下自己实战吧 不支持workerman swoole hyperf等这种服务框架 如果你会请教教我 工具版本phpstudy8.1.xphpstorm2021.x…

docker查看,删除,停止,暂停,恢复容器详解

一&#xff0c;查看容器&#xff08;假设你有一个名为 my_app_container 的容器&#xff09;。 1. 查看所有容器&#xff08;包括已停止的&#xff09; docker ps -a 输出示例&#xff1a; CONTAINER ID IMAGE COMMAND CREATED STATUS …

springboot单文件,多文件下载方式

简单大文件下载&#xff1a; /*** 下载大文件* param path 路径* param fileName 文件名* return* throws IOException*/ public static ResponseEntity<InputStreamResource> downloadFile(String path, String fileName) throws IOException {Path filePath Paths.ge…

操作系统(2) (进程调度/进程调度器类型/三种进程调度/调度算法)

目录 1. 介绍进程调度&#xff08;Introduction to Process Scheduling&#xff09; 2. 优先级调度&#xff08;Priority Scheduling&#xff09; 3. CPU 利用率&#xff08;CPU Utilization&#xff09; 4. 吞吐量&#xff08;Throughput&#xff09; 5. 周转时间&#xf…

PAT甲级-1127 ZigZagging on a Tree

题目 题目大意 给出一棵树的中序和后序遍历&#xff0c;要求按层序输出这棵树&#xff0c;但是按照从左到右&#xff0c;再从右到左&#xff0c;再从左到右的顺序。 思路 由中序遍历和后序遍历可以构造出一棵二叉树。观察题目中要求的输出顺序&#xff0c;发现层数为奇数的都…

FineReport 数据集

概念&#xff1a;数据集是指可直接应用于模板设计的数据展现集合。 按其来源范围数据集可以分为 数据库查询内置数据集文件数据集SAP 数据集存储过程多维数据库关联数据集树数据集 1、数据库查询 数据库查询&#xff1a;指从定义好的数据库连接中&#xff0c;就是数据源中使用…