Question about contexMenuStrip

Asked

Viewed 44 times

0

inserir a descrição da imagem aqui

The thing is, I need to add these tools, exactly as shown in the image.

I just need a little push on how to get started.

Insert text

Insert Image

Delete (button)

CODE

namespace frmLoginRPG
{
    public partial class frmMenuPrincipal : Form
    {
        private readonly Button[] _todosBotoes;
        public frmMenuPrincipal()
        {

            InitializeComponent();
            //Passo 1
            
            
            _todosBotoes = _mainPanel.Controls.OfType<Button>().ToArray();
        }
        
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            //Passo 2.1
            var controles = _todosBotoes.Where(btnRifts => (btnRifts.Tag as String ?? btnRifts.Text).ContainsIgnoreCase(txtBuscarJogo.Text)).ToArray();
            //Passo 2.2
            _mainPanel.Controls.Clear();
            //Passo 2.3
            _mainPanel.Controls.AddRange(controles);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }
 

        private void btnAdicionar_Click(object sender, EventArgs e)
        {

            Button button = new Button(); //criando botão
            button.Size = _todosBotoes[0].Size; //criando botão, puxando o tamanho dos outros botões
            button.BackColor = Color.AliceBlue; //definindo cor do botão
            _mainPanel.Controls.Add(button); //Adicionando botão no _mainPanel
            button.ContextMenuStrip = this.contextMenuStrip1; //Adicionando contextMenuStrip assim que o Botão for criado.
            
            MessageBox.Show("                  CRIADO        ");
            
        }
    }

  

}
namespace MyMethod
{
    public static class MyExtensions
    {
        public static bool ContainsIgnoreCase(this string source, string search)
        {
            return source.IndexOf(search, StringComparison.CurrentCultureIgnoreCase) >= 0;
        }
    }
}
`

1 answer

0

You create a contextMenu and sets it in the Button property, in the click event you will know which button came the click by sender:

    private void inserirTextoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string name = ((Button)sender).Name;
        MessageBox.Show("Você clicou para inserir no botão: " + name);
    }

    private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string name = ((Button)sender).Name;
        MessageBox.Show("Você clicou para excluir o botão: " + name);
    }
  • But I need those functions working, delete the button, put picture on the button, change description on the button... got it?

  • @Dario, just exchange the Messagebox of the rovann code for the function you need. Insert for example would be: Button btn = (Button) Sender; btn.Text = text;

  • Assuming text is a String you set, or asked the user.

  • I have no way to put working pq the 'insert text' as q the user will insert ? the delete, will delete from where ? understand.... rs only the concept

  • I understood, but in case I would be no user, only myself! Delete the Panel button insert text description on the button and put image on the button. all through the Contextmenu

  • I don’t have a database, nothing... It’s pretty raw

  • The delete function could look something like this: Button btn = (Button) Sender; _mainPanel.Controls.Remove(btn);

  • The other two, you need to get the text and image from somewhere. An option would be to open a new Form by passing the button as parameter and inside this Form, change the necessary properties as text and image.

  • It is missing that you design your application before developing and knowing which resource registration it will need. Since the first question you asked where I answered with example where a Datatable was origin of buttons. Now if you need to store this information, edit, insert or delete, none of this static part of the buttons inserted directly on the screen makes sense, you would have to change this whole part.

Show 4 more comments

Browser other questions tagged

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