I've got an odd and persistent problem... (Also my right shift key just stopped working, so please bear with my possibly odd caps & symbols; trying to get my fingers trained to use the left shift key is a pain.)
I'm running a CalcFields event when a ClientDataSet opens & goes through records (ie, AutoCalcFields is true). It takes every record and does a little modification to it, as in a field comes in with "88", it changes it to "$88.00" and puts it in a new field, and so on for formatting purposes. This works great, the only problem is that CalcFields seems to completely ignore the first record in the set. It shows the records in a DBGrid and the first record is there, but none of this formatting has been done to it. So I step through the code and CalcField never touches the first record. Well, actually, it DOES touch the first record, (I'm assuming). When I step through the code, CalcField fires off twice with two completely blank sets of information. I know CalcFields is executed when the dataset is open, and stepping through, that's where the first blank input comes from, which is fine. The second blank input comes in first when it's going through each record. Again, the first record shows up perfectly intact in the DBGrid, but completely blank on Calcfield.
Does anybody have any idea on why this is happening? This is driving me crazy and I've tracked it up and down and can't figure out what the heck is going on.
Here's the CalcField code, for what it's worth:
void __fastcall TDataModule1::sdsSEARCHCalcFields(TDataSet *DataSet)
{
// to view for debugging....
DataSet->FieldByName("MISBN")->AsString;
AnsiString formattedField;
String field;
double dInputPower;
dInputPower = DataSet->FieldByName("MBILL$")->AsFloat;
formattedField = FormatFloat("#,##0.00", dInputPower);
DataSet->FieldByName("BILL")->Text = formattedField;
dInputPower = DataSet->FieldByName("MTGUID")->AsFloat;
formattedField = FormatFloat("#,##0.00", dInputPower);
DataSet->FieldByName("GUID")->Text = formattedField;
field = DataSet->FieldByName("MISBN")->AsString;
int lght = field.Length();
String str = field.SubString(14, 1);
if (field.Length() > 13 && field.SubString(14, 1) == ".")
{
DataSet->FieldByName("ISBN")->Text = field.SetLength(13);
}
}
//---------------------------------------------------------------------------
and here's the code that executes it. note that it's a ctQuery...
DataModule1->sdsSEARCH->Active = false;
DataModule1->cdsSEARCH->Active = false;
DataModule1->dsSEARCH->Enabled = false;
DataModule1->sdsSEARCH->CommandType = ctQuery;
DataModule1->sdsSEARCH->CommandText = queryStr;
DataModule1->sdsSEARCH->Active = true;
DataModule1->cdsSEARCH->Active = true;
DataModule1->dsSEARCH->Enabled = true;
why not use the DisplayFormat property? for example, in in a TClientDataSet.AfterOpen event do something like this:
dynamic_cast<TNumericField*>(DataSet->FieldByName("quantity_to_date_adjustment"))->DisplayFormat = "#,##0.00;(-#,##0.00)";
Related
I have issues with Google app script IF condition.
Problem i am facing its not returning value TRUE rather going to next/ Else statements.
Code i am having:
const numberOfRowsToUpdate = deliveryDate.length;
// For each item making the for loop to work
for (i=0 ; i < numberOfRowsToUpdate;i++) {
debugger;
var dp = depositAmount[i];
if(dp!==""|| dp!==0 || dp !==null || dp!==isblank())
{ .... <statements>
}
}
I want to check whether particular cell of the array is empty / zero / returning null value.
thanks in advance for the help.
SUGGESTION
I have used a similar script I'm using for a spreadsheet in which I need to search through every row for some data, but obviously adpating it to your case, and since I don't have your full code (and still can't comment asking for more info due to my recent joining in SO), I had to simplify it, in hope it will work for you.
What I did was use your incrementing i index from the for loop and use it to scan every row, while adjusting it to fit your array index, because we can't have i = 0 as a row index, and it would skip the first value on the array if left as i = 1).
SCRIPT
function test(){
const n = 6;
var depositAmount = [7,2,0,2,0,8];
// For each item making the for loop to work
var ss = SpreadsheetApp.getActive();
Logger.log(ss.getName());
for (var i=1 ; i <= n ;i++) {
debugger;
ss.getRange("A"+i).setValue(1);
var dp = depositAmount[i-1];
Logger.log(dp)
if(dp != "" || dp != 0 /*|| dp != null || dp != isblank()*/)
{
ss.getRange("B"+i).setValue(dp);
}
else
{
ss.getRange("C"+i).setValue("VOID")
Logger.log(i-1+"th index of array is "+ss.getRange("C"+i).getValue());
}
}
};
RESULTS
After running it with the four original conditions you used, i didn't get the expected result, as you must have, leading to this:
.
While studying your original code, I stumbled upon this question about the differences between == and ===, as well as != and !==.
So before I used this in our favor, I tried the old trial and error method, using only one condition at a time, and then stacking them up. Not only I managed to find out the !== operator didn't work properly for this case, but also the comparison with null and the isblank() function (at least in my case, because i haven't defined it, and I'm not sure it is a built-in function) also don't work with either operator.
Therefore, using the != operator helps you better than the strict !==.
The result of the final script is that:
.
NOTES
I also tried using a null value within the array ([7,2,0,2,,8]), but it would always break away from the loop, never scanning the whole array, and I don't know how to circle that.
Here is the Execution Log for this script:
EDIT
While fooling around, I found this question and the answer by Etienne de Villers might be even faster to apply, or at least more useful for your purposes.
Using C++ Builder 2009
I have a custom control that inherits from TCustomControl, on which I paint several squares (TRect) with content etc.
I now wanted to show Hint as I hover over every square, but I'm not sure how to implement this best.
I attempted to simply use TCustomControl's ShowHint, and change Hint as I hover over the squares, but the problem is that the control doesn't show hint anymore after it first disappears, unless I leave the control and come back to it.
I hoped I could simply 'reset' it's state while hovering from one square to another but it doesn't work.
Assuming my approach is wrong to start with, kindly let me know what I should do to get the desired effect ?
if (State == rsHover && Item->FState != rsHover) // Not in the rsHover state yet, but going to
{
if (Item->Hint.Length())
{
if (ShowHint)
{
// Attempt to reset Hint's internal working, to no avail
Hint = L"" ;
ShowHint = false ;
}
Hint = Item->Hint ;
ShowHint = true ;
}
else
{
ShowHint = false ;
}
}
else if (State != rsHover)
{
ShowHint = false ;
}
The correct way to implement this feature is to make your component handle the CM_HINTSHOW message. The message's LParam value will be a pointer to a THintInfo record, whose fields you can freely modify as needed (in particular, HintStr and CursorRect).
To access the record, you can either
type-cast the LParam directly to THintInfo*.
type-cast the entire TMessage to TCMHintShow, and then access its HintInfo field.
By defining your own CursorRect rectangles, you can "[divide your control] into several hint regions", each with a different HintStr value. The CursorPos field indicates the mouse's current position within the control. When the mouse moves outside of the current CursorRect, the control will receive a new CM_HINTSHOW message, and you can update the CursorRect and HintStr fields as needed.
In advance, please forgive me if I do not give adequate background information for my question. Long time reader, first time asker.
I am making a program where one has a database of cars accessed through a tab delimited .txt file (we did something like this recently in my programming class, so I wanted to expand upon it).
Instead of using the terminal window, my format is displaying the Car objects (containing make, model, year, price, etc.) in ArrayList. I'm using JFrame, a JList, and a ListModel since I'm using an array of Car objects.
In my program, I wanted to create a delete method where the user could delete items from the database. Initially they would select the item from the JList and then would click on the delete button. This invokes the delete() method, which is the tab shown below...
void delete()
{
int i = list.getSelectedIndex();
String string = (String)listModel.getElementAt(i);
for(Car c : cars)
{
String year = String.valueOf(c.getYear());
String conditionReport = String.valueOf(c.getConditionReport());
String price = String.valueOf(c.getPrice());
if(c.getMake().indexOf(string) != -1 && c.getModel().indexOf(string) != -1 && year.indexOf(string) != -1 && conditionReport.indexOf(string) != -1 && price.indexOf(string) != -1 && c.getWarranty().indexOf(string) != -1 && c.getComments().indexOf(string) != -1)
{
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you would like to remove the " + cars.get(i).getYear() + " " + cars.get(i).getMake() + " " + cars.get(i).getModel() + "?", "Choose One", JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.NO_OPTION || choice == JOptionPane.CLOSED_OPTION)
{
return;
} else
{
cars.remove(c);
listModel.removeElementAt(i);
}
}
}
writeFile();
}
I have pinpointed my issue to be inside the if statement. (I printed things before and after to try to find where the program is lying. 'list' is my JList and 'listmodel' is my default list model. Car is an object I created that contains the elements (as seen by the get methods). The elements shown in the listModel are merely Strings that show getMake(), getModel(), and so forth... (Each 'get' item is separated by about 10 spaces.)
What am I doing wrong in the if statement? I figured that the getMake() and getModel() (and so forth) would be substrings of the index selected.
Thank you so much for your assistance! Any input regarding ways I could make further questions more specific and clear would be greatly appreciated!
It seems like you are doing this to find the selected Car in some kind of data structure. You would be better off doing something like programming a custom list model that had access to cars itself. Then you could retrieve the selection more immediately. If cars is an ArrayList that list merely parallels I also don't see why you can't do something to the effect of cars.remove(list.getSelectedIndex());. Or since JList can display any object, override Car's toString to display what the list currently displays and have the list display Cars. Then you can cars.remove((Car)list.getSelectedValue());.
But aside from that, based on your description it sounds like you mean to do the evaluation the other way. It's the list item that should contain all of the Car attributes, rather than all of the Car attributes containing the list item. So something like
if( string.contains(c.getMake()) && string.contains(year) // and so on
(Or with indexOf but since contains merely returns indexOf > -1, using contains makes your code somewhat shorter.)
I'd like to create wxGrid where user can edit some cells, but prohibit entering incorrect valued. For example, only strings of length 4 could be entered there. So, if user enter string of another length, I'd like to show a error message and return to cell editor. How to do it?
If I handle a cell change event EVT_GRID_CELL_CHANGE, for example
void Frame::OnGridCellChange(wxGridEvent& event)
{
int r = event.GetRow(); // get changed cell
int c = event.GetCol(); // get changed cell
if (Grid->GetCellValue(r, c).length() != 4)
{Error E (this);
/* Create the Error message */
E.ShowModal();
// The error message shown, uses clicks OK
// So, what to do here?
}
Grid->ShowCellEditControl(); is not a solution because cell change won't be generated if user edit nothing, but just click another cell - incorrect value would appear in Grid.
Handling EVT_GRID_EDITOR_HIDDEN seems not suitable since it appears before new value actually saved to cell.
You need to use your own specialization of the cell editor.
http://docs.wxwidgets.org/trunk/classwx_grid_cell_editor.html
Perhaps using an event of type wxEVT_GRID_CELL_CHANGING would work for you? If the string that is returned by calling GetString() on the event is not four characters long then you can veto the event, something like:
void Frame::OnGridCellChanging(wxGridEvent& event)
{
if (event.GetString().length() != 4)
{
//Veto the event so the change is not stored
event.Veto();
Error E (this);
E.ShowModal();
}
This does seem to require a wxWidgets 2.9.x release however.
I'm writing the code for a GUI (in C++), and right now I'm concerned with the organisation of text in lines. One of the problems I'm having is that the code is getting very long and confusing, and I'm starting to get into a n^2 scenario where for every option I add in for the texts presentation, the number of functions I have to write is the square of that. In trying to deal with this, A particular design choice has come up, and I don't know the better method, or the extent of the advantages or disadvantages between them:
I have two methods which are very similar in flow, i.e, iterate through the same objects, taking into account the same constraints, but ultimately perform different operations between this flow. For anyones interest, the methods render the text, and determine if any text overflows the line due to wrapping the text around other objects or simply the end of the line respectively.
These functions need to be copied and rewritten for left, right or centred text, which have different flow, so whatever design choice I make would be repeated three times.
Basically, I could continue what I have now, which is two separate methods to handle these different actions, or I could merge them into one function, which has if statements within it to determine whether or not to render the text or figure out if any text overflows.
Is there a generally accepted right way to going about this? Otherwise, what are the tradeoffs concerned, what are the signs that might indicate one way should be used over the other? Is there some other way of doing things I've missed?
I've edited through this a few times to try and make it more understandable, but if it isn't please ask me some questions so I can edit and explain. I can also post the source code of the two different methods, but they use a lot of functions and objects that would take too long to explain.
// EDIT: Source Code //
Function 1:
void GUITextLine::renderLeftShifted(const GUIRenderInfo& renderInfo) {
if(m_renderLines.empty())
return;
Uint iL = 0;
Array2t<float> renderCoords;
renderCoords.s_x = renderInfo.s_offset.s_x + m_renderLines[0].s_x;
renderCoords.s_y = renderInfo.s_offset.s_y + m_y;
float remainingPixelsInLine = m_renderLines[0].s_y;
for (Uint iTO= 0;iTO != m_text.size();++iTO)
{
if(m_text[iTO].s_pixelWidth <= remainingPixelsInLine)
{
string preview = m_text[iTO].s_string;
m_text[iTO].render(&renderCoords);
remainingPixelsInLine -= m_text[iTO].s_pixelWidth;
}
else
{
FSInternalGlyphData intData = m_text[iTO].stealFSFastFontInternalData();
float characterWidth = 0;
Uint iFirstCharacterOfRenderLine = 0;
for(Uint iC = 0;;++iC)
{
if(iC == m_text[iTO].s_string.size())
{
// wrap up
string renderPart = m_text[iTO].s_string;
renderPart.erase(iC, renderPart.size());
renderPart.erase(0, iFirstCharacterOfRenderLine);
m_text[iTO].s_font->renderString(renderPart.c_str(), intData,
&renderCoords);
break;
}
characterWidth += m_text[iTO].s_font->getWidthOfGlyph(intData,
m_text[iTO].s_string[iC]);
if(characterWidth > remainingPixelsInLine)
{
// Can't push in the last character
// No more space in this line
// First though, render what we already have:
string renderPart = m_text[iTO].s_string;
renderPart.erase(iC, renderPart.size());
renderPart.erase(0, iFirstCharacterOfRenderLine);
m_text[iTO].s_font->renderString(renderPart.c_str(), intData,
&renderCoords);
if(++iL != m_renderLines.size())
{
remainingPixelsInLine = m_renderLines[iL].s_y;
renderCoords.s_x = renderInfo.s_offset.s_x + m_renderLines[iL].s_x;
// Cool, so now try rendering this character again
--iC;
iFirstCharacterOfRenderLine = iC;
characterWidth = 0;
}
else
{
// Quit
break;
}
}
}
}
}
// Done! }
Function 2:
vector GUITextLine::recalculateWrappingContraints_LeftShift()
{
m_pixelsOfCharacters = 0;
float pixelsRemaining = m_renderLines[0].s_y;
Uint iRL = 0;
// Go through every text object, fiting them into render lines
for(Uint iTO = 0;iTO != m_text.size();++iTO)
{
// If an entire text object fits in a single line
if(pixelsRemaining >= m_text[iTO].s_pixelWidth)
{
pixelsRemaining -= m_text[iTO].s_pixelWidth;
m_pixelsOfCharacters += m_text[iTO].s_pixelWidth;
}
// Otherwise, character by character
else
{
// Get some data now we don't get it every function call
FSInternalGlyphData intData = m_text[iTO].stealFSFastFontInternalData();
for(Uint iC = 0; iC != m_text[iTO].s_string.size();++iC)
{
float characterWidth = m_text[iTO].s_font->getWidthOfGlyph(intData, '-');
if(characterWidth < pixelsRemaining)
{
pixelsRemaining -= characterWidth;
m_pixelsOfCharacters += characterWidth;
}
else // End of render line!
{
m_pixelsOfWrapperCharacters += pixelsRemaining; // we might track how much wrapping px we use
// If this is true, then we ran out of render lines before we ran out of text. Means we have some overflow to return
if(++iRL == m_renderLines.size())
{
return harvestOverflowFrom(iTO, iC);
}
else
{
pixelsRemaining = m_renderLines[iRL].s_y;
}
}
}
}
}
vector<GUIText> emptyOverflow;
return emptyOverflow; }
So basically, render() takes renderCoordinates as a parameter and gets from it the global position of where it needs to render from. calcWrappingConstraints figures out how much text in the object goes over the allocated space, and returns that text as a function.
m_renderLines is an std::vector of a two float structure, where .s_x = where rendering can start and .s_y = how large the space for rendering is - not, its essentially width of the 'renderLine', not where it ends.
m_text is an std::vector of GUIText objects, which contain a string of text, and some data, like style, colour, size ect. It also contains under s_font, a reference to a font object, which performs rendering, calculating the width of a glyph, ect.
Hopefully this clears things up.
There is no generally accepted way in this case.
However, common practice in any programming scenario is to remove duplicated code.
I think you're getting stuck on how to divide code by direction, when direction changes the outcome too much to make this division. In these cases, focus on the common portions of the three algorithms and divide them into tasks.
I did something similar when I duplicated WinForms flow layout control for MFC. I dealt with two types of objects: fixed positional (your pictures etc.) and auto positional (your words).
In the example you provided I can list out common portions of your example.
Write Line (direction)
bool TestPlaceWord (direction) // returns false if it cannot place word next to previous word
bool WrapPastObject (direction) // returns false if it runs out of line
bool WrapLine (direction) // returns false if it runs out of space for new line.
Each of these would be performed no matter what direction you are faced with.
Ultimately, the algorithm for each direction is just too different to simplify anymore than that.
How about an implementation of the Visitor Pattern? It sounds like it might be the kind of thing you are after.