ComboBox Value shows the end portion after Selecting an Item - c#

I have a ComboBox in my C# Winform. Some of the Item texts are larger than the size of the ComboBox. Whenever I select these values, the end portion is visible. How can I ensure that, the beginning portion is shown.
For example,
Consider the items: {"small","big text selection"}
Now, the ComboBox is large enough to show 8 characters. When I select, "big text selection",
I can only see, "election", but I would like to view "big text" instead.

Is it significantly to you to use DropDownStyle equal to DropDown? In this style combobox have an editor, so when yoou select new value from the list it display in the editor and cursor position set at end of text. So in this case you should send HOME button code to the combobox editor this will move cursor at the start of line. You can do that as shown below:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SendKeys.Send("{HOME}");
}
But if DropDown style is not significant to you just change it to DropDownList and you will have desired behavour.

In the SelectedIndexChanged event create a Timer:
Timer timer = new Timer();
timer.Interval = 10;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
And in its Tick:
void timer_Tick(object sender, EventArgs e)
{
comboBox1.Select(0, 0);
(sender as Timer).Stop();
(sender as Timer).Dispose();
}
The Select call will achieve what you're after.

You can also look at expanding the value dynamically or use a tooltip for large items..
I explained it here for how to do it for Listbox:
http://blogs.msdn.com/b/sajoshi/archive/2010/06/15/asp-net-mvc-creating-a-single-select-list-box-and-showing-tooltip-for-lengthy-items.aspx

Related

How do I keep a textbox scrolled to the end even after I switch focus to another textbox?

I have two textboxes, textboxB copies from textboxA every time the text is changed but textboxA doesn't keep scrolling to the end. They're both one line textboxes that must have the cursor at the end 100% of the time. pls help.
private void Question_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
autoComplete.Text = Question.Text;
autoComplete.Focus();
autoComplete.Select(autoComplete.Text.Length, 0);
Question.Focus();
}
Try this:
TextboxA.CaretIndex = TextboxA.Text.Length;

ComboBox show dropdown menu on text selection

I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}

How to set a Timer based on a user choice?

I am working on a small program and, everything works fine. Instead of having a hard coded timer though, I'd like to change the timers interval from the form from a listbox or a numericupdownbox, combobox or something along those lines.
So instead of it being a hard coded 3000MS like it is I wanted to be able to change it on the form from a small menu with 1000-10000 milliseconds.
The thing is, I am not sure how to tell the timer to use an interval specified in a optional box.
Is it possible?
Thanks.
If you're going to use a control that allows for a non-numeric input such as a TextBox ensure you validate that the input is in fact a number. Better to trap errors in the first place than deal with exceptions later.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
int interval = 0;
bool success = Int32.TryParse(intervalTextBox.Text, out interval);
if(success)
{
operationTimer.Interval = interval;
}
}
You can omit the checking above if you're using a NumericUpDown control as it only allows a numeric value.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
operationTimer.Interval = (int) numericUpDown1.Value;
}
You can set the interval of a timer to a value you like in the change event your your combobox.
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
aTimer.Interval = double.Parse(ComboBox1.SelectedValue);
}
You should use double.TryParse if you can not ensure valid data for setting the interval. If the value is being taken from a combobox and its read-only then there is no need for TryParse.
You can try:
myTimer.Interval = (int) myTextbox.Text;
You can put this in textchanged event if you want it fired every time you change the textbox value.

Combobox cancel dropdown

I've got a combobox that opens a new form window with a datagridview, and I want the users to choose the items through that datagridview rather than through the combobox. I've got this code to achieve that:
private void comboBox1_DropDown(object sender, EventArgs e)
{
valSel.incBox = (ComboBox)sender;
valSel.Show();
if (this.comboBox1.DroppedDown)
{
MessageBox.Show("test");
SendMessage(this.comboBox1.Handle, CB_SHOWDROPDOWN, 0, 0);
}
}
As you see I'm also trying to hide the dropdown of the combobox but it isn't working. I assume it's because the combobox hasn't actually "dropped down" yet, so that part of the code is never run.
Is there an event or something I can cell when the combobox has fully "dropped down" so i can send the message to close it again?
You should be able to simply set the height of the ComboBox to something really small. Last time I looked at it, this determined the height of the popup part (the actual height of the control is determined by the UI/font size).
The more elegant way, however, would be using a custom control that just mimics the appearance of dropdown boxes (I'm rather sure that can be done some easy way).
In comboBox1.Enter set the focus to a different control if condition is met.
private void comboBox1_Enter(object sender, EventArgs e)
{
if (comboBox1.Items.Count < 1)
{
comboBox1.DroppedDown = false;
comboBox2.Focus();
MessageBox.Show("Select a list first");
comboBox2.DroppedDown = true;
}
}
1) create a KeyPress event on ComboBox from the properties.
2) write code
private void cmbClientId_KeyPress(object sender, KeyPressEventArgs e)
{
((ComboBox)sender).DroppedDown = false;
}

Dropped down in combo after focused

I have a combo box. It must display its content, when focused and its value changed as well.
I wrote this code in its Value Change event:
if(combo1.Focused)
combo1.DroppedDown=true;
But it doesn't work!
what's your solution?
What Event handler are you putting that code in? Assuming that you want to show the drop down when the user types in the edit box part of the combo just handle the TextChanged event and put that code inside there and it should work.
If I understand your requirement correctly, when the combobox gets focus you want the drop down list to show. That can be achieved as follows
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.GotFocus += new EventHandler(comboBox1_GotFocus);
}
void comboBox1_GotFocus(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}

Categories

Resources