Colorcoding based on ComboBox item - c#

I'm using c# and Visual Studio 2010. I have a tablelayoutpanel with 8 rows and 8 columns. In one column the user will pick an item from a dropdown list in a combobox. Based on their choice, the row should assume a certain color. Any help would greatly be appreciated.

Add an event handler to your Combo Box.
public void MyComboBox_OnSelectedItemChanged(object sender, EventArgs e)
{
//change the color here
}
Make sure you add the event handler to the ComboBox properties.

Related

How to disable combox dropdown when user enters text into the combox

I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015
If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.

C# DatagridviewComboBox

In a Winform application, how can I manage this case :
I have a datagridview, with 2 columns, each contains a datagridviewcombobox
In the first I would list the constructor of some vehicles (BMW, FORD, etc)
In the second I would like that the content of the combobox is refreshed depending what I selected in the first combo, for example to display the model of the car
Thank you for any help
Best Regards
Austin
You can Handle the event handle SelectedIndexChanged so whenever the index is changed refresh the data example :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
your_refresh_method_here();//
}
}
Please note that e is the item selected by the user
For any additional data please refer to:
Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

Selecting text from ListView in c# windowsForms

I'm coding in C#, in Windows Forms. I'm using VS 2013.
I have a ListView in detailed mode and it shows my data correctly. I want to select it's items and copy the text of them or show some information about an item when I hover the mouse over it. But I can't do anything with Data which are shown in table. Only the first column can be selected and of course I can't copy those items too. The other columns cannot be selected.
What should I do?
I've searched table in toolbox. But there is only TableLayoutPanel and working with it is harder than ListView. Does VS 2013 have anything better?
One way to control mouse hover is by making a event for mouse hover
this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
private void panel1_MouseHover(object sender, System.EventArgs e)
{
// Update the mouse event label to indicate the MouseHover event occurred.
label1.Text = sender.GetType().ToString() + ": MouseHover";
// Instead of printing you can do whatever you want here, like you want to select some text or whatever
}
For more details, you can see this link https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
For complete row selection, you can use
listView1.FullRowSelect = true;

accessing dynamically created control variables from event handler c#

I have a program that I am trying to write that
one row of the comboboxes and text boxes are created dynamically when you click the plus button that is on the form
and then you can keep clicking the plus and it keeps adding a row but moves the new row down
I have the row organized like this:
there is a comboBox for buildings, then a comboBox for rooms. And that is one row
The building comboboxes have lists of buildings in them, and when I choose an item from the list, I need to populate the room combobox with the list of room numbers that correspond with the building.
I am having an extremely hard time doing this.
This is what Ive tried.
my row class is what I use to create the new rows.
When I press the button, a new row is created with new comboboxes etc.
Here is my event handler
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
row.roomComboBox.Items.Add(x);
}
}
Now my problem is I need to somehow add the corresponding room number data to the roomComboBox associated with the current row, and I dont know how and its driving me nuts.
The sender comboBox is associated with the current row, so is there a way to use the sender to reference the roomComboBox that is a member of the same row??
any help would be great. Ive searched lots of threads on here and cant seem to find an answer.
edit:
Is there a way to reference the object that a variable belongs to.
If I could somehow reference the row object of the sender comboBox then i might could use that to reference the room comboBox of the same row object...maybe? somehow? please?
When you create your comboboxes for each row, you can set the Building combobox's Tag property to the Room Numbers combobox. That will let you retrieve the associated combobox for that row. i.e...
// create combobox for row N...
ComboBox cmbBuilding = new ComboBox();
ComboBox cmbRooms = new ComboBox();
// store rooms combobox in building combobox
cmbBuilding.Tag = cmbRooms;
// ...
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
// get the room combobox
ComboBox cmbRooms = comboBox.Tag as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
cmbRooms.Items.Add(x);
}
}
Hope this little snippet will help
(sender as ComboBox).Parent.Controls.Find("cmbRooms",true)
This code will get the control with id "cmbRooms" with reference to the ComboBox sender
Use a DataRepeater Control for Windows Forms. It is included in the Visual Basic Power Packs but can be used in C# projects as well.
Another option is to use a DataGridView.

How to add list items for a combo box

I have tried to create items for a combo box but it is not working.Please help me out.
This is what i have so far;
private void cb_avg_weeks_month_SelectedIndexChanged(object sender, EventArgs e)
{
cb_avg_weeks_month.Items.Add('1');
cb_avg_weeks_month.Items.Add('2');
cb_avg_weeks_month.Items.Add('3');
}
Note:
cb_avg_weeks_month describes the name i have assigned to my combo box.
Well if you're only adding items to your combo box when the selected index has changed, then it'll never run because there aren't any items for the user to change the index on.
Populate your combo-box in your form's constructor after InitializeComponent();
if you work in Visual Studio you can add items to comboBox using comboBox's property.
if you want to do it using code, you can do it in constructor:
public Form1()
{
cb_avg_weeks_month.Items.Add('1');
cb_avg_weeks_month.Items.Add('2');
cb_avg_weeks_month.Items.Add('3');
}

Categories