BindingNavigator events

Anonymous
2024-11-09T02:32:53.0166667+00:00

Hi

Relatively new to C#.

I have added a BindingNavigator in code using:

		private readonly BindingNavigator BN = new(true);

Everything works fine and all the common BindingNavigator controls function correctly.

I added it in code, and set a few Properties then adding to the Form control collection.

Now I need to access one of those common controls - the 'delete item' one - so I can add code to verify the user really want to delete the record.

So, how do I find the event names and signatures so I can add a handler?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 6,735 Reputation points
    2024-11-09T06:12:55.93+00:00

    How about cloning the DeleteButton?

    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
        private readonly DataGridView dataGridView;
        private readonly BindingSource bindingSource;
        private readonly BindingNavigator bindingNavigator;
        private readonly DataTable dataTable;
    
        public Form1() {
            InitializeComponent();
    
            dataTable = new DataTable();
            dataTable.Columns.Add("ID", typeof(int));
            dataTable.Columns.Add("Name", typeof(string));
    
            dataTable.Rows.Add(1, "Alice");
            dataTable.Rows.Add(2, "Bob");
            dataTable.Rows.Add(3, "Charlie");
    
            bindingSource = new BindingSource {
                DataSource = dataTable
            };
    
            dataGridView = new DataGridView {
                Dock = DockStyle.Fill,
                DataSource = bindingSource
            };
    
            bindingNavigator = new BindingNavigator(true) {
                Dock = DockStyle.Top,
                BindingSource = bindingSource
            };
    
            using (ToolStripButton deleteButton = (ToolStripButton)bindingNavigator.DeleteItem) {
                bindingNavigator.Items.Remove(deleteButton);
                ToolStripButton newDeleteItem = CloneToolStripButton(deleteButton);
                newDeleteItem.Click += DeleteButton_Click;
                bindingNavigator.Items.Add(newDeleteItem);
            }
    
            Controls.Add(dataGridView);
            Controls.Add(bindingNavigator);
        }
    
        static ToolStripButton CloneToolStripButton(ToolStripButton oldButton) {
            var newButton = new ToolStripButton {
                Name = oldButton.Name,
                Text = oldButton.Text,
                Image = new Bitmap(oldButton.Image),
                RightToLeftAutoMirrorImage = oldButton.RightToLeftAutoMirrorImage,
                DisplayStyle = oldButton.DisplayStyle
            };
            return newButton;
        }
    
        private void DeleteButton_Click(object sender, EventArgs e) {
            if (bindingNavigator.Validate()) {
                var result = MessageBox.Show("Do you want to delete it?",
                                             "Question",
                                             MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes) {
                    bindingSource.RemoveCurrent();
                    bindingNavigator.Refresh();
                }
            }
        }
    }
    

  2. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2024-11-11T02:59:44.28+00:00

    Check out my BindingNavigator repository which demonstrates how to prompt yes/no before removing a record. A custom dialog is used that set the default button to no.

    A1


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.