Is there any way to prevent changing the selected item in a ComboBox only if for certain conditions? I want to allow update the selected item's displayValue in the ComboBox. But I don't want user to change the selected item when it's being updated. This is a windows application.
Inside your class:
private int _selectedIndex = 0;
Inside your form load method:
comboBox1.Enter += new EventHandler(comboBox1_Enter);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
Then the rest of the code:
protected void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (true) { // Add your validation or certain condition here.
(sender as ComboBox).SelectedIndex = _selectedIndex;
}
}
protected void comboBox1_Enter(object sender, EventArgs e) {
_selectedIndex = (sender as ComboBox).SelectedIndex;
}
Try setting set the Enabled property to false. (Or some third-party toolkits like Telerik have a ComboBox with a ReadOnly property.)
Related
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGrid dataGrid = (DataGrid)sender;
DataRowView row_selected = dataGrid.SelectedItem as DataRowView;
var s = row_selected["Nome"].ToString();
MessageBox.Show(s);
}
I'm trying to make a cell value message box when selected
I don't know what exactly you want to achive but I assume that your trying to pop a message box on double click on selected value on the grid view.
This is done by DataGridView Event.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
}
Then you can use the DataGridViewCell.Value Property to retrieve the value stored in a particular cell.
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
So the final code would look like this.
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
}
NOTE change the Gridview name as you datagrid name
I need to implement a ComboBox, which acts as follows:
When Click on the ComboBox, the client calling API method and updates the combobox items with the response.
My problem is, when I have 0 results - I want the ComboBox not to open (It has 0 items).
Is there a way to do that?
This is my current code:L
private void Combo_DropDown(object sender, EventArgs e)
{
// Private method which addes items to the combo, and returns false if no itmes were added
if (!AddItemsToComboBox())
{
// This is not working
Combo.DroppedDown = false;
}
}
You can make the DropDownHeight as small as possible (1). For example:
int iniHeight;
private void Form1_Load(object sender, EventArgs e)
{
iniHeight = Combo.DropDownHeight;
}
private void Combo_DropDown(object sender, EventArgs e)
{
Combo.DropDownHeight = (AddItemsToComboBox() ? iniHeight : 1);
}
I have two listboxes: listBox1, listBox2.
If i select the item in first listBox1, item of the same index must be automatically selected in listBox2.
So, If i select item 1 in listbox1 then, item 1 selected automatically in listbox2 and so on.
Not: I found some examples but not work.
private void listBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
{ listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex; }
Edit:
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Here's a sample that you may want to explore more, try to add ListBoxto your form (in this sample 3 listboxes) it should look like the following:
And here's the source that would select the same index whenever you click on an item on it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeListBoxes();
}
private void InitializeListBoxes()
{
//Populate listboxes
listBox1.Items.Add("Apple");
listBox1.Items.Add("Orange");
listBox1.Items.Add("Mango");
listBox2.Items.Add("Milk");
listBox2.Items.Add("Cheese");
listBox2.Items.Add("Butter");
listBox3.Items.Add("Coffee");
listBox3.Items.Add("Cream");
listBox3.Items.Add("Sugar");
//Subscribe to same events
listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged;
}
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
listBox1.SelectedIndex = listBox.SelectedIndex;
listBox2.SelectedIndex = listBox.SelectedIndex;
listBox3.SelectedIndex = listBox.SelectedIndex;
}
}
What happens is on the InitializeListBoxes you subscribe to the same event which would trigger the SelectedIndexChanged event, and select appropriate item from each of the ListBox.
to solve your problem, you can use a pattern called Observer: https://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx
Basically, you will have to create a notifier method in the listboxes that you want to notify. When you select an item in listBox1, you will call the notifier method of listBox2.
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Fastest and easiest way can be via MouseDown event:
private void lstBoxes_MouseDown(object sender, MouseEventArgs e)
{
ListBox lstBox = (ListBox)sender;
lstBx1.SelectedIndex = lstBox.SelectedIndex;
lstBx2.SelectedIndex = lstBox.SelectedIndex;
lstBx3.SelectedIndex = lstBox.SelectedIndex;
}
Is there any possibility to get a value of doubleclicked row in ListView?
I registered an event:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems.ToString());
}
But on message, when i doubleclick some row in listview i get:
System.Windows.Forms.ListView+SelectedListViewItemCollection
What is more, I have got 2 columns in listview:
lvLista.Columns.Add("ID");
lvLista.Columns.Add("Tilte");
And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?
If you handle the MouseDown and/or MouseDoubleClick events of the ListView control, and use the HitTest method to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.
The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.
If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.
This is a trivial example, and depending on your needs, you will have to mess with it a little.
UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
}
private void SetupListview()
{
ListView lv = this.listView1;
lv.View = View.List;
lv.Items.Add("John Lennon");
lv.Items.Add("Paul McCartney");
lv.Items.Add("George Harrison");
lv.Items.Add("Richard Starkey");
}
void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
MessageBox.Show("The selected Item Name is: " + item.Text);
}
else
{
this.listView1.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
void listView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
this.textBox1.Text = item.Text;
}
else
{
this.listView1.SelectedItems.Clear();
this.textBox1.Text = "No Item is Selected";
}
}
}
Try this:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}
I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:
// User must double-click to activate item
myListView.Activation = System.Windows.Forms.ItemActivation.Standard;
// Add event handler
myListView.ItemActivate += new
System.EventHandler(this.myListView_ItemClick);
Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.
The following line returns the data of the second column in the row that I've double clicked on as a string:
private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}
Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:
ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);
Try this
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
string x = hit.Item.Text;
}
I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true