Daisy-chained comboBoxes not refreshing on clickback - c#

Noob here.
I have a series of comboBoxes (boxen?). The first box, called AcadLevel, determines whether the second box, called Plan, shows graduate or undergraduate plans. Each choice in comboBox Plan has a few different SubPlans, which are shown in the third ComboBox called SubPlan. Here's the code that displays subplans for MSCS.
private void Plan_SelectedIndexChanged(object sender, EventArgs e)
{
if ((AcadLevel.SelectedIndex == 0) & (Plan.SelectedIndex == 0)) // show subplans for grad, MSCS
{
SubPlan.Items.Clear();
SubPlan.Items.Add("Advanced Computing");
SubPlan.Items.Add("Software Engineering");
SubPlan.Items.Add("Database Engineering");
The behavior I don't like is that after a MSCS subplan is selected, if I go back and choose a new plan (like Master of Accountancy) the MSCS subplan STAYS there until a new subplan is selected. When the user clicks back to the Plan combobox, I want the SubPlan comboBox to go BLANK, in anticipation of a new plan being chosen. How can I do this? I tried a few things in desperation...
private void Plan_Click(object sender, EventArgs e)
{
SubPlan.Items.Clear();
SubPlan.Update();
SubPlan.Refresh();
The same thing applies to the AcadLevel comboBox. If the user clicks back to that, I want the Plan and SubPlan boxes to go blank.
Found it. I figured it would be best to put the title of the comboBox back instead of making it blank, so I:
private void Plan_SelectedValueChanged(object sender, EventArgs e)
{
SubPlan.Text = "3. Subplan";
}
private void AcadLevel_SelectedValueChanged(object sender, EventArgs e)
{
Plan.Text = "2. Plan";
SubPlan.Text = "3. Subplan";
}

Related

Double press of a button c#

I am struggling to make a button to be pressed multiple times with different outcomes. I have a few buttons to add some strings to a list. The problem is that I want to press the button to add an item and, if I press it again, to delete the same item.
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
}
}
You want some kind of toggle behavior, we have this "toggle" behavior when the next click "denies" the last one. If I had some more details on if you have a specific button for each item or if you're a selection a item in a list and then pressing the button, I'd be more precise.
If you're selecting an item and then clicking "remove", you can do something like this:
private void DeleteItem_Click(object sender, EventArgs e)
{
listBox1.SelectedItems.Remove(listBox1.SelectedItems);
}
If somehow you're using the same button to remove the last value you added, you can use a local variable to store the old value like in:
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
_oldValue = pineapple;
}
else
{
My_Pizza.Items.Remove(_oldValue);
}
}
If the same button will always and only add/remove the same item, use a toggle button instead.
Sometimes we developers try to solve simple things with harder approaches, when things get too complex, try to write down what you're trying to achieve and the possible solutions.
Update: if you have the pineapple object at the moment you're removing it, you don't need to store it as _oldValue. You can remove it directly inside your else statement.

C# combobox dropdown

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.

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

GroupBox visibility

I've working on an app that displays a restaurant menu and the user has to select which items he wants. I'm using group boxes instead of multiple forms for each of the different categories of food and drink. However some of the group boxes are not becoming visible even after I set their visibility to true. Any help on this issue please?
The main form has the following code:
private void drinksButton_Click(object sender, EventArgs e)
{
drinkGroupBox.Visible = true;
}
The designer has this code:
this.userGroupBox.Controls.Add(this.drinksButton);
Try this
private void drinksButton_Click(object sender, EventArgs e)
{
drinkGroupBox.Visible = true;
drinkGroupBox.BringToFront();
}
Would be interesting to see where it says
...Controls.Add(this.userGroupBox);

Do I need to trigger an event for the bind with Combox works properly?

I have an issue with a ComboBox
private void Form_AddGoal_Load(object sender, EventArgs e)
{
LoadGoal();
IList<Perspective> perspectives = PerspectiveRepository.All(); // I get all perspectives
CBPerspective.DataSource = perspectives;
CBPerspective.DisplayMember = "Name";
// Here I initialize other components
}
private void LoadGoal()
{
if (Goal== null)
Goal = new Goal();
// Here I bind other components
CBPerspective.DataBindings.Add("SelectedItem", Goal, "Perspective");
}
public void SaveBtn_Click(object sender, EventArgs e)
{
// I save the Goal
}
When the form is open, all is ok. If I don't choose any option in the combobox (That is, the first option keep it) and I save the data on the form the Perspective property is null but If I choose other option in the combobox and proceed in the same way, perspective is equal to the selected item so works perfectly.
What is going on? Do I need trigger an event?

Categories