Ok here is my question. I assign value to each checkboxes I created, and am trying to put them into listbox only when they are checked, after a button click. So here is the code that I have written so far, in which when the button is clicked, both the values are written into listbox, whether they are checked or not, how can I make it work as I explained?
public Form1()
{
InitializeComponent();
btnOne.Click += btnOne_Click;
chckOne.CheckedChanged += chckOne_CheckedChanged;
chckTwo.CheckStateChanged += chckTwo_CheckStateChanged;
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(number1 + number2);
}
string number1 = "ONE", number2 = "TWO";
void chckOne_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
if (chk.Checked == true)
{
lstOne.Items.Add(number1);
}
}
void chckTwo_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
if (chk.Checked == true)
{
lstOne.Items.Add(number2);
}
}
Just define one method:
void chkBox_CheckedChanged(object sender, EventArgs e)
{
var chkBox = sender as CheckBox;
if (chk.Checked == true)
{
lstOne.Items.Add(chkBox.Text);
}
else
{
lstOne.Items.Remove(chkBox.Text);
}
}
And attach it all your CheckBox CheckedChanged events:
chckOne.CheckedChanged += chkBox_CheckedChanged;
chckTwo.CheckStateChanged += chkBox_CheckedChanged;
Or if you want to add all checked values in your Button click change your method like this:
void btnOne_Click(object sender, EventArgs e)
{
this.Controls.OfType<CheckBox>()
.Where(c => c.Checked == true)
.Select(c => c.Text)
.ForEach(text => lstOne.Items.Add(text));
}
List<CheckBox> cbList=new List<CheckBox>();
public Form1()
{
InitializeComponent();
btnOne.Click += btnOne_Click;
cbList.Add(chckOne);
cbList.Add(chckTwo);
//All the checkbox should be added into cbList.
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Clear();
var checked_checkbox = cbList.Where(cb=>cb.Checked==true).ToList();
if(checked_checkbox.Count>0)
{
checked_checkbox.ForEach(x=>lstOne.Items.Add(x.Text));// Maybe you want put text of checkbox into listbox.
}
}
Related
I have a textbox and a datagridview with checkbox. The excel import into datagridview. I add the checkbox into datagridview. I want to input the number into textbox and this number will match the column in datagridview.
please help me!
private void TextBox2_TextChanged(object sender, EventArgs e)
{
}
private void TextBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
button4.Focus();
Button4_Click(sender,e);
textBox2.Focus();
}
}
private void Button4_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[3].Value.ToString() == textBox2.Text)
{
row.Cells[0].Value = ((CheckBox)dataGridView1.Controls.Find("DataGridViewCheckBoxCell", true)[0]).Checked;
}
}
}
private void Form9_Load(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.TextChanged += new EventHandler(TextBox2_TextChanged);
}
Set this property when setting up your DataGridView
dataGridView1.Columns[0].ValueType = typeof(CheckState);
And then in your Event Handler set the cell using this
if (row.Cells[3].Value.ToString() == textBox2.Text)
{
row.Cells[0].Value = CheckState.Checked;
}
i am making a windows form application in which i used a datagridview.
i want that when i write something in textbox in datagridview,than a messagebox appears containing the string i wrote..
ican't get my text in textchanged event..
all thing must be fired in textchanged event..
here is my code:-
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
//listBox1.Visible = true;
//string firstChar = "";
//this.listBox1.Items.Clear();
//if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
string str = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
if (str != "")
{
MessageBox.Show(str);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
var enteredText = (sender as TextBox).Text
...
}
Showing MessageBox in TextChanged will be very annoying.
Instead you could try it in DataGridView.CellValidated event which is fired after validation of the cell is completed.
Sample code:
dataGridView1.CellValidated += new DataGridViewCellEventHandler(dataGridView1_CellValidated);
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
I have set the EditMode property to EditorEnter then to on single click i cant access the dropdown.
On gridcellclick is wrote the code
If (e.ColumnIndex == 5)
{
SendKeys.SendWait("{F4}")
}
and also is wrote the code
private void comboBox1_Enter(object sender, System.EventArgs e)
{
comboBox1.DroppedDown = true;
}
But the result was same. I was not able to access the dropdown on single click. It takes 2 click to open dropdown.
UPDATE:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.Enter -= new EventHandler(combo_Enter);
combo.Enter += new EventHandler(combo_Enter);
}
}
Works fine for me:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.Enter -= comboBox_Enter;
comboBox.Enter += comboBox_Enter;
}
}
private void comboBox_Enter(object sender, EventArgs e)
{
((ComboBox)sender).DroppedDown = true;
}
I have a DataGridView. I want its 1st column or any desired column (which has textboxes in it) to be NUMERIC ONLY. I am currently using this code:
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
This code works but the problem is that all the textboxes in all the columns are getting numeric only.
I figured it out myself :)
Just removed the previous events in the starting of the function which resolved my issue.
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
You can add in a List all the numeric cells index.
Like this:
List<int> list_numeric_columns = new List<int>{ dataGridViewItems.Columns["itemID"].Index};
You add all the columns you need there.
Then, instead of doing this:
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
You do this:
if (list_numeric_columns.Contains(dataGridViewItems.CurrentCell.ColumnIndex))
That should work. You will have to add the columns just once..
Hope thats helps
This is how to use EditingControlShowing with TextBox → Keypress Event
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var itemID = e.Control as TextBox;
if (dataGridViewItems.CurrentCell.ColumnIndex == 1) //Where the ColumnIndex of your "itemID"
{
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
itemID.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
e.Handled = true;
}
Click the textbox that you want to be numeric only then in the properties click the "events" (looks like thunder) then look for the "keypress" then double click then type this:
if (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
How can I get a value returned from the checkbox_CheckChanged event please? Its a winforms app, and both the form and the checkbox are created programmatically. Thanks for all and any help.
The Controls event handlers are always "void" and you cant change the return type. Instead you can take an external variable and you change that value only in when the CheckedChanged Event occurs.
public bool checkedthecheckbox { get; set; }
CheckBox testchbox = new CheckBox();
private void Form1_Load(object sender, EventArgs e)
{
testchbox.CheckedChanged += new EventHandler(testchbox_CheckedChanged);
}
void testchbox_CheckedChanged(object sender, EventArgs e)
{
if (testchbox.Checked)
checkedthecheckbox = true;
else
checkedthecheckbox = false;
}
You can get the value from 'sender' object.
CheckBox chk = (CheckBox) sender;
bool result = chk.Checked;
You can get the state of the Checkbox by casting the sender object from the event arguments:
public void Method1()
{
CheckBox checkBox = new CheckBox();
checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
}
void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
bool resutlt = c.Checked;
}
Hope this helps!
You can use CheckState.Checked or CheckState.Unchecked, which is built in C#. Example:
for (int i = 0; i < lsbx_layers.Items.Count; i++) {
if (lsbx_layers.GetItemCheckState(i) == CheckState.Checked) {
//set boolean variable to true
} else if (lsbx_layers.GetItemCheckState(i) == CheckState.Unchecked) {
//set boolean variable to false
}
}
I've got an alternative to change the regular check box changed event into an event that provides you with the changed Checked value directly.
You could, for example, use it this way:
var myForm = new MyForm();
myForm.CheckBoxChanged += v =>
{
Console.WriteLine("The value of the checkbox changed to {0}", v);
};
Here's the class definition:
public class MyForm
{
public event Action<bool> CheckBoxChanged;
private CheckBox testchbox = new CheckBox();
private void Form1_Load(object sender, EventArgs e)
{
testchbox.CheckedChanged += (s, e) =>
{
var cbc = this.CheckBoxChanged;
if (cbc != null)
{
cbc(testchbox.Checked);
}
};
}
}
I hope this helps.