Modify Windows Forms Control from another Project

前端 未结 3 885
广开言路
广开言路 2021-01-19 02:48

I have a Control lblDate in User Control MainScreen. I would like to modify it in a method in class Date, which is in another project

3条回答
  •  醉梦人生
    2021-01-19 02:54

    I'd rather advise you make your GameDate method to return a string then call it from your main project which are essentially what libraries are meant for. e.g.:

    public static string CalculateDate()
    {
        return month.ToString() + "/" + displayDay.ToString() + "/" + year.ToString();
    }
    
    //in your main project
    LabelDate.Text = myLibrary.CalculateDate();
    

    For accessing it from other controls in the same project, you should use a delegate - Provide a delegate which performs the addition, declare the delegate and invoke the function from your CaculateDate. Example:

    public delegate void dateSet(string);
    
    public void setDate(string date)
    {
        labelDate.Text = date;
    }
    

    then in your CaculateDate method:

    labeldate.Invoke(new MainForm.dateSet(), GameDate);
    

提交回复
热议问题