Flame
Flame

Reputation: 35

Optimizing a repetiton

I have a small Menu strip item where I have a plethora of buttons which activate different forms.

The code for one button would be this:

    Form B1 = new Form1();
    private void Button1_Click(object sender, EventArgs e)
        {
            if (B1.Visible == false)
            {
                B1 = new Form1();
            }

            B1.Visible = true;
            B1.Activate();
        }

I also have a mouse enter- and leave event:

  private void Button1_MouseEnter(object sender, EventArgs e)
    {
        Button1.Text = "Something prdy intriguing";
    }
    private void Button1_MouseLeave(object sender, EventArgs e)
    {
        Button1.Text = "Hi";
    }

And a tooltip:

private void Tooltips()
{
ToolTip forB1 = new ToolTip();
forB1.SetToolTip(button1, "21.11.17");
}

Now imagine i need about 8 buttons for 8 different forms, that means i have to repeat all of these again and a gain, wasting time AND taking up a LOT of code space.

Is it possible to compress these in anyway? This is very out of my world, im unsure where to start optimizing.

Upvotes: 0

Views: 53

Answers (3)

Flame
Flame

Reputation: 35

So i am kinda stealing @Evk's code here but essentially this works the way I wanted to.

public void ButtonHandlers(Type NewForm)
    {

            NewButton.Click += (sender, args) =>
            {
                Form TheNewMain = (Form)Activator.CreateInstance(NewForm);
                if (TheNewMain.ShowDialog() != DialogResult.Cancel)
                {
                    TheNewMain.Activate();
                }
            };

Essentially what i added was instead of getting the Form i have to get the type, since what I want is that when a form is Visible, it won't open it twice, going by Evks code it opens it yes but upon close it's disposed and it cant create a new instance of it.

In code i just have to ask for typeof(formName) in as NewForm

Thanks Evk!

Upvotes: 0

Evk
Evk

Reputation: 101483

One option is move all this to one function:

public void AttachMenuStripButtonHandlers(
    Button btn, 
    Form form, 
    string enterText,
    string leaveText,
    string tooltip) {

    btn.Click += (sender, args) => {
        form.Visible = true;
        form.Activate();
    };
    btn.MouseEnter += (sender, args) => {
        btn.Text = enterText;
    };
    btn.MouseLeave += (sender, args) => {
        btn.Text = leaveText;
    };
    new ToolTip().SetToolTip(btn, tooltip);
}

And for each button call like this:

AttachMenuStripButtonHandlers(Button1, B1, "on enter", "on leave", "tooltip");

Upvotes: 1

Gor Rustamyan
Gor Rustamyan

Reputation: 930

For second part of your question, You could do something like this

private void Button_MouseEnter(object sender, EventArgs e)
{
    ((Button)sender).Text = "Something prdy intriguing";
}
private void Button_MouseLeave(object sender, EventArgs e)
{
    ((Button)sender).Text = "Hi";
}

You need to attach same event handler to all buttons.

Upvotes: 0

Related Questions