in winform when i create a combobox i can found event "SelectedIndexChanged"
the event work after index of combobox changed
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("after index change app show this MessageBox ");
}
but in wpf i cannot found event "SelectedIndexChanged"
instead of i can found event "SelectionChanged"
but i have a problem when is use it before index of combobox event work but i want to after index change show my code in event "SelectionChanged"
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("before index change app show this MessageBox ");
}
what should i do . i want to show my MessageBox after i change index of my combobox
sry for my poor english
Actually The event 'SelectionChanged' is called after index and value are changed you can check it simple
public partial class MainWindow : Window
{
private string[] _cmbxSource = new string[] {
"ZeroIndex",
"FirstIndex"
};
public MainWindow()
{
InitializeComponent();
cmbx.ItemsSource = _cmbxSource;
cmbx.SelectionChanged += cmbx_SelectionChanged;
}
void cmbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(string.Format("Value and Index has been changed {0} {1}",
_cmbxSource[cmbx.SelectedIndex], cmbx.SelectedIndex));
}
}
Related
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
I Create a function NewLoad() and call it in butto1_click.
And i have event listBox1_SelectedIndexChanged which called itself during operation function "NewLoad"
private void button1_Click(object sender, EventArgs e)
{
NewLoad();
}
private void NewLoad()
{
String text = textBox1.Text.Trim();
textBox1.Text = text;
oleDbSelectCommand1.Parameters[0].Value = text;
dataSet11.Clear(); <<<--- call listbox1_SelectedIndexChanged
oleDbDataAdapter1.Fill(dataSet11);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataSet21.Clear();
}
why this happens and how i can to avoid it?
My psychic debugging skills tell me that the listbox is databound to the dataset.
When you clear your dataset, the listbox is emptied, and the selection changes.
This raises the relevant event.
If you have something selected in list box 1, when you clear it, the selected index will change, thus raising selection changed event.
This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}
To make it as simple as possible: ComboBox1 is bound to an empty list (in Form1 load event handler), and there is an event handler associated with ComboBox1:
private void CB1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Event fired");
}
private void Form1_Load(object sender, EventArgs e)
{
CB1.DataSource = list1;
CB1.ValueMember = "Name";
CB1.DisplayMember = "Name";
}
The form is loaded, CB1.SelectedIndex = -1, CB1.Text = "", CB1.Items.Count = 0
When I click on Button1, list1 is populated. Now the situation is as follows:
CB1.SelectedIndex = 0, CB1.Text = "Some Text", CB1.Items.Count =196
BUT, the event didn't fire, although SelectedIndex changed from -1 to 0, and I didn't get MessageBox.Show("Event fired"). However, when the user selects some item from the list, the event fires. Also, there is another button that clears list1, and consequently CB1.Items. When this button is pressed, the event also fires (SelectedIndex changes from X to -1).
I've tried to use other events, such as SelectedValueChanged, TextChanged, SelectionChangeCommitted, with no success.
Although there is a simple brute force workaround for this problem, I still do not understand why the problem appears in the first place, and thus cannot anticipate similar situations. That's why I would be grateful if somebody explained to me why no events are firing in the situation I described.
My comment got enough attention, so it seemed appropriate that I should put this as a potential answer. You should make sure that you've actually assigned the event to the method either through a delegate or in the designer with the properties of the combobox itself.
// Somewhere in the form load or init events
CB1.SelectedIndexChanged += new EventHandler(CB1_SelectedIndexChanged);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string> { "a", "b", "c" };
comboBox1.DataSource = list;
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 1;
}
}
I have a textBox and I call an event get focus, when click on it. The behaviour is different when I make a double click on it, how can I make an event for getting focus for double click on this textbox?
You can use the OnTap() and OnDoubleTap() methods of the TextBox. And in each method you can define the different logic and set the focus on the TextBox.
Update:
Here's a simple code structure on how to make it work:
XAML:
<TextBox x:Name="InputTextBox" Margin="0,0,0,520" />
C#
public MainPage()
{
InitializeComponent();
InputTextBox.Tap += InputTextBoxTap;
InputTextBox.DoubleTap += InputTextBoxDoubleTap;
}
private void InputTextBoxDoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Double tapped!";
}
private void InputTextBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Tapped!";
}
I tested this on both the emulator and on a device and it works in both cases!
The reason is simple.
If you have notice the arguments supplied are different
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void textBox1_Click(object sender, EventArgs e)
{
}
Yes if you want them to resemble the same you can select "MouseClick" event from the properties.
Cheers!