Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, November 9, 2012 12:12 PM
Hi All,
How to set the focus last added item in Listbox Click in Form2 Ok Button C#.net
I am using the two Forms one is Form1 and another one is Form2. Form1 I have Listbox1 and Form2 I have one TextBox1.
What I am doing is, I will enter the value in Form2 Textbox and I am adding the that value in From Listbox1. Please see the below image.
I need to set the focus last added item in Listbox1 once Clicking the Form2 Ok button.
What is my problem is Once Clicking the Form2 cancel button also it will setting focus the last item in the Listbox but I did not add any value.
I am using the below code. I need to set the focus last added item in Listbox1 When I will Clicking the Form2 Ok button.
Form1 Code:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Focus();
frm.ShowDialog(this);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.Focus();
}
Vijay
All replies (8)
Friday, November 9, 2012 7:02 PM ✅Answered
Ohh i got the point now, the code isnt coming back to the code:
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.Focus();
So you need to capture the close event like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.FormClosed += frmClosed;
frm.ShowDialog();
}
private void frmClosed(object sender, EventArgs e)
{
Form2 frm = (Form2)sender;
if (frm.HasAddedItem)
{
// Change the selected index
}
}
}
public partial class Form2 : Form
{
public bool HasAddedItem { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HasAddedItem = true;
Close();
}
}
Hope that helps =)
Desenvolvedor com ênfase em qualidade de código e performance. MCP - MCTS - Partner
Friday, November 9, 2012 12:25 PM | 1 vote
Hello Vijayark
I think here its a case to make an property on the Form2, the "HasAddedItem", and you surround your set focus code with an if like this:
if(frm.HasAddedItem)
{
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.Focus();
}
Sure, you have to manipulate that property in the Form2 code.
Regards,
Desenvolvedor com ênfase em qualidade de código e performance. MCP - MCTS - Partner
Friday, November 9, 2012 12:27 PM | 1 vote
Hi Vijay
try this
this.ListBox1.TopIndex = this.ListBox1.Items.Count - 1;
Regards
Harshad
Harshad..... Always 4 U
Friday, November 9, 2012 1:32 PM
Hi Raphael,
I need to set the focus last added item in Form1 Listbox1 When I will Clicking the Form2 Ok button.
Please can you share me complete example code. That is better understand for me.
Vijay
Friday, November 9, 2012 1:41 PM
Hi Harjohn,
I tried that one, It not setting focus last added item in Listbox.
For example: I will selected item two in Listbox and I will click the button1 but I did not add any value. Just I clicking the cancel button.
It will retain the selected the Set Focus for selected Item two in Listbox.
Please help on this request.
Vijay
Saturday, November 10, 2012 3:44 AM
Hi Raphael,
No, It come back to the code.
What is my issue is below code need fire only when I will click the Ok button.
listBox1.SelectedIndex = listBox1.Items.Count - 1;listBox1.Focus();
Its always firing When I click Cancel button and Form2 Close also.
You have any idea.
Vijay
Saturday, November 10, 2012 5:14 PM
Hi Vijayark
I got the problem now, give a look in my last post, that with an event handling
Desenvolvedor com ênfase em qualidade de código e performance. MCP - MCTS - Partner
Monday, November 12, 2012 4:05 AM
Thanks Raphael.
I will check and let you know.
Vijay