VC++ listbox to listbox - winforms

I'm using VC++ and 2 listboxs, i need to transfer the items from the first listbox to the second.
i used this code but it only transfer the 1st item repeated as much as the first listbox contains items (the items count) :
for (int x = 0; x < Listbox1->Items->Count; x++)
{
Listbox1->SetSelected(x, true);
String^ curItem = Listbox1->SelectedItem->ToString();
listBox2->Items->Add(curItem);
std::string str = msclr::interop::marshal_as<std::string>(curItem);
cv::Mat img = cv::imread(str, 1);
cv::imshow("ShowImage", img);
cvWaitKey(5000);
}
I have to do an auto-selection of the items

Related

Keep tablelayoputpanel rows height identical as rows are added dynamically

I have a TableLayoutPanel whose rows are set to autoresize. It has two columns, but rows can be added dynamically at run time.
How do I add a row, and then have the height of each roenter code herew be the same percentage? I tried this code after adding the control:
private void BindRawData(int rc) // row number is passed in
{
var percent = 100 - (tableLayoutPanel1.RowCount * 10);
tableLayoutPanel2.Controls.Add(grid1, 1, rc);
for(int i = 0; i < tableLayoutPanel2.RowStyles.Count - 1; i++)
{
var panel = tableLayoutPanel2.RowStyles[i];
panel.SizeType = SizeType.AutoSize;
}
}
The result is a grid on top that is really tiny, and the next grid takes up almost the whole panel.

Show certain number of datapoints on chart at a time

I'm new to charting in winforms and I'm having a little trouble getting the chart to display a set number of points at time.
Using the code from this answer https://stackoverflow.com/a/5146430 :
private void FillChart()
{
int blockSize = 100;
// generates random data (i.e. 30 * blockSize random numbers)
Random rand = new Random();
var valuesArray = Enumerable.Range(0, blockSize * 30).Select(x => rand.Next(1, 10)).ToArray();
// clear the chart
chart1.Series.Clear();
// fill the chart
var series = chart1.Series.Add("My Series");
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.Int32;
for (int i = 0; i < valuesArray.Length; i++)
series.Points.AddXY(i, valuesArray[i]);
var chartArea = chart1.ChartAreas[series.ChartArea];
// set view range to [0,max]
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = valuesArray.Length;
// enable autoscroll
chartArea.CursorX.AutoScroll = true;
// let's zoom to [0,blockSize] (e.g. [0,100])
chartArea.AxisX.ScaleView.Zoomable = true;
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
int position = 0;
int size = blockSize;
chartArea.AxisX.ScaleView.Zoom(position, size);
// disable zoom-reset button (only scrollbar's arrows are available)
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to blockSize (e.g. 100)
chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;
}
What would I change here to only show, say three data points at a time? I tried changing the Maximum of the x-axis, which worked but took away the scrolling bar. I also tried messing around with the blockSize variable here.

Items and Subitems in List-View control

I'm want to use a List-View control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item is equivalent to a "row" (using LVS_REPORTstyle), and the Subitem is equivalent to a "column" (e.g. for each item I can display a number of subitems, each in a separate column on the same row).
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
int CreateColumns(HWND *hwndlistbox)
{
wchar_t *cnames[100];
LVCOLUMN lvc;
int i;
cnames[0] = L"column1";
cnames[1] = L"column2";
cnames[2] = L"column3";
cnames[3] = L"column4";
cnames[4] = NULL;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (i = 0; cnames[i] != NULL; i++)
{
lvc.iSubItem = i;
lvc.pszText = cnames[i];
lvc.cx = 100;
lvc.fmt = LVCFMT_LEFT;
ListView_InsertColumn(*hwndlistbox, i, &lvc);
}
return i;
}
void InsertItems(HWND *hwndlistbox, int *columncount)
{
LVITEM lvi;
wchar_t *items[100];
int i, j;
items[0] = L"text1";
items[1] = L"text2";
items[2] = L"text3";
items[3] = L"text4";
items[4] = NULL;
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
for (i = 0; i < *columncount; i++)
{
lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_InsertItem(*hwndlistbox, &lvi);
}
}
I expect this to generate a single row (lvi.iItem = 0;) with a text string under each column (lvi.iSubItem = i;). This is what it displays instead:
Changing lvi.iSubItem = i to lvi.iSubItem = 0 results in each text string being displayed as a new row in the first column:
I've played around with it, hardcoding the numbers on both iItem and iSubItem, changing both to i, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
First of all, your cnames and items arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];.
Secondly, you need to use ListView_InsertItem to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
lvi.pszText = items[0];
lvi.iSubItem = 0;
ListView_InsertItem(*hwndlistbox, &lvi);
for (i = 1; i < *columncount; i++)
{ lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_SetItem(*hwndlistbox, &lvi);
}
Each row shows a single item so you cannot populate the columns by adding items.
As the documentation says:
"You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems. The iSubItem member of the LVITEM structure must be zero. See LVM_SETITEM for information on setting subitems."
The LVM_SETITEM documentation explains how to set the text of a sub-item.

Creating a resizable grid in WPF

I need to write a C# WPF program in order to let the user individually modify the width and height of a grid using the mouse. After some reading, I've found out that WPF featues the GridSplitter control, which seems to be a possible solution for my problem. So far, this is my approach:
private const int NumCols = 5;
private const int NumRows = 7;
private void CreateDynamicWPFGrid()
{
// Create the Grid
var dynamicGrid = new Grid();
for (int i = 0; i < NumCols - 1; ++i )
{
// Define 2 * (NumCols - 1) columns. For every two columns, the first one will hold a label
// whereas the second one will hold a vertical splitter.
var gridColDefA = new ColumnDefinition();
// The gridColDefB is for the splitter.
var gridColDefB = new ColumnDefinition();
gridColDefB.Width = new GridLength(1, GridUnitType.Auto);
dynamicGrid.ColumnDefinitions.Add(gridColDefA);
dynamicGrid.ColumnDefinitions.Add(gridColDefB);
}
{
// The last column only needs a cell for holding a label. No splitter whatsoever.
var gridColDef = new ColumnDefinition();
dynamicGrid.ColumnDefinitions.Add(gridColDef);
}
for (int j = 0; j < NumRows - 1; ++j)
{
var gridRowDefA = new RowDefinition();
var gridRowDefB = new RowDefinition();
// The gridRowDefB is for the splitter.
gridRowDefB.Height = new GridLength(1, GridUnitType.Auto);
dynamicGrid.RowDefinitions.Add(gridRowDefA);
dynamicGrid.RowDefinitions.Add(gridRowDefB);
}
{
// The last row only needs a cell for holding a label. No splitter whatsoever.
var gridRowDef = new RowDefinition();
dynamicGrid.RowDefinitions.Add(gridRowDef);
}
for (int i = 0; i < NumCols - 1; ++i )
{
for(int j = 0; j < NumRows - 1; ++j )
{
// Insert the label.
var label = new Label();
label.Content = "C" + i + "-R" + j;
label.Background = new SolidColorBrush(Colors.Azure);
Grid.SetColumn(label, 2 * i);
Grid.SetRow(label, 2 * j);
dynamicGrid.Children.Add(label);
// Insert the horizontal splitter.
var horizontalGridSplitter = new GridSplitter();
horizontalGridSplitter.Height = 1;
horizontalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
horizontalGridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
horizontalGridSplitter.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(horizontalGridSplitter, 2 * i );
Grid.SetRow(horizontalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(horizontalGridSplitter, 1);
Grid.SetColumnSpan(horizontalGridSplitter, 1);
dynamicGrid.Children.Add(horizontalGridSplitter);
// Insert the vertical splitter.
var verticalGridSplitter = new GridSplitter();
verticalGridSplitter.Width = 1;
verticalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
verticalGridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
verticalGridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
Grid.SetColumn(verticalGridSplitter, 2 * i + 1);
Grid.SetRow(verticalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(verticalGridSplitter, 1);
Grid.SetColumnSpan(verticalGridSplitter, 1);
dynamicGrid.Children.Add(verticalGridSplitter);
}
}
// Display grid into a Window
Content = dynamicGrid;
}
The output I'm getting is as follows:
Notice that I'm only able to resize the rows (don't know why the vertical splitters don't show up) and, for some reason, when I grab a horizontal splitter it resizes the whole row and not just the individual cell. Any ideas? Please, See the following screenshot to see the resizing in action:
This is what I'd expect if I resize cell (0,0) (the image has being manually edited by me):
Thanks in advance!
If you remove the line setting the row for your verticalGridSplitter and set them to span NumRows, you will see your vertical splitters. But ultimately, I think you are trying to do something that the grid with splitters can't do. You can't resize the width and height of individual cells, only whole rows and columns.
After all, if you make C0-R0 taller, what do you expect the rest of that row to do?

Subclassed ListView column 0 displays corrupted text on Windows 7

Windows 7 (64 bit), .NET 4 (32 bit app)
I have a subclassed System.Windows.Forms.ListView comprising 2 columns displayed
in "Details" view. Only text fields are involved. The list comprises about 100
rows. On initial display all fields are drawn clearly but when scrolling or
"paging" with the scroll bar the text of some fields in the first column becomes
unreadable. It appears to be overwriten with random lines and blotches.
Is there anything that I can do to ensure that the text is clearly drawn in column 0?
The effect is diminished (clarity improved) when using remote desktop across a
LAN.
The effect disappears (all text is drawn clearly) when using remote desktop
across a WAN.
The ListView has always drawn perfectly on Windows XP.
For the purposes of a stripped down test, column header and DrawItem calls are ingored. The problem area is OnDrawSubItem which comprises the code
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
using (StringFormat sf = new StringFormat(StringFormatFlags.NoWrap))
{
Rectangle rect = new Rectangle(e.Bounds.Left
,e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString(e.SubItem.Text, this.Font, Brushes.Black
,rect, sf);
}
base.OnDrawSubItem(e);
}
The following is the complete code that reproduces the problem:
using System;
using System.Windows.Forms;
using System.Drawing;
///
/// Build by saving to a file called LVTrial.cs and then executing "csc LVTrial.cs"
/// Scroll list up and down on a windows 7 box and the first column of text is corrupted.
///
class MyListView : ListView
{
Random randomiser = new Random();
private const int NUM_ROWS = 100;
private const int NUM_COLS = 2;
public MyListView()
{
this.Location = new System.Drawing.Point(23, 25);
this.OwnerDraw = true;
this.Size = new System.Drawing.Size(625, 387);
this.TabIndex = 0;
this.UseCompatibleStateImageBehavior = false;
this.View = System.Windows.Forms.View.Details;
for (int ii = 0; ii < NUM_COLS; ii++)
{
this.Columns.Add((
(System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())));
}
for (int ii = 0; ii < NUM_ROWS; ii++)
{
this.Items.Add( new ListViewItem(
new string[NUM_COLS] { CreateRandomString(1, 6), CreateRandomString(1, 6) } ) );
}
}
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
using (StringFormat sf = new StringFormat(StringFormatFlags.NoWrap))
{
Rectangle rect = new Rectangle(e.Bounds.Left
,e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString(e.SubItem.Text, this.Font, Brushes.Black
,rect, sf);
}
base.OnDrawSubItem(e);
}
private string CreateRandomString(int minLength, int maxLength)
{
string str = string.Empty;
int length = randomiser.Next(minLength, maxLength + 1);
for (int ii = 0; ii < length; ii++)
{
int asciiChar = randomiser.Next(32, 126); // ascii range
str += Convert.ToChar(asciiChar);
}
return str;
}
}
class LVTrial : Form
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LVTrial());
}
private LVTrial()
{
MyListView myListView = new MyListView();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(674, 440);
this.Controls.Add(myListView);
this.Text = "LVTrial";
}
}
I have recorded this as a Windows 7 bug at https://connect.microsoft.com/VisualStudio/feedback/details/657909/subclassed-listview-column-0-displays-corrupted-text-on-windows-7
My own workaround for the problem was to insert an additional column at position 0. I now draw a rectangle with a transparent brush in column 0 for each item. All of which stops the text in the column to the right being drawn corrupt. I imagine drawing an image would achieve the same thing but I have not tried it. You can't hide this dummy column as the problem simply transfers to the second column (i.e. the first visible coolumn).
Another tip is that it seems to be performance related. The bug was more likely to occur with 100 items in the list view than 10,000.

Resources