I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.
Related
I have implemented a trackbar that adjusts a textbox value accordingly with the slider as shown with the code below.
private void trackBar_Temp_Scroll(object sender, EventArgs e)
{
Oven_Temp.Text = trackBar_Temp.Value.ToString();
I am trying to have the value displayed in the oven_temp textbox appear within another textbox called feedback, when a button is clicked using this code.
private void Oven_select_Click(object sender, EventArgs e)
{
Oven_Temp.Text = Feeback.Text;
No errors seem to be appearing however no output appears within the feedback textbox.
Anyone able to help a student out for a coursework who has done a limited amount of coding before? :)
You set Oven_Temp.Text to the value of Feeback.Text. According to your text, you however wanted to set Feeback.Text to the value of Oven_Temp.Text. So you got the Assignment flipped.
Try Feeback.Text = Oven_Temp.Text;
The other common mistake is that the Event is not actually registered with the Button. You can put a simple Message Box in to test if the event even fires. That is usually one of the first things I check with Events.
My expectation: Is Wash and Is Return are ComboBox columns; when I click Is Wash, I intend to change Is Return to true.
This is my DataGridView:
I tried to use CellContentClick and CellClick
Below is my function:
private void dtGridViewLoan_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (dtGridViewLoan.SelectedRows[0].Cells["is_wash"].Value != DBNull.Value)
if (Convert.ToBoolean(dtGridViewLoan.Rows[e.RowIndex].Cells["is_wash"].Value))
{
dtGridViewLoan.Rows[e.RowIndex].Cells["is_return"].Value = true;
}
}
In the end, no matter how I change the ComboBox value, the data itself keeps returning false even it is true. Please help.
The problem with checking DataGridViewCheckboxCells in the CellClick is that they don't yet have the new values. This makes more sense for other cell types, which may have to go through more or less complicated validation routines more or less successfully, but we have to live with it.
As per MSDN you have two options:
Typically, check box cell values are intended either for storage, like
any other data, or for performing bulk operations. If you want to
respond immediately when users click a check box cell, you can handle
the DataGridView.CellClick event, but this event occurs before the
cell value is updated. If you need the new value at the time of the
click, one option is to calculate what the expected value will be
based on the current value. Another approach is to commit the change
immediately, and handle the DataGridView.CellValueChanged event to
respond to it. To commit the change when the cell is clicked, you must
handle the DataGridView.CurrentCellDirtyStateChanged event. In the
handler, if the current cell is a check box cell, call the
DataGridView.CommitEdit method and pass in the Commit value.
I find the 1st approach seemingly simpler but rather hacky.
Here is an example of the second, somewhat cleaner approach:
First we immediatly commit any checkbox clicks:
// add the event handler to the grid view
dtGridViewLoan.CurrentCellDirtyStateChanged +=
new EventHandler(dtGridViewLoan_CurrentCellDirtyStateChanged);
private void dtGridViewLoan_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dtGridViewLoan.CurrentCell is DataGridViewCheckBoxCell)
dtGridViewLoan.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
Now this code can be used to work with the new checkbox values:
private void dtGridViewLoan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// ..
if (dtGridViewLoan.Columns[e.ColumnIndex].Name == "is_wash")
{ your code here... }
// ..
}
great answer by taw, thanks for your help. Your answer is wrong because i already used the method that u asked me to use, but the result is still same.
In the end, i realize what wrong with it is because when i tick the check box, the column itself doesnt call the cell value changed method. because when i enable editing, when u tick the checkbox, the data grid view will enter 'edit mode' which the cell value changed method only be called when u exit the column ( please see the row header of the picture, the picture showed the pencil icon). I will try not to use enable editing for this case. thanks for help.
http://i.stack.imgur.com/Rj1v1.png
Hi all I have created a dynamic combo box with a Textbox and a button to appear as dropdown style, every thing works fine but I handled keyup event for the textbox so that when user enter some text I will search for the results and display them
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//Some code to filter my data
textBox1.Focus();
}
But I am unable to set the focus Immediately back to the textbox after results getting displayed so can some one help me
Code I used is from here
http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=8554&lngWid=10
I have found that the Focus() method is a bit flaky.
Other options:
textBox1.Select(textBox1.Text.Length - button1, 1);
...or simply:
textBox1.Select();
If you can verify that something else is going wrong, then this might be off base, otherwise you might simply be fighting weirdness.
As part of this assignment for my class, in which I have a listbox which is connected to a database and it displays the first and last name of the students. I have also created a search feature in which the user types in the first and/or last name they are looking for in a textbox and when they press the "search" button it displays the filtered results appear in the listbox.
The last part of the question asks me to detect when the user clears the textbox, to once again display the original data in the listbox. I have the data in a method called databaseload()and so it is really down to how to I get my program to detect that the listbox is once again empty.
I found a couple of things online, and when I tried them, it didn't work.
private void searchTextBox_KeyPress(object sender, KeyPressEventArgs e){
if (e.KeyChar == 8)
{
databaseload();
}
}
and I have also tried KeyDown
I also don't want it to reload when first backspace is detected. I want it to reload the listbox when the searchTextBox has nothing in it.
Your help is greatly appreciated.
You should handle the KeyUp event, which fires after the key has been entered into the text.
There is a TextChanged event which seems more appropriate than manually detecting backspaces. The delete key can remove all the text as well, or someone might just highlight all of the text and cut it to the Clipboard.
Example of using the TextChanged event:
private void textBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(((TextBox)sender).Text))
{
// reload database
}
}
I have a combobox on my form. It's in DropDown mode and it has autocomplete. When it is first shown, its text is "Choose part...". I would like it to reset its text to this after a selection is made. I've tried this (assuming the combobox is named comboBox1):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// [omitted]
comboBox1.Text = "Choose part...";
}
It only works when the selection is made using the keyboard (e.g. type a value and press [Enter] or start typing, use the arrows to select one of the autocorrect values, and press [Enter]). When the selection is made using the mouse, the text remains the value selected.
I've had problems with keyboard & mouse doing different things with comboboxes before, but that had to do with certain events not firing. I'm sure that this event is firing (the omitted code above runs regardless of the method used).
Has anyone seen this before? Any solutions?
Try using a delegate instead:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
// [omitted]
this.BeginInvoke((MethodInvoker)delegate { comboBox1.Text = "Choose part..."; });
}
And as Hans commented, this probably is not considered the best UI implementation with how users come to expect a ComboBox to work.
Another approach is to add an item at index 0:
ComboBox1.Items.Insert(0, new ListItem("Choose Part...", "Choose Part..."));
then reset the combobox to item 0 after handling the ComboBox1.SelectedListItemChanged() event
ComboBox1._SelectedIndexChanged = 0;
The user experience issue here is somewhat ambiguous: there may be times when you want to reset the list to a "known good state". For example, a user could be confused if the list remains on the previous selection. Resetting the list box does give the user some sense that whatever action they intended actually happened.