Help required on GuiEdit

Hi,

I am trying to use GuiEdit to save data to static variables for each symbol. However I am not able to figure out where am I doing wrong in below code. I checked using TRACE and found that its storing data in static vars but as soon as I change symbol couple of times the data is no longer available! Not getting how the data is getting reset ! Any suggestion would be helpful.

PS: I already tried using the event functionality (such as using notifyEditChange ) but that's crashing my Amibroker GUI dashboard as it continues to refresh in an infinite loop. I read a similar topic in the forum ( https://forum.amibroker.com/t/guiedit-guibutton-puzzling-errors-when-clicked-several-times/17218/3 ) where it was hinted that it might be due to individual's machine configuration(BTW , My Amibroker version is 6.31.0 32 bit and using Windows 10 OS). So dropped that method of implementation as of now. Instead I am trying to using a GuiEdit/GuiButton Combo to save data in symbol specific staticvars and display the staticvar data on the GuiEdit Component whenever required.

My trimmed sample Code is as below -

_SECTION_BEGIN("Procedure");
	global id_LotSize; id_LotSize = 100;
	global id_SaveRefresh; id_SaveRefresh = 101;

	global SymbolChanged; 
	
	global Trachine_Svar_Name_LotSize; Trachine_Svar_Name_LotSize = "Stk_Svar_" + Name() + "_LotSize";
	global Trachine_Svar_Value_LotSize; Trachine_Svar_Value_LotSize = StaticVarGetText(Trachine_Svar_Name_LotSize);
	if(Trachine_Svar_Value_LotSize == "") {	StaticVarSetText(Trachine_Svar_Name_LotSize,"1",True); }


	function HasSymbolChanged()   
	{
		RC = False;
		CurrentSymbol = Name();
		PreviousSymbol = StaticVarGetText( "Svar_PreviousSymbol" );
		if(CurrentSymbol != PreviousSymbol)
		{
			RC = True;
			StaticVarSetText("Svar_PreviousSymbol", Name());
		}	
		return RC;
	}

	procedure CreateLabel(InputText, FontSize, TextColor, Column_X_Cor, Row_Y_Cor)
	{
		 GfxSetBkMode( 1 ); 
		 GfxSelectFont("Aerial", FontSize, 800 ,False,False);
		 GfxSetTextColor(TextColor);  
		 GfxTextOut(InputText, Column_X_Cor,  Row_Y_Cor);
	}
	procedure CreateTextBox(TbxId,TbxStaticVar,Column_X_Cor,Row_Y_Cor, TbxLength, TbxWidth, FontSize,SymChanged)
	{
		GuiSetFont("Aerial",FontSize);
		rc = GuiEdit( TbxId, Column_X_Cor, Row_Y_Cor, TbxLength, TbxWidth, 0);
		//GuiEdit( TbxId, Column_X_Cor, Row_Y_Cor, TbxLength, TbxWidth, notifyKillFocus); // Not Working .. Maybe due to System Config
		//GuiEdit( TbxId, Column_X_Cor, Row_Y_Cor, TbxLength, TbxWidth, notifyEditChange);// Not Working .. InfiniteRefresh.. Maybe due to System Config
		//SymChanged = HasSymbolChanged();

		if(rc == guiNew OR SymChanged == True) 
		{ 
			_TRACE("SymbolChanged = " + SymChanged + "StaticVar = " + TbxStaticVar);
			GuiSetText(StaticVarGetText(TbxStaticVar),TbxId); 
		}
	}

	procedure HandleEvents()  
	{
		for ( n = 0; id = GuiGetEvent( n, 0 ); n++ ) // get the id of the event
		{
			code = GuiGetEvent( n, 1 );
			switch ( id )
			{         	 
				case id_SaveRefresh:
				_TRACE("LSz = " + GuiGetValue(id_LotSize));
				StaticVarSetText(Trachine_Svar_Name_LotSize,GuiGetText(id_LotSize),True);
				break;	
			}
		}
	}

	SymbolChanged = HasSymbolChanged(); 
	CreateLabel("LotSize" ,10,colorWhite,10 , 50);  
	CreateTextBox(id_LotSize, Trachine_Svar_Name_LotSize, 110 , 50, 80, 30, 10,SymbolChanged);
	GuiButton("SAVE / REFRESH",id_SaveRefresh,10, 100, 200,45, notifyClicked); 
	
_SECTION_END();

Thanks,
Nandy

@amitabhanandy you forgot to invoke the HandleEvents() function.

Try to modify your code final lines as:

SymbolChanged = HasSymbolChanged();
HandleEvents();
CreateLabel( "LotSize" , 10, colorWhite, 10 , 50 );
CreateTextBox( id_LotSize, Trachine_Svar_Name_LotSize, 110 , 50, 80, 30, 10, SymbolChanged );
GuiButton( "SAVE / REFRESH", id_SaveRefresh, 10, 100, 200, 45, notifyClicked );

In general, I suggest you to use more _TRACE() instructions when debugging code. Adding one inside the HandleEvents() function immediately showed me that pressing the button did nothing since the event was not processed.

Moreover, there is no need to initialize the vars at the top of your formula as globals since they are "globals" by default in your code. Please, review this page of the guide.

Finally, while I welcome you to this community, I must also remember you to get your License Verified badge, or if you are still evaluating AmiBroker, post your next questions only in the specific Presale/Trial section of the forum.

1 Like

Hi @beppe,

Thanks for your input. Sorry.. Somehow the HandleEvents() function was missed while copy pasting my code. I have tested with that function. And having the same problem. Its getting stored and when I go to next symbol and came back again I can see the value on screen but if I repeat this couple of more times(switching between different symbols), the value is getting lost! Not sure how this is happening.. Your input will be helpful..

And as suggested, I have got the License Verified Batch .. Thanks for pointing that out.. I was not aware of it.

Thanks,
Amitabha

@amitabhanandy I briefly tried your snippet with the suggested modifications and, unfortunately, I do not see the reported issue (or maybe I do not understand the actual problem you are facing):

GuiEdit

Before grabbing this animated .gif, I saved some "lots sizes" (pressing the button for multiple stocks).
Then I moved with the mouse between the different symbols, and the GuiEdit seems to work as expected showing the value I saved.
In the middle of the clip's recording, I also changed the lot size for Baba to 22, and then it retained the modified value.
(Tested in AmiBroker 6.30.5 - 64 bits - Windows 7)

Do you still see the problem in the same (corrected) code you posted, or does it happens in your full formula?

2 Likes

@beppe That's Strange ! In My Machine its behaving totally different .... Attached the screen capture for the same code at my end .. Seems a dead end to me ... ! In attached image I tried to change the lotsize for 3 different symbols to 10,11,12 respectively and then when I go back to those symbols again its not showing up the values and getting reset as you can see ! What could be the possible issue ? ABScreenCapture|607x500

@amitabhanandy, no more ideas here...
Let's hope someone else is able to replicate it and provide you a solution.

How do I contact you @beppe