Use standard Form instance in another class

Asked

Viewed 73 times

2

This is my class HotKey

    public class HotKey
{
    private string nome;
    public HotKey(string nome)
    {
        this.nome = nome;
    }
    private void Start_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        Form1.StartClicked();
    }
}

And this is my class Form1

namespace Teste
{
    public partial class Form1 : Form
    {
        HotKey Start = new HotKey("start");
        public Form1()
        {
            InitializeComponent();
        }
        public void StartClicked()
        {
            MessageBox.Show("Started");
        }
    }
}

I want the function Start_KeyPressed call the function StartClicked which is at the instance of Form1

How do I do that? I’ve tried to pass the instance of Form1 as a parameter but not successful.

  • I’m closing the question because it turned out that she wasn’t clear at all, only the AP could answer what he wanted as an answer.

  • Actually, I think it’s clear and it might help other people...

  • Not only is it unclear what you’ve done is a gambit that doesn’t even have to do with the question, but I know you’ll think you’ve solved your problem, even if it’s not quite true and it’s caused you worse, but I also know there’s no point in arguing.

  • Sorry, I don’t want trouble... thanks for letting me know you’re wrong, you can close the question.

  • @Maniero I figured out a way to do this and I’m sure it’s not gambiarra, I can edit?

2 answers

3

Assuming that the rest of the class is implemented correctly and that it is the form itself that triggers the event of HotKey just call him by the received object since he is just the calling object:

(Form)sender.StartClicked();

I put in the Github for future reference.

You have to do the cast because receiving the object of the form as object the methods of Form will not be available. If this is confusing for you need to study inheritance.

If that’s not the case, the question cannot be answered with this information alone.

  • I certainly should study more, I didn’t expect it to be something complex... I thought there was some simple way that I didn’t know...

  • @Complex Alanassis? I find it simple, at least much simpler than what you are already doing :)

  • Let me get this straight, by using (Form)Sender I can access the Form that prompted the current class?

  • No, you access the class object that called this method.

  • Then I would have to call the Start_keypressed method through the Form?

  • It confused me because the Start_keypressed method is called automatically when the user clicks on a certain key

  • I don’t know, I thought you knew what you were doing, I just answered what you asked, about other things in your code is what I said, there’s no way to answer because the question isn’t about it and you don’t have enough information to help answer.

  • I managed, I will leave answer here to others with doubt

  • Surely, you are more able to know how to solve than someone who does not have all the necessary information.

Show 4 more comments

0

In order for the Hotkey class to have access to the members of the Form1 instance, I created added the following line to the Hotkey class:

public Form1 f1 = null;

And in the Form1 class, I did so:

HotKey start = new HotKey();
start.f1 = this;

That way, if I want to use some Form1 function I just need to do so:

f1.DoSomething();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.