I want to force call [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp] .

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
Hello
I want to force call [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp]

C#:
namespace MyWinForm
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
      private void textBox1_KeyUp(object sender, KeyEventArgs e)
      {  // After textBox1_KeyUp text into the textbox, I try to force call listBox1_SelectedIndexChanged.
         // listBox1_SelectedIndexChanged is private type.... I don't know this part.  
          listBox1.listBox1_SelectedIndexChanged(); //<===========  I want to force call  [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp]
      }
      private void textBox1_TextChanged(object sender, EventArgs e)
      { // I will enter text into the textbox.
           listBox1.SelectedIndexChanged -= new EventHandler(listBox1_SelectedIndexChanged);    
           listBox1.SelectedIndex = idx;
           listBox1.SetSelected(idx, true);
           listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
      }
      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         int nIndex = listBox1.SelectedIndex;
      }
      private void Form1_Load(object sender, EventArgs e)
      {
         listBox1.Items.Add("Apple");
         listBox1.Items.Add("Banana");
         listBox1.Items.Add("Coconut");
         listBox1.Items.Add("Grape");

         listBox1.SelectedIndex = 1;
      }
   }
}
 
Last edited:
Why?

That is completely the wrong approach if you are hoping to not duplicate code. The right thing to do is move the code out into another method and call that method from other the selected index change event handler and from the other place where you wanted to call the selected change handler.

I know that in the paragraph above I told you the right way to do things. Now that wrong way to do it: The event handler code that you write is just a regular method. There is nothing magic about it. Just call that method passing in the correct parameters. Pray that you wrote the method that it doesn't assume that it was called by the system.
 
I need a way to force call force call [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp]

Please .... Please ....Please teach me

**** Don't modify the code below***
**** Without modifying the code below, you need to add listBox1_SelectedIndexChanged to textBox1_KeyUp.



C#:
namespace MyWinForm
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
      private void textBox1_KeyUp(object sender, KeyEventArgs e)
      {  // After textBox1_KeyUp text into the textbox, I try to force call listBox1_SelectedIndexChanged.
         // listBox1_SelectedIndexChanged is private type.... I don't know this part.
          listBox1.listBox1_SelectedIndexChanged(); //<===========  I want to force call  [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp]
      }
      private void textBox1_TextChanged(object sender, EventArgs e)
      { // I will enter text into the textbox.
           listBox1.SelectedIndexChanged -= new EventHandler(listBox1_SelectedIndexChanged);  
           listBox1.SelectedIndex = idx;
           listBox1.SetSelected(idx, true);
           listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
      }
      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         int nIndex = listBox1.SelectedIndex;
      }
      private void Form1_Load(object sender, EventArgs e)
      {
         listBox1.Items.Add("Apple");
         listBox1.Items.Add("Banana");
         listBox1.Items.Add("Coconut");
         listBox1.Items.Add("Grape");

         listBox1.SelectedIndex = 1;
      }
   }
}
 
Last edited:
All pseudo-code belowe:

Given:
C#:
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    do key-up stuff
    
    want to do listBox1 selection index changed stuff here
}

void listBox1_SelectionIndexChanged(object sender, EventArgs e)
{
    do some magic stuff
}

The correct thing to do is:
C#:
void DoKeyUpStuff()
{
    do key-up stuff
}

void DoSpecialStuff()
{
    do some magic stuff
}

void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    DoKeyUpStuff();
    DoSpecialStuff();
}

void listBox1_SelectionIndexChanged(object sender, EventArgs e)
{
    DoSpecialStuff();
}

The incorrect thing to do:
C#:
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    do key-up stuff
    
    listBox1_SelectionIndexChanged(listBox1, new EventArgs());
}

void listBox1_SelectionIndexChanged(object sender, EventArgs e)
{
    do some magic stuff
}
 
I need a way to force call force call [ listBox1_SelectedIndexChanged] IN [ textBox1_KeyUp]

Why? It's a terrible idea. Who told you that's what you should do and why? If this is homework or the like then you should be stating that in the OP. If this is actual work or you're trying to learn something then you should be trying to write good code rather than garbage. If you're determined to do it then you've already been told that it's a method, like any other. Why do you need us to tell you how to call a method? If your actual question is what arguments should you pass when calling it, that's the question you should have actually asked.
 
Oh. Wait. I forgot that this is Patrick we are dealing with. He (or the group of people sharing his account?) always has some weird constraint, or is always getting into an X-Y problem.
 
If your event handler for SelectedIndexChanged doesnt use the sender/e arguments, just pass null for them...If it does, pass something sensible for them so the code still works
 
Back
Top Bottom