如何在自定义控件中获取控件的值 [英] How to get values of the control in the Custom Control

查看:55
本文介绍了如何在自定义控件中获取控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个自定义控件,该控件在Degsin时接收来自应用程序开发人员的输入,并根据开发人员的输入来呈现控件.现在,请告诉我如何获取这些控件(文本框,按钮等)的值.

Hi,

I have created a custom control that takes input from the application developer at the Degsin time and render the controls depending upon the inputs from the developer. Now Please tell me how to get the values of these controls(text box,button etc).

推荐答案

朋友,
我不确定,但我想这可能是答案...

如果您有一个名为MyControl的自定义控件.并且其中有一个名为MyBox01的文本框,则可以像这样访问它:
Hi Friend,
I am not sure but I guess it can be the answer...

If you have a custom control named MyControl. and it has a text box in it named MyBox01 then it can be accessed like this:
string mydata;
mydata = MyControl.MyBox01.Text;



希望对您有所帮助.
谢谢.



Hope this can help you.
Thank you.


朋友,
我回来了,下面是完整的代码,可以肯定会对您有帮助.

Hi Friend,
I am back, and here is the full code that may help you for sure.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Dynamic_Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TextBox mybox = new TextBox();
        Button mybutton = new Button();
        List<control> arraylist = new List<control>();
        private void initlist()
        {
            mybox.Text = "Hello";
            mybutton.Text = "Click";
            mybox.Left = 10;
            mybutton.Left = 10;
            mybox.Top = 10;
            mybutton.Top = 40;
            mybutton.Click +=new EventHandler(mybutton_Click);
            arraylist.Add(mybox);
            arraylist.Add(mybutton);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            initlist();
            foreach (Control myobj in arraylist)
            {
                this.Controls.Add(myobj);
            }
        }

        private void mybutton_Click(object sender, EventArgs e)
        {
            mybox.Text = "I am Changed";
        }
    }
}



该程序运行良好.它使用一个列表在屏幕上动态生成控件,并在该列表内设置一个回调.因此,无论何时调用此回调,它都会在函数内部执行任务.
按照此代码.它在屏幕上绘制了两个控件,第一个是文本框,第二个是按钮,按钮单击事件有一个事件回调设置.这将动态设置一个回调事件,并将其链接到函数mybutton_Click().因此,文本框内的文本会更改:-)

希望这段代码对您有所帮助.
问候
Tushar Srivastava:-)



This program is working fine. It uses a list to generate the controls dynamically on the screen and sets a callback inside this list. So, whenever this callback is called it performs the task inside the function.
As per this code. It draws two controls on the screen, the first being a Textbox and the next is a button, there is a Event callback setup for button click event. This dynamically sets up a callback event and links it to the function mybutton_Click(). Thus the text inside the textbox changes :-)

Hope this code helped you.
Regards
Tushar Srivastava :-)


这篇关于如何在自定义控件中获取控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆