I have one form and inside form i have one toolstrip control, and i am adding ToolStripMenuItem dynamically.
What i want is when one is filled up, items should list in next row.
I tried this for increasing the height and width of form but adding items in new row not happening.
ToolStripItemCollection t_col = toolStripTaskBar.Items;
int _howMany = t_col.Count;
Rectangle mi_bounds = t_col[_howMany - 1].Bounds;
if (this.Width < (mi_bounds.X + mi_bounds.Width))
{
int minimumFormHeight = 80;
this.MinimumSize = new Size((mi_bounds.X + mi_bounds.Width), minimumFormHeight);
}
Let me know if you not understand what i want.
Any suggestion how can i achieve this.
Thank You.
You can use LayoutStyle property of ToolStrip. You need to set up it to Table and modify layout settings (specify rows and columns count).
You can do it like this:
this.toolStrip1.LayoutStyle = ToolStripLayoutStyle.Table;
var layoutSettings = (this.toolStrip1.LayoutSettings as TableLayoutSettings);
layoutSettings.ColumnCount = 3;
layoutSettings.RowCount = 3;
And the you can add new items to toolstrip:
var item = new ToolStripMenuItem(string.Format("item{0}", this.toolStrip1.Items.Count + 1));
this.toolStrip1.Items.Add(item);
Related
i have created a listview
ListView ListView1 = new ListView();
ListView1.Location = new System.Drawing.Point(12, 12);
ListView1.Name = "ListView1";
ListView1.Size = new System.Drawing.Size(280, 300);
ListView1.BackColor = System.Drawing.Color.White;
ListView1.ForeColor = System.Drawing.Color.Black;
ListView1.View = View.Details;
ListView1.GridLines = true;
ListView1.FullRowSelect = true;
ListView1.Columns.Add("ProductName", 100);
ListView1.Columns.Add("Quantity", 100);
ListView1.Columns.Add("Price", 100);
and i add items to it using the following code :
b.Click += (s, e) => {
string[] arr = new string[4];
ListViewItem itm;
arr[0] = b.Text;
arr[1] = x.ToString();
arr[2] = price;
itm = new ListViewItem(arr);
ListView1.Items.Add(itm);
x++;
};
b is an auto generated button, what i want to achieve is simple the variable x will increment with value 1 everytime i click on the button b, and x indicate the Quantity.
What i want:
when ever i click on the button b, the Quantity will change for the current item that has the Column["Productname"]=b.Text
What im getting:
the Quantity change but the item gets reinserted so i want to check if the item does exist first(based on Column["Productname"]) and if it does w the Quantity gets incremented by 1.
image_to_help_understand
More details: im sorry if this is getting too long, but im simply having a number of auto generated buttons and every button represents a product, when the user click on a product it gets added to the list ( to buy it later) and if the client clicks the same product n times, the Quantity should became Quantity=n without the item being added another time. thanks all and sorry for the long post.
Poking values into the UI is not a good design. I have these changes from my previous comments. Use a BindingList, public class Product : INotifyPropertyChanged, and replace the ListView with a DataGridView. This just makes life a whole lot easier and the code so simple. Creating ListBoxItems on your own is NOT a good way of doing things.
BTW: to find a product you just use a LINQ query on the ProductCollection. No searching the UI.
I'm trying to have a text from a List View Item in a single row without resize the column size, something like textbox property "Multiline"
In the picture below the column "Descripcion" is adding "..." to the product description, instead of that I want all the description in one row.
Something Like:
Descripcion Precio
-------------------|-------
All the description|
in one single row | 80
-------------------|-------
Another item in one|
single row | 70
-------------------|-------
I just tried with something like this:
ListViewItem row = new ListViewItem();
row.ListView.Height = 30;
but row.Height throw me an exception, any idea how to do this?
In order to set the height you can use SmallImageList, here is an example :
private void SetHeight(ListView listView, int height)
{
ImageList imgLst = new ImageList();
imgLst.ImageSize = new Size(1, height);
listView.SmallImageList = imgLst;
}
Also to wrap your Column, you could use ObjectListView, it's an open source C# wrapper around a .NET ListView
I know how to add/delete items but I don't know how to add more items to the same field (same row and same column). I want whenever I click a button, an item is added to same selected row but not to new row in the listView.
I uploaded a photo you can check to see what I exactly mean.
Consider looking at ObjectListView or DataGridView instead of what you are currently. It may be more flexible to your needs.
Your question is somewhat unclear. Clearly you are using listView and you have columns and rows resulting in a cell / box / grid location. I gather that, after its initial creation, you wish to append or alter the data at that location.
To get to the point: Multi-line text within a given 'cell' is not supported (as best I can tell). The picture you have shown is likely a custom object or something similar to a listView, but different (such as a ObjectListView). Or perhaps a picture.
listView2.Items[0].SubItems[4].Text = "123\nabc"; //Doesn't add a proper newline like a normal string
listView2.Items[0].SubItems[4].Text = "123\rabc"; //Doesn't add a proper return carriage like a normal string
listView2.Items[0].SubItems[4].Text = "123\r\nabc"; //Doesn't add a proper newline like a normal string
I am assuming you are using the details view
listView1.View = View.Details;
First adding your headers, listView1.Columns.Add(text, width);
listView1.Columns.Add(First Name", 50);
listView1.Columns.Add("Middle Name", 100);
listView1.Columns.Add("Last Name", 100);
You then add data to the listView. However, this is not done directly. You build a listViewITEM then add that item to the list view.
string[] row = new string[3];
row[0] = "john";
row[1] = "someone";
row[2] = "doe";
ListViewItem lvi = new ListViewItem(row);
listView1.Items.Add(item);
listView1.SelectedItems[#].SubItems[#].Text = "string" + "\n" + "string2";
CrazyPaste suggested adding a row, which could be practical and is something you often see with listViews.
However, If you choose to add or "redo" the rows, be sure to remove any old information before inputting new information to avoid duplicates.
Taken from the popup within visual studio 2013 pro
listView1.Items.RemoveAt(int index)
listView1.Items.Insert(int index, string key, string text, int imageIndex)
OR
listView1.Items.Clear(); //Clears all items
then
//Add populate logic here
Two arrays or a multidimensional array in a loop would be effective if you wish to populate the listview in that manner.
To achieve this programmatically, you could...
listView2 = new ListView();
listView2.View = View.Details;
listView2.Location = new Point(50, 50);
listView2.Size = new Size(400, 100);
this.Controls.Add(listView2);
listView2.Columns.Add("AAA");
listView2.Columns.Add("BBB");
listView2.Columns.Add("CCC");
listView2.Columns.Add("DDD");
listView2.Columns.Add("EEE");
ListViewItem item1 = new ListViewItem();
item1.Text = "0"; //The way to properly set the first piece of a data in a row is with .Text
item1.SubItems.Add("1"); //all other row items are then done with .SubItems
item1.SubItems.Add("2");
item1.SubItems.Add("3");
item1.SubItems.Add("");
item1.SubItems.Add("");
ListViewItem item2 = new ListViewItem();
item2.Text = "00";
item2.SubItems.Add("11");
item2.SubItems.Add("22");
item2.SubItems.Add("33");
item2.SubItems.Add("");
item2.SubItems.Add("");
ListViewItem item3 = new ListViewItem();
item3.Text = "000";
item3.SubItems.Add("111");
item3.SubItems.Add("222");
item3.SubItems.Add("333");
item3.SubItems.Add("");
item3.SubItems.Add("");
//item1.SubItems.Clear();
//item1.SubItems.RemoveAt(1);
listView2.Items.Add(item1);
listView2.Items.Add(item2);
listView2.Items.Add(item3);
//listView2.Items.Insert(2, item1); //0 here is the row. Increasing the number, changes which row you are writing data across
listView2.Items[0].SubItems[4].Text = "123\rabc";
To 'update' the information:
listView1.Items.Clear();
listView1.Items.Add(item1);
listView1.Items.Add(item2);
...etc
NOTES:
I was not able to get .Insert to work with subitems.
If you already inserted a listViewItem, You cannot insert an item
without first removing it
SubItems are not automatically created to fill empty space. Commands like 'listView2.Items[0].SubItems[4].Text' will not work with null/non-existent SubItems
I don't have much to go on. But this adds a new row:
string[] row = { "1", "snack", "2.50" };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
Here's a post discussing how to update an existing listitem:
C#: How do you edit items and subitems in a listview?
Ok. after I searched the internet for ages, it turned out that listView does not support text wrap. so instead I used DataGridView. thank you for your help
Im trying to create a dynamic grid with columns equal to the amount of days in a month (will add that feature later after i get the grid to appear) and rows equal to the amount of objects within the emplist list.
This is my code so far.
Grid dategrid = new Grid();
dategrid.Width = 400;
dategrid.HorizontalAlignment = HorizontalAlignment.Left;
dategrid.VerticalAlignment = VerticalAlignment.Top;
dategrid.ShowGridLines = true;
dategrid.Background = new SolidColorBrush(Colors.DimGray);
List<ColumnDefinition> columnlist = new List<ColumnDefinition>();
List<RowDefinition> rowlist = new List<RowDefinition>();
for (int i = 0; i < 31; i++)
{
columnlist.Add(new ColumnDefinition());
dategrid.ColumnDefinitions.Add(columnlist[i]);
}
for (int i = 0; i < Control.empList.Count; i++)
{
rowlist.Add(new RowDefinition());
dategrid.RowDefinitions.Add(rowlist[i]);
rowlist[i].Height = new GridLength(45);
}
The code compiles, but no grid appears on the form.
I feel like I'm missing something real basic here, but can't for the life of me figure it out.
You're missing dategrid.Bind()
Your code doesn't show you adding the grid to the page anywhere. So far all you have shown is instantiating a building the grid in memory. You need something like gridSpace.controls.add(datagrid) where gridspace is a container on the page. Something like <div id="gridspace" runat="server"></div> (any other container will do...). Or (if this is winforms) to a panel or other container on the form....
I am working on a Drag and Drop Editor that is creating items and adding them dynamically to a WPF Canvas. For each item, it is creating a dynamic grid and addign that to the canvas. I need layout state information about each one of these grids as it is added so that I know its coordinates on the canvas. The problem I am having is that when I try to access the Height/ActualHeight/RenderedSize information of each of these Grids that I add, it always turns out to be 0. I am assuming that I might need to render the items so that the new state information is registered, but I am not quite sure how to do this. I have seen support through the InvalidateXXXX() methods that are provided, but I am not sure if/or which one I should be using. there is also an UpdateLayout() function, but I am not sure if this is what I need.
Here is the code that I have running through a loop and adding Grids, which represent LineItems in the Canvas.
/*Initialize Grid Layout*/
Grid newGrid = new Grid();
newGrid.MinHeight = 50;
newGrid.Width = PreviewWindow.ActualWidth;
newGrid.Background = Brushes.Beige;
newGrid.ShowGridLines = true;
/*Define Column Definitions*/
List<ColumnDefinition> columns = new List<ColumnDefinition>(fieldItemList.Count);
foreach (ColumnDefinition column in columns)
{
ColumnDefinition labelColumn = new ColumnDefinition();
newGrid.ColumnDefinitions.Add(labelColumn);
newGrid.ColumnDefinitions.Add(column);
}
/*Define Row Definitions*/
RowDefinition row = new RowDefinition();
newGrid.RowDefinitions.Add(row);
int colCount = 0;
for (int i = 0; i < fieldItemList.Count; i++)
{
FieldItem fieldItem = fieldItemList[i];
/*Initialize Example Label*/
Label label = new Label();
label.Content = fieldItem.Label;
Grid.SetRow(label, 0);
Grid.SetColumn(label, colCount);
newGrid.Children.Add(label);
/*Initialize Example Text Box*/
TextBox textBox = new TextBox();
Grid.SetRow(textBox, 0);
Grid.SetColumn(textBox, colCount + 1);
newGrid.Children.Add(textBox);
colCount++;
}
stackPanel.Children.Add(newGrid);
//I need to Access the Height of each of the grids here somehow
lastTop += (int)newGrid.ActualHeight ;
newGrid.InvalidateMeasure();
//Or InvalidateVisual(), or do I perform this on something else?
}
There is reference to a StackPanel, all of these Grids are being added to a stack panel, and they will eventuall be arranged by the coordinates(which I currently cannot grab). The problem is right now it is adding all these items to a list that will need to be sorted by the Top coordinate, but since the Top coordinate is 0, it will not sort correctly.
A few notes:
The StackPanel does not provide size Layout suggestions to its children, so the Grid will need to supply it's own Layout size. In order for this to work, you'll need to set the Row and Column sizes to "Auto" so that they expand to accommodate their contents. Even then, I'm not entirely sure if this works (usually you combine Auto with "*" rows or columns, rather than having all Auto columns, having them expand to a fixed or stretched overall Grid size.) The layout I've proposed here will behave a bit oddly, as it will dynamically resize columns based on the contents of the column text boxes; for instance, if you have no text in any row, you'll get zero width text boxes with only margin and border providing any size.
Once the StackPanel has had its children added, you'll need to call UpdateLayout on the StackPanel. This will cause it to compute the layout information for each of its children and then position them appropriately. This can become expensive if you keep adding grids dynamically, so fair warning.