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.
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.
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.
Is there a method which initiates on focus change and can be overridden?
My goal is for the program to fetch closest data automatically from database to input fields whenever user changes his focus/presses enter or tab when on corresponding field. I'm still looking for a way to do this when user selects an item by mouse.
I'm aware that this could be implemented on mouse click but I refuse to believe that there is not a general method for focus change.
What about something like this:
foreach(Control ctrl in this.Controls)
{
ctrl.Enter += new EventHandler(Focus_Changed); // Your method to fire
}
Iterate through all controls and add a enter-event. Bind this handler to your method.
Edit:
Just in case you are wondering why "Enter" and not "LostFocus" or something like that: From my knowledge not every control got focus-events. As I've seen so far "Enter" is presented for all. Maybe there are exceptions. Should be checked out...
You could use Control.Enter event and Control.Leave event for that purpose.
See on MSDN Control.Enter and
Control.Leave.
textBox1.Enter += textBox1_Enter;
textBox1.Leave += textBox1_Leave;
private void textBox1_Enter(object sender, System.EventArgs e)
{
// the control got focus
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
// the control lost focus
}
I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}
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
}
}