如何动态添加usercontrol时验证它

编程入门 行业动态 更新时间:2024-10-23 09:28:01
本文介绍了如何动态添加usercontrol时验证它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已通过此代码动态地将用户控件添加到面板

i have added a usercontrol dynamically by this code to a panel

int c = 0; private void button1_Click(object sender, EventArgs e) { int v; v = c++; panel1.VerticalScroll.Value = VerticalScroll.Minimum; UserControl1 us = new UserControl1(); us.Name = "us" + v; us.Location = new Point(150, 5 + (30 * v)); us.Tag = btn; panel1.Controls.Add(us); }

i在表单上有一个用于制作动态用户控件的按钮,另一个用于将文本传递给其他表单,然后将其传递给另一个表单i如果usercontrol中的任何控件都留空,想要验证文本并清除传输文本的过程,usercontrol中的控件是组合框和文本框,所以我写了这个方法

i have a two button on the form one for making the dynamic usercontrol and one for passing the text to other form, before passing it to another form i want to validate the text and cancle the process of transferring the text if any control in the usercontrol is left blank,the control in the usercontrol is comboboxes and textbox, so i have writte this method

UserControl1 uss = new UserControl1(); public bool validation() { if (ussboBox1.Text == string.Empty) { return true; } else { return false; } }

以下代码位于验证按钮

and the below code is at the validating button

bool vss = validation(); if (!vss) { errorlabel.Visible = false; //this label is defaultly set to false Form4 fl = new Form4(); fl.Show(); } else { errorlabel.Visible = true; //this label is defaultly set to false } //but the validation is not done properly, whats the problem here

推荐答案

如果动态添加内容,则应创建一个集合来跟踪它们,以便您可以出于某种原因迭代它们。您的验证方法不是在寻找您动态添加的控件,那么它如何验证它们呢? If you add things dynamically, you should create a collection to keep track of them so you can iterate over them for whatever reason. Your validation method is not looking for controls you added dynamically, so how can it validate them ?

更多推荐

如何动态添加usercontrol时验证它

本文发布于:2023-11-11 18:07:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   usercontrol

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!