Display dropdown list of comboBox when clicked/entered? - c#

How to display the dropdown list of a comboBox when I click/enter it?
private void comboBox1_Click(object sender, EventArgs e)
{
}
or
private void comboBox1_Enter(object sender, EventArgs e)
{
}

Since you have not mentioned if this is a Web/Windows/WPF so I am suggesting to do this
Winforms
((ComboBox)sender).DroppedDown = true;
WPF
((ComboBox)sender).IsDropDownOpen = true;

Related

RoutedEvents Combobox not Firing

Hi could some kind person help. I have two user controls. One with a textbox, the other with a Combobox. The Main window will perform calculation routine as soon as combos and textboxes are modified.
The Textbox version works, the ComboBox doesn't. The only difference I can see is
Textbox uses TextChangedEventArgs
whereas
Combobox uses System.EventArgs
Any ideas?
Thanks
// UserControl - with TextBox
public event RoutedEventHandler ucTextChanged;
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (ucTextChanged != null)
{
ucTextChanged(this, new RoutedEventArgs());
}
}
private void txtValue_TextChanged(object sender, TextChangedEventArgs e)
{
OnTextChanged(sender, e);
}
// UserControl - ComboBox
public event RoutedEventHandler ucComboChanged;
private void OnComboChanged(object sender, RoutedEventArgs e)
{
if (ucComboChanged != null)
{
ucComboChanged(this, new RoutedEventArgs());
}
}
private void ucCombo_DropDownClosed(object sender, System.EventArgs e)
{
OnComboChanged(sender, e);
}
Try looking at the event SelectionChanged of the ComboBox ( https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.combobox.onselectionchanged )
XAML:
<ComboBox SelectionChanged="ucCombo_SelectionChanged"></ComboBox>
C#:
private void ucCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// your code here...
OnComboChanged(sender, e);
}

How do I change this Windows Form code to WPF

I wrote a code in Windows form with a checkbox that enable an Update button when the user check. Now, I am required to change the code into WPF because of UI requirement. How can I convert this code below into a working format in WPF.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkBoxforupdate.Checked;
}
My attempted codes is below:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.Checked;
}
I would do it in the XAML rather than the code behind.
Set the Button's IsEnabled="{Binding ElementName=checkboxlincense, Path=IsChecked}"
I figured out how to do it. I just needed to Disable the Update button initially in the property section such as: IsEnabled="False".
Change
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
}
...to:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
checkboxlincense.IsChecked = true;
License_agreement();
}
private void License_agreement()
{
updatebutton.Enabled = checkboxlincense.IsChecked;
}
I will leave it to you to simplify but this closest matches your original code.

How to edit the contents of a list box, from a text box in another form

I am creating a library application with a listbox containing a list of books on the main form. I have created an edit form for the books. I want to be able to change the contents of the selected item in the listbox by changing the text in the textboxes of the edit form. Any suggestions how I could do this?
Main form:
private void lstBooks_SelectedIndexChanged(object sender, EventArgs e)
{
string currentBook = lstBooks.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
}
Edit form:
private void frmEditBook_Load(object sender, EventArgs e)
{
txtName.Text = listBoxBooks.SelectedItem.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text.Replace);
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
In your edit form, have a public property for your text
public string NewText
{
get
{
return txtName.Text;
}
}
And then when changing the item in the listbox, simply use the following procedure.
private void btnEdit_Click(object sender, EventArgs e)
{
//Your code from above
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
//My line
lstBooks.Items[lstBooks.SelectedIndex] = tempEditBook.NewText;
}
Well, it could be very simple if you call the new form with ShowDialog() instead of Show(). In your frmEditBook you should define the property
public string Txt {get; set;}
and set it at btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text);
Txt = txtName.Text;
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
and use the variable after the edit form is closed:
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.ShowDialog();
//tempEditBook.Txt here is your text
lstBooks.SelectedItem = tempEditBook.Txt;
frmkeepBookstore.Hide();
}

adding, deleting, sorting, searching, and counting inside a listbox

So in this listbox I have the names in this order
Abigal
Hannah
Tyler
Bill
Sasquach
The name of the listbox is called namesListBox.
Using TextBoxes and a different button for each input the user needs to input to:
add a name
delete a name
and then search a name to where only that name shows up in the list box at that time.
Once that is complete use a button to count how many items are in the list. Use a different Button to Sort.
This is c#. I can't seem to get nameslistbox.sort or .count to work so any help with that would be great and I have no idea how to do the add, delete, and search.
private void showListButton_Click(object sender, EventArgs e)
{
nameListBox.Visible = true;
}
private void countListButton_Click(object sender, EventArgs e)
{
}
private void addButton_Click(object sender, EventArgs e)
{
List<string> nameListBox = new List<string>();
nameListBox.Add(addTextBox.Text);
}
private void sortListButton_Click(object sender, EventArgs e)
{
}
private void searchButton_Click(object sender, EventArgs e)
{
}
private void deleteButton_Click(object sender, EventArgs e)
{
}
As what icedragon stated in his comment, you are creating a new list every time you click the add button so you need to make it a global variable.
You might want to read more about Linq to handle your collection. http://www.codeproject.com/Articles/19154/Understanding-LINQ-C
private List<string> namesList;
public class YourClass()
{
namesList = new List<string>();
}
private void addButton_Click(object sender, EventArgs e)
{
nameListBox.Add(addTextBox.Text);
}
private void sortListButton_Click(object sender, EventArgs e)
{
nameListBox.Sort();
}
private void searchButton_Click(object sender, EventArgs e)
{
string searchedString = nameListBox.FirstOrDefault(x => x.Contains(searchTextbox.Text);
}
private void deleteButton_Click(object sender, EventArgs e)
{
nameListBox.Remove(removeTextbox.Text);
}
You are creating the List instance in the click event method. This means, that you are creating a new List every time, when you click on the add button and the List is just inside your click method. You should make the List global:
private List<string> namesListBox; // this should be outside of the method
and initialize it in the constructor:
namesListBox = new List<string>();
now you can add elements like this:
private void addButton_Click(object sender, EventArgs e)
{
nameListBox.Add(addTextBox.Text);
} 

c# how to make a tabpage visibe to only some users based on roles

private void tabPage3_Click(object sender, EventArgs e)
{
((Control)this.tabPage3).Enabled = false;
}
private void tabPage4_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
((Control)this.tabPage4).Visible = false;
this.tabPage4.Hide();
}
}
private void tabPage1_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabPage1.Hide();
}
}
private void WelcomePage_Load(object sender, EventArgs e)
{
I've done this some time ago. The problem is that there is no property Visible and Enabled is doing not the things you would like to do.
So here is how i'm doing it:
// Put this over the constructor
private TabPage tabPage4ToShowForNotStudents = this.tabPage4;
private TabPage tabPage1ToShowForNotStudents = this.tabPage1;
Then you have to subscribe the Load-Method of your Form:
void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role != "student")
{
yourTabControl.TabPages.Add(this.tabPage4ToShowForNotStudents);
yourTabControl.TabPages.Add(this.tabPage1ToShowForNotStudents);
}
}
Now it will add the TabPage to your TabControl if the Role is not student. If it is it will not be added.
Be sure to not have them added in the designer otherwise it will not work.
Hope this is useful :)
thanks guys.i have sorted out the problem.
private void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabControl1.TabPages.Remove(tabPage5);
}
else
{
tabControl1.TabPages.Remove(tabPage4);
}
}

Categories