我想在另一个框架中按下按钮时从另一个框架调用一个方法,我目前唯一的代码就是这个+我的AddReview.cs中的按钮(Windows窗体框架)
应调用的方法:(位于MainScreen.cs中)
public void GetTrackLayout()
{
TrackInformation t = new TrackInformation(lblMainScreen_Username.Text, cBMainScreen_Search.SelectedValue.ToString());
fPanelUpperMainScreen.Controls.Add(t);
int valueInt = int.Parse(cBMainScreen_Search.SelectedValue.ToString());
t.pbTrackInformation_image.ImageLocation = tr.GetTrack(valueInt).trackPicture;
t.lblTrackInformation_Title.Text = tr.GetTrack(valueInt).title;
t.lblTrackInformation_RevType.Text = "Track".ToUpper();
var reviews = rr.GetMatchingReviewsTrack(valueInt);
t.lblTrackInformation_Count.Text = reviews.Count().ToString();
foreach (var review in reviews)
{
UserControl1 u = new UserControl1();
u.lblUser.Text = review.username;
u.lblComment.Text = review.comments;
u.lblDate.Text = review.date;
u.lblRating.Text = review.rating.ToString();
t.fpTrackInformation_Reviews.Controls.Add(u);
}
}
在我的MainScreen.cs上我添加了一个名为TrackInformation.cs的用户控件(这里没有太多代码显示,因此我没有发布它),在这个布局中添加评论的按钮是可用。
答案 0 :(得分:0)
您可以使用AddReview
表单创建活动:
partial class AddReview : Form
{
public event EventHandler SpecialButtonClicked;
public AddReview()
{
InitializeComponent();
// Normally you'd hook this up in the Designer, but for the sake of
// illustration, I show the subscription here
button1.Click += button1_Click;
}
private void button1_Click(object sender, EventArgs e)
{
EventHandler handler = SpecialButtonClicked;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
partial class MainScreen : Form
{
private void SomeMethod()
{
AddReview addReview = new AddReview();
addReview.SpecialButtonClicked += (sender, e) => GetTrackLayout();
addReview.Show();
}
}
换句话说,AddReview
表单公开了MainScreen
表单可以订阅的事件。单击该按钮时,AddReview
表单会引发该事件。当您创建AddReview
表单时,您订阅它并在处理程序中调用您的方法。