Items and Subitems in List-View control - c

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.

Related

Google Apps Script .setvalues For loop not working

The goal of the loop is to fill each cell over 797 rows across 5 columns A, B, C, D and E with a formula whose cell reference increments by 1.
E.g. Column A rows 6 onwards will have formula "=indirect("'Data Repository'!A3++")"
Column B rows 6 onwards will have formula "=indirect("'Data Repository'!B3++")"
What happens when I run the function however is it only fills in column A. I've checked the execution transcript and execution succeeded is logged after the first column has been filled up. I've tried various variations to no avail.
Below is the last variation I've tested:
function indirect(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Fleet - Weekly V3");
var formulaArray = [];
var columns = ["A","B","C","D","E"];
var row = 2;
var text = '=indirect(\"\'Data Repository\'!';
var headerRow = 6;
var column;
for(i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(i = 0; i < 797; i++) {
row += 1;
if (formulaArray.length == 797) {
sheet.getRange(headerRow, column).offset(0, 0, formulaArray.length).setValues(formulaArray);
} else {
formulaArray.push([formula + row + '")']);
}
Logger.log(formulaArray.length);
}
Logger.log(i)
formulaArray = [];
}
}
Here is where you might be making an error - you need to create the variable i (var i = 0 instead of just i = 0) and if you're nesting loops, you need to have different variables increasing (first loop use i, then nest with j, then nest in that with k etc as needed)
for(var i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(var j = 0; j < 797; j++) {
Untested but I believe it should work if you just substitute that in.
Your problem is in your loops. You are using the 'i' variable twice. Change the for loop that you have nested to iterate over the variable 'j' or something other than 'i'.

VC++ listbox to listbox

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

Java get field values from DefaultTableModel and format into array

I am having trouble getting two of my fields from DefaultTableModel and format them into String[].
Here is how I get data from desired fields.
model = new DataTable().Table();
int rowcount = model.getRowCount();
int[] columns = {0,1};
String[][] name = new String[columns.length][rowcount];
for (i = 0; i < columns.length; i++)
{
for (row = 0; row < rowcount; row++)
{
int column = columns[i];
name[i][row] = model.getValueAt(row, column).toString();
}
}
name[0] is firstname, name[1] is lastname. Now, would you please guide me how to combine these two fields as firstname, lastname and put it into String[] addressee?
Any inputs will be much appreciated.
I am not sure if I understand your problem. Do you need just to combine two fields?
You could create addressee directly from table.
String[] addressee = new String [rowcount];
for (row = 0; row < rowcount; row++){
addressee [row]= model.getValueAt(row, 0).toString()+" "+model.getValueAt(row, 1).toString()
}

Calculating ObjectAtIndex items in tableview

I have an array which contains my product prices.They are number type on my pList. I can reach them with this code.
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"];
I can show these items with my cell view. But I want to summarise the items and set my Total label.text
For Example :
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"] = 55 in my plist value.
another :
[[mainList objectAtIndex:indexPath.row] objectForKey:#"Adi"] = 45 in my pList value.
I want to summarise items like first one + second one = my total.
55+45 = 100 $
It should summarise my first row item + my second row item.
How can I calculate those items ?
Thank you very much !
Edited...
I tried these codes ;
for (int i=0; i<[mainList count]; i++) {
int a=[[[mainList objectAtIndex:0] objectForKey:#"dAdi"] integerValue];
int b=[[[mainList objectAtIndex:i] objectForKey:#"dAdi"] integerValue];
int final = a + b;
self.lblToplamTutar.text=[NSString stringWithFormat:#"%i",final];
it doesnt work correctly.
Ok. I found the solution.
There is the method for summarise array objects.
-(void)toplamaYap{
int sum=0;
for(int i=0; i < [mainList count]; i++)
{
sum += [[[mainList objectAtIndex:i]objectForKey:#"dAdi"] intValue];
}
self.lblToplamTutar.text = [NSString stringWithFormat:#"%d", sum];
}
Thank you for replies. Especially Santi Bernaldo...
If you are getting these values from your plist, I assume you are receiving NSNumber values, type of object for getting numbers values allowed with Plist serialization.
So sending the message "integerValue" to your NSNumber objects:
int i=[[[mainList objectAtIndex:0] objectForKey:#"Adi"] integerValue];
int j=[[[mainList objectAtIndex:1] objectForKey:#"Adi"] integerValue];
int final = i + j;
self.totalLabel.text=[NSString stringWithFormat:#"%i",final];

jCombobox giving incremental value error on jTable

I am having problems with my code below, the code below shows a jComboBox being populated, when i select an item from this list it is added to the jTable below it.
There is alos code to check for duplicate entries ont he table. If a duplicate entry is found it should increase the qty column by one and not create a seperate entry.
This is where the problem comes in, when I press the back button on this screen and go to a different screen and then come back via same route as the first time, I get an incrementally different qty added to the table row/cell.
I have also included the code that populates the Round Details depending on Round Drop selected from table, for reference, but Im fairly certain the problem lies in the below code. The navigation is as follows...
To get to the below screen... Round Drop panel table of round drops) >> click on table row and taken to associated round details panel >> pressing the Till button takes user to screen with code below...
Test results:
First pass through below code using navigation above gives results as expected
Second pass gives an initial value of 2 (instead of one), and duplicate row increases qty by 2 instead of one
Third pass gives an initial value of 3 (instead of one), and duplicate row increases qty by 3 instead of one
Fourth pass gives an initial value of 4 (instead of one), and duplicate row increases qty by 4 instead of one
...and so on.
Any help, guidance on solution or a better design would be hugely appreciated.
Thanks
/*************Code sample ********************************/
public void tillOperations(String sourceCall) {
final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
if (main.tillPanel.cmbTillProdSelect.getItemCount() < 1) {
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
}}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
main.tillPanel.cmbTillProdSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent f)
{
int qty = 1;
for (int index = 0; index < 4; index++) {
addSelectedItem[index] = "";
}
int row;
selectedItem = null;
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();
for (int d = 0; d < roundStockObj.length; d++) {
if (selectedItem.equals(roundStockObj[d].getDescription())) {
addSelectedItem[0] = roundStockObj[d].getDescription();
addSelectedItem[1] = Integer.toString(qty);
addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
//break;
}
}
if(model.getRowCount() == 0) { //check if model is empty
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
}
else { //check if there is a duplicate row
int duplicateRow = -1;
for (row = 0 ; row < model.getRowCount(); row++) {
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getModel().getValueAt(row,0))) {
duplicateRow = row;
break;
}
}
if(duplicateRow == -1) { //if there is no duplicate row, append
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
}
else { //if there is a duplicate row, update
main.tillPanel.jLabel1.setText(addSelectedItem[1]);
DecimalFormat fmtObj = new DecimalFormat("####0.00");
int currentValue = Integer.parseInt(main.tillPanel.tblTillSale.getValueAt(row, 1).toString().trim());
int newValue = currentValue + 1;
Integer newValueInt = new Integer(newValue);
model.setValueAt(newValueInt, row, 1);
double unitPrice = Double.parseDouble(main.tillPanel.tblTillSale.getValueAt(row, 2).toString().trim());
double newPrice = newValue * unitPrice;
Double newPriceDbl = new Double(newPrice);
main.tillPanel.tblTillSale.setValueAt(fmtObj.format(newPriceDbl), row, 3);
}
}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
for (int index = 0; index < 4; index++) {
addSelectedItem[index] = "";
}
}
});
//This code loads the specific Round Details, based on the selection form the round drops table
public void displayRoundDropDetails() {
DefaultTableModel model = (DefaultTableModel)main.selectRoundDropPanel.tblSelectRoundDrop.getModel();
if (!loaded) {
for (int d = 0; d < roundDropsData.length; d++) {
if (roundDropsData[d][0].equals(defaultRoundID)) {
model.addRow(new Object[]{roundDropsData[d][3], roundDropsData[d][2],
roundDropsData[d][4], roundDropsData[d][5]});
}
}
loaded = true;
}
main.selectRoundDropPanel.tblSelectRoundDrop.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
int row = 0;
row = main.selectRoundDropPanel.tblSelectRoundDrop.getSelectedRow();
for (int index = 0; index < roundDropsData.length; index++) {
if (roundDropsData[index][3].equals(
main.selectRoundDropPanel.tblSelectRoundDrop.getModel().getValueAt(row, 0))) {
main.roundDetailsPanel.txtRoundDetailsAddress.setText(roundDropsData[index][6] + "\n"
+ roundDropsData[index][7] + ", " + roundDropsData[index][8] + "\n" +
roundDropsData[index][9]);
main.roundDetailsPanel.lblRoundDetailsName.setText(roundDropsData[index][2]);
main.roundDetailsPanel.txtRoundDetailsInstuct.setText(roundDropsData[index][10]);
main.roundDetailsPanel.txtDropDetailsIn.setText(roundDropsData[index][4]);
main.roundDetailsPanel.txtDropDetailsOut.setText(roundDropsData[index][5]);
main.roundDetailsPanel.txtRoundDetailsInstruct.setText(roundDropsData[index][12]);
break;
}
}
Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
showRoundDetailsPanel();
}
});
}
Try changing the listener for JComboBox. try using stateChangeListener.

Resources