Disabling Combobox with Leave event is selecting text - c#

I'm using the Leave event on Combobox to disable it and load some data but, after leaving and disabling, it selects the whole text in Combobox. I want to unselect the text.
Im using:
cmbNome.DropDownStyle = ComboBoxStyle.DropDown;
cmbNome.AutoCompleteMode = AutoCompleteMode.Suggest;
cmbNome.AutoCompleteSource = AutoCompleteSource.CustomSource;
if it helps...
the code:
private void cmbNome_Leave(object sender, EventArgs e)
{
cmbNome.Enabled = false;
CarregarDados();
CarregarTelefones();
}

Try setting data source before disabling combo box:
string[] data = new string[] {
"Absecon","Abstracta","Abundantia","Academia","Acadiau","Acamas",
"Ackerman","Ackley","Ackworth","Acomita","Aconcagua","Acton","Acushnet",
"Acworth","Ada","Ada","Adair","Adairs","Adair","Adak","Adalberta","Adamkrafft",
"Adams"
};
private void comboBox2_Leave(object sender, EventArgs e)
{
comboBox2.DataSource = data;
comboBox2.Enabled = false;
comboBox2.SelectedIndex = -1;
}
This code fills the combo with data; however, no item is selected and text field is empty
EDIT: added sample data

Try to first deselect item and than set combobox disabled.
cmbNome.SelectedIndex = -1;
cmbNome.Enabled = false;

Related

How to show grid row value in textBox on SelectionChanged event C#?

I have to show the grid selected row value into textboxes.I'm using this code, but it's not working. Any help will be appreciated.
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.SelectedRows[0].Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.SelectedRows[1].Cells[1].Value.ToString();
txtBoxName.Text = CRUD.SelectedRows[2].Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.SelectedRows[3].Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.SelectedRows[4].Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.SelectedRows[5].Cells[5].Value.ToString();
}
You're indexing your selected rows. if you have less than 6 selected rows, then you will go out of bounds. You probably want to fetch data from only one row. Check if only one row is selected and then use index 0. Make sure you set CRUD.MultiSelect = false
Alternatively use CRUD.CurrentRow which will only ever get you one row.
Form.Designer.cs:
this.CRUD.SelectionChanged += new System.EventHandler(this.CRUD_SelectionChanged);
Form.cs:
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.CurrentRow.Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.CurrentRow.Cells[1].Value.ToString();
txtBoxName.Text = CRUD.CurrentRow.Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.CurrentRow.Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.CurrentRow.Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.CurrentRow.Cells[5].Value.ToString();
}

How to get selected value of datagridview combobox column?

I have a combox with list of image labels and a save button. On the save button I need to get the combox selected value on the save button if it is null then validate. How I can do this?
enter image description here
This is how I add combox in datagridview:
string[] ImageLabels = {Photograph, PassportPage1, PassportPage2,
PassportPage3, PassportPage4};
var list=new ArrayList();
var combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Image Labels";
combo.Name = "combo";
list = new ArrayList();
list.AddRange(ImageLabels);
combo.Items.AddRange(list.ToArray());
dgvFiles.Columns.Add(combo);
I am getting null values.
enter image description here
Add the below event for your datagridview
Here my datagridview name is dataGridView1
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string comboboxSelectedValue = string.Empty;
if (dataGridView1.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewComboBoxColumn))
comboboxSelectedValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
1) dataGridView1_CurrentCellDirtyStateChanged: This event will fire your dataGridView1_CellValueChanged immediately after you select value from your DataGridViewComboBoxColumn.
2) dataGridView1_CellValueChanged: This event will give you the value of your selected option in combobx, we additionally checked that is this the values comes from DataGridViewComboBoxColumn or not.
You may add above events on Form1_Load like
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
}
Output:

DataGridViewComboBoxCell ComboBox doesn't appear

Before setting datagridview with EditMode: EditProgrammatical the ComboBox appeared as it should.
After settings the EditMode: EditProgrammatical it requires 2-3 clicks on the arrow for the selection items to appear.
private void suggestButton_Click(object sender, EventArgs e)
{
var dict = getSuggestDict();
var dataGridViewComboBoxCell = new DataGridViewComboBoxCell
{
DataSource = dict.Keys.ToList();
};
dataGridView[selectedColumn, selectedRow] = dataGridViewComboBoxCell;
}
The function simplified a bit to avoid unnecessary complications.
You need to enable editing and set the focus to in question cell to make combo dropdown to open in single click.
private void suggestButton_Click(object sender, EventArgs e)
{
var dict = getSuggestDict();
var dataGridViewComboBoxCell = new DataGridViewComboBoxCell
{
DataSource = dict.Keys.ToList()
};
dataGridView[selectedColumn, selectedRow] = dataGridViewComboBoxCell;
dataGridView.CurrentCell = dataGridView.Rows[selectedRow].Cells[selectedColumn];
dataGridView.BeginEdit(false);
}
Edited: Moved setting current cell and enabling edit mode at the start of button click event handler to ensure its in edit mode when replacing combobox contents.
private void suggestButton_Click(object sender, EventArgs e)
{
dataGridView.CurrentCell = dataGridView.Rows[selectedRow].Cells[selectedColumn];
dataGridView.BeginEdit(true);
var dict = getSuggestDict();
var dataGridViewComboBoxCell = new DataGridViewComboBoxCell
{
DataSource = dict.Keys.ToList()
};
dataGridView[selectedColumn, selectedRow] = dataGridViewComboBoxCell;
}

Item in ListBox can have different value in textbox?

I have this ListBox that displays items I dragged from my DataGridView. Items in ListBox displays their MenuCode. What I want to happen is I want to show the MenuPrice of each item I have on ListBox on a TextBox. I tried to do this but the MenuCode displays on that TextBox. Please see the image below.
Is it possible to have the MenuPrice displayed on that TextBox? I have done this code but I dont think this is right.
private void menuDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) //this code triggers when I dragged an item from datagridview to listbox.
{
menuDataGrid.DoDragDrop(menuDataGrid.CurrentRow.Cells[3].Value.ToString(), DragDropEffects.Copy);
}
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
pricetxtbox.Text = menuListBox.SelectedItem.ToString();
}
private void ShowDataToGrid() //datagrid code
{
db_connection();
MySqlDataAdapter datagrid = new MySqlDataAdapter();
string selectAll = "SELECT * FROM Menu";
datagrid.SelectCommand = new MySqlCommand(selectAll, connect);
DataTable tbl = new DataTable();
datagrid.Fill(tbl);
BindingSource bs = new BindingSource();
bs.DataSource = tbl;
menuDataGrid.DataSource = bs;
menuDataGrid.Columns[6].Visible = false;
menuDataGrid.Columns[7].Visible = false;
}
I'm going to assume that the MenuPrice is also present on the GridView at a different cell, say Cells[4].
Then you can do something like this
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
foreach (DataGridViewRow row in menuDataGrid.Rows)
{
if (row.Cells[3].Value.ToString().Equals(menuListBox.SelectedItem.ToString()))
{
pricetxtbox.Text = row.Cells[4].Value.ToString();
break;
}
}
}
You should pack your data into an object class with properties you want to display and hold. For example:
public class MenuItem()
{
public float MenuPrice { get; set; }
public string MenuCode { get; set; }
}
If you store data of type MenuItem in your ListView, you will be able to do:
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) textbox
{
var item = menu.ListBoxItem.SelectedItem as MenuItem;
if(item == null) return;
pricetxtbox.Text = item.MenuPrice;
}
This would be one way to do it, if you want to enhance your code you should probably consider using databinding for you textboxes.

Pass the values from ListView control to text boxes in c#

private void deleteDisplayGamesButton_Click(object sender, EventArgs e)
{
//Game game = new Game(homeTeamComboBox.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamComboBox.Text, int.Parse(awayScoreUpDown.Value.ToString()));
deleteDisplayGamesListView.Items.Clear();
deleteDisplayGamesListView.View = View.Details;
foreach (Game currentgame in footballLeagueDatabase.games)
{
ListViewItem row = new ListViewItem();
row.SubItems.Add(currentgame.HomeTeam.ToString());
row.SubItems.Add(currentgame.HomeScore.ToString());
row.SubItems.Add(currentgame.AwayTeam.ToString());
row.SubItems.Add(currentgame.AwayScore.ToString());
deleteDisplayGamesListView.Items.Add(row);
}
}
I need to pass the values from above ListView control to following text boxes when I use the deleteDisplayGamesListView_SelectedIndexChanged method.
private void deleteDisplayGamesListView_SelectedIndexChanged(object sender, EventArgs e)
{
deleteModifyHomeTeamTxt.Text = "";
deleteModifyHomeScoreUpDown.Text = "";
deleteModifyAwayTeamTxt.Text = "";
deleteModifyAwayScoreUpDown.Text = "";
foreach (Game currentgame in footballLeagueDatabase.games);
{
?-----------------------------
}
}
Futhermore I need to clear the row after inserting to the textboxes,which i selected from the ListView Control.
If you know how to do this please let me know.
You should have access to your deleteDisplayGamesListView from within the SelectedIndexChanged event handler.
This should let you access the ListViewItems and their SubItems.

Categories

Resources