User control not workin fine

Ismael

Member
Joined
Mar 20, 2020
Messages
18
Programming Experience
1-3
Hi, folks.
I created a UserControl to have some buttons availabe in a form. These buttons control automatically their enable property, and it's workin fine. I wanted to make it more sophisticated, so I included a tip, so as when the user passes the cursor over the button, it shows the tip. This is working partially, because when I change the tip text in the form where I included this user control, it ignores the change. It always use the default text I included in the user control. The code (simplified) is showed below.
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 GestaoLav
{
    public partial class Botoes : UserControl
    {
        public Botoes()
        {
            InitializeComponent();
            TipNovo.SetToolTip(this.bt_Novo, _DicaNovo);
            TipSalvar.SetToolTip(this.bt_Salvar, _DicaSalvar);
        }
        private bool _EmEdicao = false;
        private string _DicaNovo = "Cria um registro";
        private string _DicaSalvar = "Salva os dados do novo registro";

        public string DicaNovo
        {
            get => _DicaNovo;
            set
            {
                _DicaNovo = value;
            }
        }
        public string DicaSalvar
        {
            get => _DicaSalvar;
            set
            {
                _DicaSalvar = value;
            }
        }
       public bool EmEdicao
        {
            get => _EmEdicao;
            set
            {
                _EmEdicao = value;
                bt_Novo.Enabled = !_EmEdicao;
                bt_Editar.Enabled = !_EmEdicao;
                bt_Apagar.Enabled = !_EmEdicao;
                bt_Salvar.Enabled = _EmEdicao;
                bt_Cancelar.Enabled = _EmEdicao;
            }
        }
        public event EventHandler NovoClicked;
        public event EventHandler SalvarClicked;
        private void NovoButton_Click(object sender, EventArgs e)
        {
            NovoClicked?.Invoke(this, e);
            EmEdicao = true;
        }
        private void SalvarButton_Click(object sender, EventArgs e)
        {
            SalvarClicked?.Invoke(this, EventArgs.Empty);
            EmEdicao = false;
        }
    }
}
Also, lines 22 and 23, where I define the defalt tips are in a light colour, and VS indicates there is something wrong, but I can't find it. Can anyone help me? What is wrong in my code?
Thank you.
 
The only place you actually set the tool tip text is in the constructor. If you want the text to change, you have to have code to change it. In the property setters, you modify the fields but there's no link between those fields and the ToolTips. I would suggest that you get rid of those fields completely and have the properties interact directly with the ToolTips.
C#:
public Botoes()
{
    InitializeComponent();
    TipNovo.SetToolTip(this.bt_Novo, "Cria um registro");
    TipSalvar.SetToolTip(this.bt_Salvar, "Salva os dados do novo registro");
}

private bool _EmEdicao = false;

public string DicaNovo
{
    get => TipNovo.GetToolTip(this.bt_Novo);
    set => TipNovo.SetToolTip(this.bt_Novo, value);
}

public string DicaSalvar
{
    get => TipSalvar.GetToolTip(this.bt_Salvar);
    set => TipSalvar.SetToolTip(this.bt_Salvar, value);
}
Some things to note. Firstly, there's no need to set the tool tip text in code like that, as you can set it in the designer, in the Properties window for each control. Secondly, there's no need for multiple TooTips. You can add a single ToolTip to a form and use it for every control.
 
Last edited:
He's half way there to implanting a model view, which would be the way I think he should be directed. In the short-coming, people will be moving to WPF; and MVVM will be one of the questions people will be asking how to implement. I'd advise teaching people to write their code in the more traditional way with the implementation of INotifyPropertyChanged interface with MVVM instead of using the old and dated Winforms way of only using a single property value. Granted, its more effort to explain, more to teach, but its more efficient for the users if you want to teach them modern practices. Moreover I'd be encouraging users to use WPF over Winforms, as we should be moving with the times, and not being stuck in the moment.
 
Back
Top Bottom