刷新以前调用的类的属性

时间:2015-08-09 19:33:46

标签: c# winforms

说,我已经在我的主窗体中调用了一个类,比如

BSPclass bsp = new BSPclass();

在该课程中有1个属性,

class BSPclass 
{ 
    public string p1 = "player1";
}

然后,如果我单击mainform中的按钮,将出现一个新表单(如form2), 比方说,有一个文本框可以为p1设置一个值(同样也在form2中调用了BSPclass)

private void save_Click(object sender, EventArgs e) 
{ 
    BSP.p1 = txtbox.text;
}

现在问题是,如果我关闭form2,它不会在mainform中显示p1的neew值,仍会显示“player1”

如何刷新或做其他事情?

2 个答案:

答案 0 :(得分:0)

这取决于您创建BSP实例的位置?如果它是在form2中创建的,那么它与表单一起处理。这两个实例都不同。您必须使用类似的代码将其传递给主窗体:

假设您的主要表单包含BSPclass BSPclass bsp = new BSPclass();

的内容

在你身上form2声明一个像

这样的委托
public delegate void passinfotomainform(string data);

创建该委托类型的事件

public event passinfotomainform datapassevent;

然后在按钮中单击提升事件

private void save_Click(object sender, EventArgs e) 
{ 
BSP.p1 = txtbox.text;
if(datapassevent != null)
 datapassevent(txtbox.text);
}

在主要表单中,注册此事件,如

form2.datapassevent += new EventHandler(myhandler);

myhandler获取数据

private void myhandler(string arg)
{
  bsp.p1 = arg; //set the bsp in main form
}

答案 1 :(得分:0)

您可以简单地将textbox的值传递给mainform,然后刷新p1。您不应该在form2中创建BSPClass的实例。请参阅以下代码:

在form2中:

$(function() {

$("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        // Prevent spam click and default submit behaviour
        $("#btnSubmit").attr("disabled", true);
        event.preventDefault();

        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var message = $("textarea#message").val();
        var human = $("textarea#human").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            data: {
                name: name,
                phone: phone,
                email: email,
                message: message,
                human: human
            },
            cache: false,
            success: function() {
                // Enable button & show success message
                $("#btnSubmit").attr("disabled", false);
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');

                //clear all fields
                $('#contactForm').trigger("reset");
            },
            error: function() {
                // Fail message
                $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#contactForm').trigger("reset");
            },
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
});

$("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
});
});

// When clicking on Full hide fail/success boxes
$('#name').focus(function() {
$('#success').html('');
});

在mainform中:

public string pTemp;
private void save_Click(object sender, EventArgs e) 
{ 
    pTemp = txtbox.text;
}