Question Access buttons in other form

Ismael

Member
Joined
Mar 20, 2020
Messages
18
Programming Experience
1-3
Hi, Folks.
I Have a form that has 5 buttons to deal with information that will be passed to a DataBase: New, Edit, Delete, Save and Cancel. Normally Save and Cancel are disabled and the others are enabled. When user presses New (for example), The 3 first become disabled; Save and Cancel become enabled. And so on (I guess you have seen this). I've created a procedure that enables and disables the buttons according to the situation and it's working fine. Now I have many other forms in my application that have the same buttons and I need to use the same routine on them. My idea it to create the routine in another class which can be accessed by all forms. My problem is: how do I access the buttons that are in a form? I have already turned the buttons in the form public (instead of private). Now what? For example I have Form1 with buttons Bt_New, Bt_Edit, Br_Erase, Bt_Save and Bt_Cancel. The class I created is Routines.
In Routines:
C#:
public void buttons(int Op)
{
    if (op == 1)
    {
        Bt_New.Enabled = false;
        Bt_Edit.Enabled = false;
        Bt_Erase.Enabled = false;
        Bt_Save.Enabled = true;
        Bt_Cancel.Enabled = true;
    }
    else if (op == 2)
    {
        Bt_New.Enabled = true;
        Bt_Edit.Enabled = true;
        Bt_Erase.Enabled = true;
        Bt_Save.Enabled = false;
        Bt_Cancel.Enabled = false;
    }
}
Can anybody help me? Thanks.
 
Last edited by a moderator:
The code you posted would not have compiled as it was. It's important that you post the actual code you're using because any small difference could send us off in the wrong direction. I have made appropriate changes so that the code would compile but we don't know if the end result is what you're actually running.
 
You are going about this the wrong way. What you should have done was create a user control and built the functionality into that control. You could then have added that control to any form you wanted that UI and functionality to be on. Also, don't use magic numbers for the states. Declare your own enum and then the possible values will be limited to exactly what you declare, where an int could be anything and is meaningless to anyone who reads the code.
 
Hi, jmcilhinney. First of all, I don't know how to format text here. Could you explain how you did this?
Second point: I didn't detail the code (meaning of op, for example) because it's irrelevant. Anyway, this variable indicates if the state is navigation or edition. Just this. You speak of a user control, but I don't know how to create it. Could you give a hint on where I can find this subject? Or could you give an explanation about it?
Thanks.
 
You add a user control to a project in much the same way as you add a form. You then design the user control in much the same way as you do a form too. Once you build, the control is automatically added to the Toolbox and you can add it to a form like you can any other control. In this case, you'd add your five Buttons and add pass-through events for them, i.e. your user control would handle the Click events of the Buttons and then raise its own events and the form(s) would handle those user control events. In this case, it seems that there are only two states for the Buttons so you could probably dispense with the enum and use a single bool property:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class EditingButtons : UserControl
    {
        public EditingButtons()
        {
            InitializeComponent();
        }

        private bool _isEditing = false;

        public bool IsEditing
        {
            get => _isEditing;
            set
            {
                _isEditing = value;

                newButton.Enabled = !_isEditing;
                editButton.Enabled = !_isEditing;
                eraseButton.Enabled = !_isEditing;
                saveButton.Enabled = _isEditing;
                cancelButton.Enabled = _isEditing;
            }
        }

        public event EventHandler NewClicked;
        public event EventHandler EditClicked;
        public event EventHandler EraseClicked;
        public event EventHandler SaveClicked;
        public event EventHandler CancelClicked;

        private void newButton_Click(object sender, EventArgs e)
        {
            NewClicked?.Invoke(this,EventArgs.Empty);
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            EditClicked?.Invoke(this, EventArgs.Empty);
        }

        private void eraseButton_Click(object sender, EventArgs e)
        {
            EraseClicked?.Invoke(this, EventArgs.Empty);
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveClicked?.Invoke(this, EventArgs.Empty);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            CancelClicked?.Invoke(this, EventArgs.Empty);
        }
    }
}
 
I don't know how to format text here. Could you explain how you did this?
You've used text editors before. How do you usually format text? You can see the toolbar right above where you are entering the text. Can you not take a minute to explore that toolbar and see what options are available? Being new just means that you don't know right now. It doesn't mean that you can't try to find out. If you're not new to software or the internet then finding out should not be a challenge. It's a matter of whether you're prepared to make an effort in order to help us help you.
 
I've explained this to you before. I'm pretty sure if you are able to drag and drop controls in Winforms, you surely know how to click three dots to insert your code in code tags using the forum editor.
 

Attachments

  • CodeTags.gif
    CodeTags.gif
    138.5 KB · Views: 392
jmcilhinney and Sheepings. Thanks for the tips on editing text. I'll try to do this next time.
jmcilhinney. I created the user control you sugested, but it didn't work. Please, have in mind that for you, this should be very easy, but for me (i'm new to C#) it's very difficult.
The user control really was available in tool box, but the buttons didn't act enabling and disabling the buttons. Another point: I need to include code in these buttons click event. How can I do this? Do you have any sugestion? Also, be patient with me.
Thanks.
 
The user control really was available in tool box, but the buttons didn't act enabling and disabling the buttons.
Did you set the IsEditing property at appropriate times? You could do that inside the control or outside; whichever is more appropriate. For example, if you always want IsEditing set to true when the New button is clicked, you can do that in the appropriate event handler inside the user control.
Another point: I need to include code in these buttons click event.
As I already said, the user control handles the Click events of the Buttons internally and then raises its own events that the forms handle. If you need to do something when the user clicks the New button, you handle the NewClicked event of the user control.
 
Back
Top Bottom