Change ToolStripDropDownButton Text by Selecting Item - c#

I have been stuck on this problem for awhile now, and I was hoping for some direction or an answer. How do you change the text of a ToolStripDropDownButton based off of a selected item within itself?
I have a grid that is to be displayed, but I want to change the size of the grid on the fly. By selecting the ToolStripDropDownButton and selecting a predetermined list of grid sizes, the text of the ToolStripDropDownButton should change to show the user the new grid size.
Any answer is appreciated.

I have found the answer to my question, thank you all for the answers.
private void changeGridSizeDropDownButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem != null)
{
changeGridSizeDropDownButton.Text = e.ClickedItem.Text;
// This line converts my list gridsize options like 64, 32, and 16
// and Parses that text into the new selected gridsize.
gridSize = Int32.Parse(e.ClickedItem.Text);
}
}

It's easy you'll see.
myToolStripDropDownButton.DropDownItems[0].Text = "New grid size";
The index[0] is the first item in your DropBox just change it to access other items.
This is to change the name of the items.
If you need to change Text somewhere on the ToolStrip independently from your DropDownButton check what item is selected than change
myToolStrip.Items.Add("DefautlGridSize");
myToolStrip.Items[0].Text("NewGridSize");
If you don't need the ToolStrip especially I would consider using a ComboBox with a Label which would do the job much more simply.
Hope this helped

Related

Dropdown(list) ComboBox control field options C#

I use a ComboBox DropDown to show a number of items that can be selected. I also use a default text "Select item". It works perfectly apart from the fact that once I picked an item from the list, I can select the text and remove it (using the backspace) so that the area for the selected item is blank (see image).
I would like to prevent this or, in case this is not possible, to have the default text shown again. The latter doesn't work so far, so I tried the DropDownList but then the background of the entire list becomes grey. If I then select OwnerDrawFixed or OwnerDrawVariable in DrawMode, the background turns white again but also the font color becomes white (instead of black).
I found that I have to use DrawItem to change the layout but even that doesn't work. I simply want the same style as I indicated for DropDown. Does anyone know how to do this or where to find a similar question that has already been answered?
How about something like this?
You could also try setting the DropDownStyle to DropDownList and FlatStyle to Flat. This gives you a slightly different result.
private void InitialiseCombo(ComboBox combo)
{
combo.Items.Clear();
combo.Items.AddRange(new object[] { 3391200121, 3391200122 });
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.Text = "Select Item";
combo.KeyPress += combo_KeyPress;
}
void combo_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
The suggested solution did not do the trick but I found a workaround to the initial problem, which is the manual deletion of the ComboBox Text, so I'll just put it here in case someone else has the same problem. When using
MyComboBox_TextChanged(object sender, EventArgs e)
I can set a restriction myself: if the text is blank, it should show the default text. If the typed text is equal to one of the item in the ComboBox, it should set the selectedItem equal to the typed text.
I also included GotFocus and Leave EventHandlers.

Selecting entire row in DataGridView programmatically (without using ...Rows[].Selected)

I've been trying to create Mp3 list using DataGridView to show info. I want to add data at runtime and I have Columns like Artist, Song Name, Rating, Path... but I want to be able to select entire Row when selecting a Cell. I've used this code for that:
private void DataViewGrid1_MouseClick(object sender, MouseEventArgs e)
{
int rindex = DataViewGrid1.CurrentCell.RowIndex;
DataViewGrid1.Rows[rindex].Selected = true;
}
and it works but the problem is that it's to sloooow! When I click on the cell whole row is selected but it's visually terrible. I can see cell selected and after a delay whole row is selected but the delay is just too long. Is there a faster (or better) way to do this? Or maybe there is a better control that can do this? I also want to be able to present rows in a different font (for example: change font color for the same artist in the list).
I'm open for all suggestions.
Thanks!
DataViewGrid1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
The entire row will be selected by clicking its row's header or a cell contained in that row.
If you want to color based on having a value in the cell, something like this will do the trick.

How to show tool tip when cursor is on ListView cell?

I have listview object, and several columns in it. One of the columns does not fit all text info, and the text has been cutted. I need to implement something like this: when user move mouse cursor to this column's cell, all the text is shown. Is it possible to do that ?
Now i have:
protected ListViewItem GetItem(ListView listView, Point mousePosition)
{
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
private void myListView_MouseMove(object sender, MouseEventArgs e)
{
ListViewItem item = GetItem(myListView, Cursor.Position);
// or should I use e.Location instead of Cursor.Position?
item.ToolTipText = "my info"//Now I need to show "my info" on the cell
//that user move cursor on
}
One of the old ways to show decent tooltip with lists is to hide a label and make it visible at the desired location, with the desired text for the desired duration. Benefit of this could be multi-line, multi-color tip.
So it works like this:
the label sits on the listview or form and it is invisible.
on mouse move you get item data using mouse X and Y. Position,
size and show your label filled with data.
label itself can have a timer, which will start when label
is visible and make it invisible in 5 seconds, for example.
I remember doing just that this year. Although my "tooltip" was done over the listbox.
Replace label with a textbox and suddenly you can make your data in the cell editable :o)
Try this code
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);
Do this by setting the ShowItemToolTips property of ListView to true

Add text box with corresponding combo box dynamically

I am creating a windows form app with the following goal:
get a list of products, each with a unique name and a category (from an enumerated list) from a user (and then do some things after, but this is not relevant to the question).
The idea is I would like to have the user specify they would like to configure "n" products by entering a value in a text box. I have the event handler for the text box calling a method which sets a variable to this value n. This value, "n", will be used as the loop counter, or what have you - the point is it will create the bound for the number of boxes to create.
I would then like to add (dynamically based on n), n number of (text box / combo box) pairs to the form. If there is no room to add another (text box / combo box) pair below the last one created, it should create another column.
n is unbounded, but, realistically, will likely never exceed 20. In any event, I'd like to be able to handle it if there are more products than this.
the options in the combo box will be filled from a string list that is passed in at run time, but will be consistent per box, per instance of this Form application.
i tried to enter a mock up image but stack overflow won't let me until i have earned some reputation points :(
I understand how to create a number of boxes using something like the code below, but its the finer points i'm stuck on. Can anyone help?
thanks!
` private void Method1()
{
int boxes = Int32.Parse(NumProducts.Text);
for (int i = 0; i < boxes; i++)
{
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(40, i * 20);
tb.Name = "TextBoxName" + i.ToString();
tb.Size = new System.Drawing.Size(184, 20);
tb.TabIndex = i + 2;
tb.Text = String.Empty;
panel1.Controls.Add(tb);
}
}
private void NumProducts_TextChanged(object sender, EventArgs e)
{
Method1();
}`
Sounds to me like a DataGridView would be the better choice here. You can configure it with a DataGridViewTextBoxColumn as the first column and a DataGridViewComboBoxColumn for the second. It supports a "new row" as the last item.
Read the docs. Drop one on a form and play with it.
Asking the user for the number of rows in advance is not very good from a usability viewpoint.
You should rather create an interface that keeps creating new boxes as the user inputs things, either by having a "new row" row that activates when the user types something into it (the empty row isn't saved) or by having a "new row" button.
To achieve the layout, use a FlowLayoutPanel control, and add the controls to this instead of to the panel as you are already doing. That should transparently take care of the columns issue, and add scroll bars if the user goes beyond your anticipated maximum number of edit boxes. General info on the FlowLayoutPanel here (as well as many others).

How to change the cell appearence of the filter in UltraWinGrid

I am using UltraWinGrid control and I want to customize its filtering.
I am able to get all the values from the ValueList property .
private void dgridData_BeforeRowFilterDropDown(object sender, BeforeRowFilterDropDownEventArgs e)
{
// Get each item from the list
foreach (ValueListItem item in e.ValueList.ValueListItems)
{
// Do Something
}
}
I want to show the values in the dropdown ( see picture ) as follows :-
(All)
(Custom)
(Blanks)
(NonBlanks)
*********
*********
*********
The values after (NonBlanks) should appear as asterisks .
One of the option I can think is to attach a masked editor to the current editor , to change the display. But I dont know how to attach an editor control in this scenario.
Sharing some links on I was going through :-
Filtering Rows in Ultragrid
Removing default entries from Infragistics UltraWinGrid RowFilterDropDown
You are welcom to let me know of other options to acheive the same.
I'm not in front of my pc with Infragistics on it right now. But Couldn't you just loop through the items and change the DisplayText?
private void dgridData_BeforeRowFilterDropDown(object sender, BeforeRowFilterDropDownEventArgs e)
{
// Get each item from the list
foreach (ValueListItem item in e.ValueList.ValueListItems)
{
if (!item.DisplayText.StartsWith("("))
item.DisplayText = new String('*', item.DisplayText.Length);
}
}
I'm just typing the code from the top of my head, pardon any errors.
What should the filtering do after you have changed the list to be strings of asterisk characters? Are you looking to provide a filter based on the length of the items?

Categories