I'm in a situation and I want to know that it is possible or not.
I want to change the Items of ListBox control depend on TextBox value.
Means when I change textbox value, the listitems of ListBox changes asynchronously (without pressing button or something).
Is it possible? If it is possible please guide me some references, tutorials or something.
Not completely getting what you need,but i hope the cases below would help you;
Case 1 : If you have a listbox with several items in it and you want the item to be selected that matches the text in textbox. If this is the case the code below should do the job;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength >= 1)
{
int index = listBox1.FindString(textBox1.Text);//Search any items that match Textbox's text.
listBox1.SelectedIndex = index;//Highlight the match
}
else
{
listBox1.SelectedIndex = 0;
}
}
Add the code above to the TextChangedEvent of your textbox and make sure you rename the controls in the code with the ones you have.If this is not the case,see the below one;
Case 2 : You have a textbox and you want to add the text of textbox to listbox.On more thing i would like to tell you,the code below assumes that when you press the Enter Key while the textbox is focused,it's text(if any) should be added to listbox.Add the code below in KeyDownEvent of your textbox,and make sure to rename the controls.If this is the case the code below would help you;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string item = textBox1.Text;
if (textBox1.Text.Length >= 1)
{
if (e.KeyCode == Keys.Enter)//If Enter key is pressed while textbox is focused.
{
listBox1.Items.Add(item);
}
}
}
Hope this helps you.
I hope the below code will help you:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(TextBox1.Text == "One")
listBox1.Items.Add("One");
if(TextBox1.Text == "two")
listBox1.Items.Add("two");
}
You don't have to do that async. Just use the TextBox.TextChanged-Event.
Related
I have a KeyPress event bound on multiple TextBoxs and I want to check which TextBox is getting clicked and do different things depending on the one clicked.
I'm trying to compare which TextBox is getting clicked based on the .Name attribute of the text box. I'm doing this in a switch statement, but am getting a Constant value is expected.
private void UpdateValues(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
switch (textBox.Name)
{
case txtBox1.Name: // Error here
break;
}
}
Is there a way to get around this? I don't want to hard code the .Name as a string in case future developers work on this.
Can I do this, or will it become a run time error?
private const string _TXTBOX1NAME = txtBox1.Name;
private void UpdateValues(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
switch (textBox.Name)
{
case _TXTBOX1NAME: // Use the const variable
break;
}
}
EDIT:
Actually, you cannot assign const values like that.
How would I compare which TextBox has a KeyPress without hard coding the .Name as a string in the case statement?
You can't use the switch like that. The cases need to be compile-time constants.
You could do:
private void UpdateValues(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
switch (textBox.Name)
{
case "NameTextBox":
break;
case "PasswordTextBox":
break;
}
}
If you know the names, this would be possible. Your example fails, because textbox1.Name is not a constant, but a property read from the instance of one TextBox.
Another way would be to use the textbox reference, given as the sender:
private void UpdateValues(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
if(textBox == textBox1) { ... }
if(textBox == textBox2) { ... }
}
But IMHO the best solution would be to use two change-callbacks, one for each method. Then you do not need to compare the textboxes or the textbox's names.
So you could change UpdateValues into one UpdateUserName and UpdatedPasswort. Doing this, the method name will clearly show, what the method does(, or at least should do), making your code a lot more readable.
try this
private void UpdateValues(object sender, KeyPressEventArgs e)
{
TextBox textBox = (TextBox)sender;
if (textBox.Name == textBox1.Name){
//error
} else if(textBox.Name == textBox2.Name){
// and so on
}
}
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.
In Windows Form controls like listview and treeview, when someone edit the label of an item an then press the "Escape" key, the edition end but the node remains with whatever i write in it. I want in exchange that when i press the Escape key the label return to what it was. I know that i must take the label before the label edit precisely in the "BeforeLabelEdit" event. In the "KeyPress" event handler i don't know how to stop the label edition. How can i do that?
Update
i found the method that i thought that doesn't exist, but now the problem is other. The Escape key press appears to be unchatchable in the middle of an edition label action.
private void ObjectWithItems_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (treeViewDocXml.SelectedNode != null)
{
treeViewDocXml.SelectedNode.EndEdit(true);
}
}
}
ok, I'am not sure what you talking about, but here is example how to cancel text box editing and set text before editing started:
string textBefore;
private void textBox1_Enter(object sender, EventArgs e)
{
textBefore = textBox1.Text;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Escape)
textBox1.Text = textBefore;
}
Hope it helps.
Is there a way to let only certain chars be added to a datagridview cell?
like '1234567890'?
There are two approaches I know of that you can use for this. The first (and I think the best) is to use the CellValidating event on the DataGridView and check if the entered text is numeric.
Here is an example of that which sets the row error value too (with an additional CellEndEdit event handler incase the user cancels out of editing).
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
string headerText =
dataGridView1.Columns[e.ColumnIndex].HeaderText;
// Abort validation if cell is not in the Age column.
if (!headerText.Equals("Age")) return;
int output;
// Confirm that the cell is an integer.
if (!int.TryParse(e.FormattedValue.ToString(), out output))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Age must be numeric";
e.Cancel = true;
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Clear the row error in case the user presses ESC.
dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}
The second approach is to use the EditingControlShowing event and the attach an event to the cell's KeyPress - I'm not such a fan of this approach since it silently blocks the input of non numeric keys - though I suppose you could give some feedback (like a bell sounding) it just feels like more work compared to the other way.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= TextboxNumeric_KeyPress;
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += TextboxNumeric_KeyPress;
}
}
private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
bool nonNumberEntered = true;
if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false;
}
if (nonNumberEntered)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
else
{
e.Handled = false;
}
}
One important note with this is be careful to remove the event handler on the control within the editing control showing method. This is important since the DataGridView reuses the same object for each cell of the same type, including across different columns. If you attach an event handler to a control in one textbox column, all the other text box cells in the grid will have the same handler! Also, multiple handlers will be attached, one for each time the control is shown.
The first solution came from this MSDN article. The second came from this blog.
If you would like the datagridview to simply remove the invalid chars for the user, instead of issue an error message, use DataGridView.CellParsing(). This event only fires after you make a cell edit, and allows you to override what was entered.
For example:
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
// If this is column 1
if (e.ColumnIndex == 1)
{
// Remove special chars from cell value
e.Value = RemoveSpecialCharacters(e.Value.ToString());
e.ParsingApplied = true;
}
}
For the RemoveSpecialCharacters() method, see this SO question for some excellent methods of removing special chars from a string.
I'm writing a simple application with several controls on a Windows form. I need to monitor the state of buttons (enabled/disabled) according to the state of a textbox and a listbox.
For example, when the listbox is empty, buttons Delete, Delete All and Edit are to be disabled, or when either the textbox or the listbox is empty button Forward is disabled, and so on.
So, I put the change of these properties on Application.Idle event, so it goes something like this:
private void MainForm_Load(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Application_Idle);
}
public void Application_Idle(object sender, EventArgs e)
{
CheckFillingFields(forwardBtn);
CheckFillingList(deleteBtn);
CheckFillingList(deleteAllBtn);
CheckFillingList(editBtn);
}
private void CheckFillingFields(object sender)
{
if (questionTxt.Text == "" || answersLst.Items.Count == 0)
(sender as Button).Enabled = false;
else
(sender as Button).Enabled = true;
}
private void CheckFillingList(object sender)
{
if (answersLst.Items.Count == 0)
(sender as Button).Enabled = false;
else
(sender as Button).Enabled = true;
}
So, the question is - is it acceptable to use Application.Idle in this case? Or should I make these properties dependable on user actions? (For example, when the user deletes an item from the listbox, I should check if it was the last one, and disable the corresponding buttons.)
Thanks a lot in advance, I really appreciate your help!
The simple answer is that, yes, the idle checking is bad and you should re-check the state of your controls on their change events, not "whenever possible".