Winforms - Stop Dropdown of Combobox during DropDown event - c#

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);
}

Related

C# Permanently Add and Save item to combobox

everybody!
I have collection of items in combobox's properties. And I want to add new item in my combobox by writing text in combobox and then use button event:
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}
But in next time of starting my programm - my added item doesn't exist in combobox. What do I wrong? I need all added items have been saved in my combobox for ever. May be problem in method InitializeComponents()? May be I have to add event before it?
Thank you very much.
ComboBox has no functionality to save and reload items.
You may store items into .NET Settings file on closing window and reload them on loading form:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Default.cboCollection != null)
this.cbx_unix_dir.Items.AddRange(Settings.Default.cboCollection.ToArray());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ArrayList arraylist = new ArrayList(this.cbx_unix_dir.Items);
Settings.Default.cboCollection = arraylist;
Settings.Default.Save();
}
//A button to add items to the ComboBox
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

First mouse click event on an item in a listbox doesn't trigger the function

I am trying to do something like: when a user select an item on a listbox, then the function listboxClicked will be triggered. However, the first click often does not able to trigger the function. It only triggers the function when I click the same item or another item for the second time.
May I know what's wrong with my code? Thank you.
My Code:
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
listbox.MouseClick += listboxClicked;
}
private void listboxClicked(object sender, EventArgs e)
{
if (listbox.SelectedIndex != -1)
{
//do something
}
}
Try this one:
Listbox1_SelectedValueChanged(object sender, EventArgs e)
{
Listbox listbox = (Listbox)sender;
MessageBox.Show(listbox.SelectedItem.ToString());
}

How to keep and add items in a List of objects during button click events

I want to add new items to my generic list when user clicks on a button, but each the the list contains only the last introduced item, it seems that during each button click list get reinitialized :(.
This is a part of code:
List<ProdusBon> listaProduseBon = new List<ProdusBon>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
I also tried using this code:
List<ProdusBon> listaProduseBon = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listaProduseBon = new List<ProdusBon>();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
but in this case a null reference exception was raised.
I must keep all the items in the list and not only the last one, and when click event was raised a new item to be added to the list.
All the controls in Default.aspx got the default values only the ListBox has "Enable AutoPostBack" set to true but i believe that this is not causing this behavior.
I do not how to keep the items in the list in these conditions, please give me a hand if you know how to do this.
Thanks !
Member variables are lost between page loads. You could store the variable in Session if you want it to remain the same value between loads.
List<ProdusBon> listaProduseBon
{
get { return (List<ProdusBon>) Session["ProdusBon"]; }
set { Session["ProdusBon"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (listaProduseBon == null) listaProduseBon = new List<ProdusBon>();
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
On your button click event first bind the list button and then add the new item from the textbox.
protected void Button1_Click(object sender, EventArgs e)
{
//code to bind your list goes here
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}

selected item change ComboBox

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.)

Categories

Resources