开发者

C# Set Form Parent after calling method from another class

开发者 https://www.devze.com 2023-03-13 05:04 出处:网络
I\'ve searched Google all day and can\'t find the correct answer to my issue, hoping someone here can help me.

I've searched Google all day and can't find the correct answer to my issue, hoping someone here can help me.

So, in the "Main" form I have the method to show a form that needs to be centered directly above the parent form (frmMain). Normally I would call ShowDialog(this) to see the parent, but for some reason I have to set the loadNewsFeedItem to static in order to see the method from the flpNewsFeedHeader : Label derrived class (below). The OnClick event triggers the method loadNewsFeedItem().

When I call this to set the parent, I'm getting the message "Keyword 'this' is not valid in a static property, static method, or static field initializer"

namespace NewsFeeds
{
public partial class FrmMain : Form
{
    public static void loadNewsFeedItem()
    {
        frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
        frmFeedView.ShowDialog(this); // Error occurs on this line, when calling this via a static method
    }
}
}

public class flpNewsFeedHeader : Label
{
 private int FeedID = 0;

 public int theFeedID
 {
     get { return FeedID; }
     set { FeedID = value; }
 }

 protected override void OnClick(EventArgs e)
 {开发者_如何学JAVA
     FrmFuncs.selFeedID = FeedID;
     Thread thrShowFeed = new Thread(new ThreadStart(FrmMain.loadNewsFeedItem));
     thrShowFeed.Start();
 }
}

Can someone please give me a corrected code example or a hint as to how to get the loadNewsFeedItem() to be visible without setting the accessor to static, or how to work around this in a static accessor?

Thanks in advance!

Chris


Edit: used ActiveForm for owner.

   public partial class FrmMain : Form
   {
      public static void loadNewsFeedItem(Form owner)
      {
         frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
         frmFeedView.ShowDialog(owner);
      }
   }
}

public class flpNewsFeedHeader : Label
{
   private int FeedID = 0;

   public int theFeedID
   {
      get { return FeedID; }
      set { FeedID = value; }
   }

   protected override void OnClick(EventArgs e)
   {
      FrmFuncs.selFeedID = FeedID;
      // Shouldn't need a new thread.  Already on the GUI thread.
      FrmMain.loadNewsFeedItem (System.Windows.Forms.Form.ActiveForm);
   }
} 


may be you mean this:

frmFeedView.Owner = System.Windows.Forms.Form.ActiveForm;
frmFeedView.ShowDialog();


In a static method, this is meaningless. One option is to skip the parameter

frmFeedView.ShowDialog();

The other option is to setup a static variable as shown below (but beware, it can have side effects if you try to open multiple instances of FrmMain)

public partial class FrmMain : Form
{
    private static FrmMain staticInstance;
    public FrmMain()
    {
       staticInstance = this;
       InitializeComponent();
       ...
    }

    public static void loadNewsFeedItem()
    {
        frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
        frmFeedView.ShowDialog(staticInstance ); 
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消