从windows app中的usercontrol访问值 [英] Accessing the value from usercontrol in windows app

查看:90
本文介绍了从windows app中的usercontrol访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为AddRow的用户控件和一个名为TimeSheet的表单,Timesheet有一个save_btn事件,点击save_btn后,在usercontrol中输入的值应写入数据库,但我无法从usercontrol访问该值



以下是代码



  public   void  save_btn_Click( object  sender,EventArgs e)
{

SqlCommand cmdinsert = new SqlCommand( 插入TimeSheet(Project_Name,Activity_Name,周一,周二,周三,周四,周五,周六,周日)值(@ Project_Name,@ Activity_Name,@ Monday,@ Tuesday,@ Wednesday,@ Thursday,@ Friday,@ Saturday,@星期天),con);
cmdinsert.Parameters.AddWithValue( @ Project_Name添加 .project_cbox.SelectedIndex);
cmdinsert.Parameters.AddWithValue( @ Activity_Name,activity_cb.SelectedIndex);
cmdinsert.Parameters.AddWithValue( @ Monday添加 .monday_txt_val);
cmdinsert.Parameters.AddWithValue( @ Tuesday添加 .tuesday_txt_val);
cmdinsert.Parameters.AddWithValue( @ Wednesday添加 .wednesday_txt.Text);
cmdinsert.Parameters.AddWithValue( @ Thursday添加 .thursday_txt.Text);
cmdinsert.Parameters.AddWithValue( @ Friday添加 .friday_txt.Text);
cmdinsert.Parameters.AddWithValue( @ Saturday添加 .saturday_txt.Text);
cmdinsert.Parameters.AddWithValue( @ Sunday添加 .sunday_txt.Text);
con.Open();
cmdinsert.ExecuteNonQuery();
con.Close();

}





此事件在时间表中,但值为空字符串...我无法访问价值,有解决方案吗?这是窗口应用程序



  public   string  monday_txt_val 
{
get
{
return monday_txt.Text;
}

set
{
monday_txt.Text = ;
}
}





monday_txt是一个在输入值后动态创建的文本框,但它正在读取一个空字符串,尽管我在文本框中输入了一个值,当它传递给monday_txt的表格值为空字符串时,如何解决这个问题?



唯一方法我可以想到访问monday_txt是通过在Timesheet中创建一个AddRow实例。但是当创建新实例时我将无法获得该值,请帮助

解决方案

< blockquote>你不想创建一个新实例 - 你需要获得你动态创建的UserControl的实际实例并呈现给用户供他填写。有两种方法:

1)当你调用new AddRow()来添加控件时,在表单类中保持类级引用,

2)查看Form.Controls集合以查找AddRow实例 - 记住如果你把它添加到面板或其他容器,你会需要通过容器递归才能找到它。


I have a usercontrol called AddRow and a form called TimeSheet ,Timesheet has a save_btn event, on click of save_btn the value enter in usercontrol should be written to database,but I am not able to access the value from usercontrol

below is the code

public void save_btn_Click(object sender, EventArgs e)
    {

        SqlCommand cmdinsert = new SqlCommand("insert into TimeSheet (Project_Name ,Activity_Name,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday) values (@Project_Name,@Activity_Name,@Monday,@Tuesday,@Wednesday,@Thursday,@Friday,@Saturday,@Sunday) ", con);
        cmdinsert.Parameters.AddWithValue("@Project_Name",  add.project_cbox.SelectedIndex);
        cmdinsert.Parameters.AddWithValue("@Activity_Name", activity_cb.SelectedIndex);
        cmdinsert.Parameters.AddWithValue("@Monday", add.monday_txt_val);
        cmdinsert.Parameters.AddWithValue("@Tuesday", add.tuesday_txt_val);
        cmdinsert.Parameters.AddWithValue("@Wednesday", add.wednesday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Thursday", add.thursday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Friday", add.friday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Saturday", add.saturday_txt.Text);
        cmdinsert.Parameters.AddWithValue("@Sunday", add.sunday_txt.Text);
        con.Open();
        cmdinsert.ExecuteNonQuery();
        con.Close();  

    }



this event is in timesheet but the value is empty string...I just cant access the value,is there a solution? this is window application

public string monday_txt_val
    {
        get
        {
            return monday_txt.Text;
        }

        set
        {
            monday_txt.Text = value;
        }
    }



monday_txt is a textbox that is created dynamically,after the value is entered ,but it is reading a empty string,despite me entering a value in textbox,when it is passed to form value of monday_txt is empty string,how to fix this?

only way I can think of accessing monday_txt is by creating a instance of AddRow in Timesheet..but When new instance is created I will not be able to get the value, kindly help

解决方案

You don''t want to create an new instance - you need to get the actual instance of the UserControl that you created dynamically and presented to the user for him to fill in. There are two ways:
1) Keep a class level reference in your form class when you call new AddRow() to add the control,
2) Look through the Form.Controls collection to find the AddRow instance - remember that if you add it to a panel or other container, you will need to recurse through containers to find it.


这篇关于从windows app中的usercontrol访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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