C# combobox dropdown - c#

I Want my combobox to drop down when i press the textfield and the dropdown symbol
I have done this:
private void comboBoxOpretKomponentLevel_Enter(object sender, EventArgs e)
{
if (comboBoxOpretKomponentLevel.SelectedIndex <= 0)
{
comboBoxOpretKomponentLevel.Text = null;
}
comboBoxOpretKomponentLevel.Focus();
comboBoxOpretKomponentLevel.DroppedDown = true;
}
The .Droppeddown = true makes it work if text is selected ("Select Product")
But when the dropdown symbol of dropbox is pressed - the droppeddown goes false again.
How do I make this work?
And as far as I know I cant use DropDownList because i canĀ“t have my ("Select Product") Text.

I would simply use the MouseClickevent. Since you're question is too broad I only have this piece of code that may help you.
What it does is that only if you click the ComboBox or any controller regarding that ComboBox it is going to open the dropdownlist. To close it simply click on the Form_Load event and it will idle the dropdownlist
private void comboBoxOpretKomponentLevel_MouseClick(object sender, MouseEventArgs e)
{
//This piece will dropdown the combobox once you click it.
comboBoxOpretKomponentLevel.DroppedDown = true;
comboBoxOpretKomponentLevel.Focus();
}
private void YourForm_Click (object sender, EventArgs e)
{
//This piece will simply close the dropdown from your combobox and use the selected value.
comboBoxOpretKomponentLevel.DroppedDown = false;
}
Hope it helps, otherwise simple reformulate your question so we can help you.

Related

C# WPF Load ComboBox from txt file similar to Listbox

I have a listbox that I am populating from a text file with this code..
(Works)
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Listbox1.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
But when i try to populate a Combobox dropdown list the same way.. the list is empty.
(Doesnt work.. is blank combobox)
private void ComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
thanks!
You're setting the value for the ComboBox in the Selection_Changed event handler. The code in this block will only fire if someone changes the selected value in the combo box - which will never happen because there isn't anything in it - hence your code isn't being run. You should put that code in the constructor for the page, or an OnNavigatedTo() method.
public YourPageName()
{
this.InitializeComponent();
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}

Selected index changed fail into ListBox

I created a windows forms like this
As you can see in selected changed event I disable button move to, it works correctly, problem starts when I try to
return an item it to main list, move to button keeps disable instead enable it again when I select item of first list. Someone knows
what occurs there?
Events:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = false;
btnMoveTo.Enabled = true;
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = true;
btnMoveTo.Enabled = false;
}
You need to make sure that there actually is an item being selected since ListBox.SelectedIndexChanged event gets fired even when there're no items selected - making the new SelectedIndex equal to -1. Also, from the way you asking, I expect you want to enable btnMoveTo when there's a selected item in lstTechUnnotified and otherwise, disable it - and the same for btnReturnTo and lstTechToNotified; if that's it, then the easy way is:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnMoveTo.Enabled = (lstTechUnnotified.SelectedIndex > -1);
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = (lstTechToNotified.SelectedIndex > -1);
}
Though I'm not sure about your button names..

C# WinForms toggle button "enabled" property based on listView.selectedItems.count

I'd like to be able to have a button ONLY enabled when a certain listview has a selected item... such as listView1.SelectedItems.Count > 0
I can enable a button once a listViewItem is selected... but I can't figure out how to UNenable once the user clicks away from the listView.
Is there any "ListViewItem DeActivate" function? I've looked around but can't find anything.
You can do this in the SelectedIndexChanged event ...
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
use this code i am writing this code by supposing that in list at 0 index you have nothing to select or it has --select-- value
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
//this code will disable the button if it has any selection
button1.Enabled =false;
}
if(listView1.SelectedItems.Count==0)
{
//this code will enable the button if it has any selection
button1.Enabled =true;
}
}
Look into the Lostfocus event and then try something like this.
private void Lost_Focus_Ev(object sender, RoutedEventArgs e)
{
My_button.IsEnabled = false;
}
Everytime a user selects another control the button will be disabled. You can renable the button when the listview is re selected.

How can I manage overlapping DropDownBox drawers?

Most of my dropdown boxes use the SuggestAppend property, meaning when you start typing in the box, it will make a shortlist of the items that match your case. However, if I do this after opening the drawer, this happens:
I have tried using this method, but it closes both instead of just one:
private void cmbLoc_TextChanged(object sender, EventArgs e)
{
if (cmbLoc.Text != "")
{
cmbLoc.DroppedDown = false;
}
}
I am trying to have it so that when I type something into the text box, the original dropdown will disappear, and the SuggestAppend draw will appear. How can I manage this?
It worked if I used KeyDown. Try and tell if that helps
private void cmbLoc_KeyDown(object sender, KeyEventArgs e)
{
var comboBox = (ComboBox)sender;
comboBox.DroppedDown = false;
}

How to bind suggestions with text box with first and last name?

IDE: C#.net, Winforms, .net 4.0
I want to bind a text box with suggestions, suggestions will come from a list, that list is having space separated words for example 'Little Jhon' now with the help of following code I have implemented suggestion functionality, but I want when user type anything suggestions should come from both words, currently it is coming from first word only.
Code:
private void BindTournamentNames()
{
//On Load Code
List<String> lstNames= new List<string>();
lstNames.Add("Little John");
lstNames.Add("Hello Yogesh");
var source = new AutoCompleteStringCollection();
txtBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
source.AddRange(lstNames.ToArray());
txtBox1.AutoCompleteCustomSource = source;
txtBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
Now when I am typing in textBox 'Little' it is giving me suggestion, but when I am typing John it is not giving me suggestion, please tell me how to do this.
Well existing autoComplete functionality only supports searching by prefix. I have the same requirement in one of my project. So what i had done is -
Added a ListBox just below the TextBox and set its default visibility to false. Then use the OnTextChanged event of the TextBox and the SelectedIndexChanged event of the ListBox to display and select the items. like this -
Note: Assume your BindTournamentNames() method called in Form's constructor.
protected void textBox1_TextChanged(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
if (textBox1.Text.Length == 0)
{
listBox1.Visible = false;
return;
}
foreach (String s in textBox1.AutoCompleteCustomSource)
{
if (s.Contains(textBox1.Text))
{
listBox1.Items.Add(s);
listBox1.Visible = true;
}
}
}
protected void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
listBox1.Visible = false;
}
good luck...

Categories