Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:19:51+00:00 2026-06-06T19:19:51+00:00

Say I have 2 buttons witch supposed to perform the same operation but on

  • 0

Say I have 2 buttons witch supposed to perform the same operation but on different objects.

Currently I’m passing all the needed references to the method like this:

    private void sub1_add_to_db_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(
            substanse1, sub1_add_to_db_btn, sub2_add_to_db_btn, sub1_found_in_db_list,
            sub2_found_in_db_list, false, sub1_listBox, sub2_listBox);
    }

    private void sub2_add_to_db_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(
            substanse2, sub2_add_to_db_btn, sub1_add_to_db_btn, sub2_found_in_db_list,
            sub1_found_in_db_list, false, sub2_listBox, sub1_listBox);
    }

I was wondering if there is some other, more efficient way to do that. Thanks.

EDIT:

This is how some of my code looks like and it making me CRAZY!!!

private void sub1_add_to_db_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(substanse1, sub1_add_to_db_btn, sub2_add_to_db_btn,
            sub1_found_in_db_list, sub2_found_in_db_list, false, sub1_listBox, sub2_listBox);
    }

    private void sub2_add_to_db_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(substanse2, sub2_add_to_db_btn, sub1_add_to_db_btn,
            sub2_found_in_db_list, sub1_found_in_db_list, false, sub2_listBox, sub1_listBox);
    }

    private void sub1_edit_name_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(substanse1, sub1_add_to_db_btn, sub2_add_to_db_btn,
            sub1_found_in_db_list, sub2_found_in_db_list, true, sub1_listBox, sub2_listBox);
    }

    private void sub2_edit_name_btn_Click(object sender, EventArgs e)
    {
        Add_Substance_To_Database(substanse2, sub2_add_to_db_btn, sub1_add_to_db_btn,
            sub2_found_in_db_list, sub1_found_in_db_list, true, sub2_listBox, sub1_listBox);
    }

    private void sub1_delete_from_db_btn_Click(object sender, EventArgs e)
    {
        Delete_Substance_From_DB(sub1_listBox,
            sub2_listBox,sub2_list_is_from_file,sub1_delete_from_db_btn,
            sub2_delete_from_db_btn);
    }

    private void sub2_delete_from_db_btn_Click(object sender, EventArgs e)
    {
        Delete_Substance_From_DB(sub2_listBox,
            sub1_listBox,sub1_list_is_from_file,sub2_delete_from_db_btn,
            sub1_delete_from_db_btn);
    }

For example:
If I want to delete a substance, I need to delete it from both of the lists and remove it from other lists, change the selection to the next substance etc…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:19:52+00:00Added an answer on June 6, 2026 at 7:19 pm

    Eventually I followed my own suggestion…
    I created an object array with all the controllers and added same methods to the controllers event handlers. In the method I simply select the right controller according to the sender.

    substance1_controllers = new object[]{
                sub1_main_listbox, sub1_peaks_list,sub1_found_in_db_list,
                sub1_similar_in_db_list, sub1_eigenvector_list,
                sub1_sourse_switch_btn, sub1_folder_btn,sub1_add_to_db_btn,
                sub1_edit_name_btn,sub1_delete_btn, sub1_picture_box,
                chart_peaks.Series[0], chart_compare.Series[0], true, -1};
    substance2_controllers = new object[]{
                sub2_main_listbox, sub2_peaks_list, sub2_found_in_db_list,
                sub2_similar_in_db_list,sub2_eigenvector_list,
                sub2_sourse_switch_btn, sub2_folder_btn, sub2_add_to_db_btn,
                sub2_edit_name_btn,sub2_delete_btn, sub2_picture_box, 
                chart_peaks.Series[1],chart_compare.Series[1], true, -1};
    

    It might look like it is harder to maintain that way but personally I found it very comfortable to maintain and use with the help of this table (and it looks great):

            // [0]  - Main Listbox
            // [1]  - Peaks Listbox
            // [2]  - Found in Database Listbox
            // [3]  - Found similar Listbox
            // [4]  - Eigenvectors Listbox
            // [5]  - Switch sourse Button
            // [6]  - Change Folder Button
            // [7]  - Add to Database Button
            // [8]  - Edit Name Button
            // [9]  - Delete Button
            // [10] - Picture Box
            // [11] - Peaks Chart Series
            // [12] - Compare Chart Series
            // [13] - List is from File Boolean
            // [14] - Previous Selected Index
    

    Method Example:

    void Delete_Substance_From_DB(object sender, EventArgs e)
        {
            object[] controller;
            object[] other_controller;
            if (((Button)sender).Name == "sub1_delete_btn")
            {
                controller = substance1_controllers;
                other_controller = substance2_controllers;
            }
            else
            {
                controller = substance2_controllers;
                other_controller = substance1_controllers;
            }
        }
    

    Using examle:

    if (((ListBox)other_controller[0]).Items.Count != 0)
            {
                if (((ListBox)other_controller[0]).Items.Count == index2)
                {
                    ((ListBox)other_controller[0]).SelectedIndex = index2 - 1;
                }
                else
                {
                   ((ListBox)other_controller[0]).SelectedIndex = index2;
                }
                Main_Listbox_Index_Changed(((ListBox)other_controller[0]), null);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have textboxes, dropdownlists and submit buttons. They are all inline-elements. Which means
Lets say I have one row with three columns - some buttons on left
I have 3 buttons in my html page, say ADD , UPDATE & DELETE
Say I have a couple of basic objects like so: [Serializable] public class Base
I have two radio buttons with the same name. Using JavaScript, how can I
Let say you have 2 WPF buttons. One uses TextBlock, not the other one.
I have a grid of buttons, lets say user opens up the grid and
Let's say, for example, that I have some buttons that calls AJAX requests when
Say I have a list with radio buttons in it. How do I make
Say I have 5 buttons on a page, numbered 1-5. When one is clicked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.