0

There are only (3) things I need to know in here. Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:

public partial class frm_main : Form
{
    public_class pc = new public_class();
    public frm_main()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(frm_main_Load);
    }
    private void close_button_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void frm_main_Load(object sender, EventArgs e)
    {
        pc.screen_adjust(close_button);
    }
}

And here's my public_class.cs code:

public partial class public_class
{
    public int cl_b;
    public void screen_adjust(Button b)
    {
        cl_b = frm_main.ActiveForm.Width;
        frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
        frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
        b.Left += frm_main.ActiveForm.Width - cl_b;
    }
}

The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:

  1. Did I do a correct "VB.net module" in C# properly?
  2. How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
  3. In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...

Thanks!

  • 1. No http://stackoverflow.com/questions/5331695/what-would-be-considered-a-vb-net-module-in-c – Tim Schmelter Nov 11 '14 at 13:43
  • 2
    You can make the method static (and the class, if all methods are marked as static). Then you could call it by `public_class.screen_adjust(..)` – scheien Nov 11 '14 at 13:44
  • 1
    3. you are passing in the button you want to change the text of. So you could simply say `b.Text = "Some other text";` – Corak Nov 11 '14 at 13:53
  • Btw. it sounds like you're not 100% clear on the concept of [classes](http://msdn.microsoft.com/library/x9afc042.aspx), [instances (objects)](http://msdn.microsoft.com/library/k6sa6h87.aspx) and [static classes](http://msdn.microsoft.com/library/79b3xss3.aspx) (probably the closest to VB Modules). – Corak Nov 11 '14 at 13:57
  • ah, that number 3 is constructed with a button parameter to change the specific button's string.. what if I had many? that's why I need to do something like (VB.net) frm_main.button1.Text = "New Text" ... but I don't know how to do it in C# – Pat-kun Teruel Nov 11 '14 at 14:12
  • and lastly, I forgot, there is one other problem: When I debug this and load, it exits without errors but redirects me to my public_class.cs code – Pat-kun Teruel Nov 11 '14 at 14:14
  • This is a bit more tricky. But one way would be not to pass the button, but to pass the entire form object. You change the method signature to `screen_adjust(frm_main form)`. Then you can change all `frm_main.ActiveForm` inside to `form`. To get to the button, you can try `var button = form.Controls.Find("button1", false).FirstOrDefault() as Button;`. And then `if (button != null) { button.Text = "New Text"; }`. But then you also need to change `frm_main_Load` to `pc.screen_adjust(this);`. – Corak Nov 11 '14 at 14:26
  • With `Controls.Find("button1", false)` you search for all (direct) child controls with the name "button1". Since `button1` was added to `frm_main`, `button1` *is* a child control of `frm_main`. This will return a list, so you probably want the first one, or null if there is no control with that name. That's what `.FirstOrDefault()` does. You might need to add `using System.Linq;` for that to work. Then, what you get back might be a button or a checkbox or a lable or whatever. To be sure it's a button, you add `as Button`. Now, if it's not a button, it will be null. – Corak Nov 11 '14 at 14:31
  • So after that you check if it's null or not. If it's not, you have a real button and can change its `Text` property. - Like I said, it's a bit tricky, but hopefully my attempt at explaining helps a little. – Corak Nov 11 '14 at 14:34
  • yes, it helps a lot, Thank you! but that's not my main problem here, like I said, I have to call that public method in the form_load but it automatically closes... – Pat-kun Teruel Nov 11 '14 at 16:40

0 Answers0