CDHBookingEdge
12-05-2006, 04:18 PM
Here's the code, question(s) to follow after:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" frameRate="120" width="100%" height="100%" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.*;
import mx.controls.*;
private const SEARCH_GOOGLE:int = 0;
private const SEARCH_WIKIPEDIA:int = 1;
private const SEARCH_DICTIONARY:int = 2;
private var myMenu:Menu;
private var nSearchType:int = SEARCH_GOOGLE;
private function doSearch(strSearch:String) : void
{
if (strSearch == "")
{
Alert.show("The entry cannot be empty");
return;
}
var strUrl:String = "";
Alert.show("doSearch -> nSearchType = " + nSearchType);
switch (nSearchType)
{
case SEARCH_GOOGLE:
strUrl = getGoogleUrl(strSearch);
break;
case SEARCH_WIKIPEDIA:
strUrl = getWikipediaUrl(strSearch);
break;
case SEARCH_DICTIONARY:
Alert.show("I don't know how to search the dictionary yet");
break;
}
if (strUrl != "")
navigateToURL(new URLRequest(strUrl), '_blank');
}
private function getGoogleUrl(strSearch:String) : String
{
var strUrl : String = "http://www.google.com/search?hl=en&q=" + strSearch;
return strUrl;
}
private function getWikipediaUrl(strSearch:String) : String
{
var strUrl : String = "http://en.wikipedia.org/wiki/" + strSearch;
navigateToURL(new URLRequest(strUrl), '_blank');
return strUrl;
}
private function LookupInDictionary(strWord:String) : void
{
Alert.show("Gonna find this for ya in the dictionary " + strWord);
}
// Initialize the Menu control, and specify it as the pop up object
// of the PopUpButton control.
private function initSearchButtonMenu():void
{
myMenu = new Menu();
var dp:Object = [{label: "Google"}, {label: "Wikipedia"}, {label: "Dictionary"}];
myMenu.dataProvider = dp;
myMenu.selectedIndex = 0;
myMenu.addEventListener("itemClick", itemClickHandler);
searchButton.popUp = myMenu;
searchButton.label = "Search: " + myMenu.dataProvider[myMenu.selectedIndex].label;
}
// Define the event listener for the Menu control's itemClick event.
private function itemClickHandler(event:MenuEvent):void
{
var label:String = event.item.label;
searchPod.title = String("Search " + label);
searchButton.label = String("Search: " + label);
searchButton.close();
myMenu.selectedIndex = event.index;
Alert.show("myMenu.selectedIndex = " + myMenu.selectedIndex);
nSearchType = myMenu.selectedIndex;
Alert.show("nSearchType = " + nSearchType);
}
]]>
</mx:Script>
<mx:Panel id="searchPod">
<mx:VBox>
<mx:TextInput id="src" text=""/>
<mx:PopUpButton id="searchButton" label="Search" creationComplete="initSearchButtonMenu();" click="doSearch(src.text);" width="135" />
</mx:VBox>
</mx:Panel>
</mx:Application>
Ok now if you run this (I've made sure it runs in the Online Flex Compiler (http://try.flex.org/)) you'll see the alerts in itemClickHandler being shown twice once showing -1 values and once showing non-negative values. Then if you click on the button, you'll see it tell you the nSearchType is -1. Which, if you follow the alerts is not true it is a positive value.
Now my questions are why is itemClickHandler called twice, and why is nSearchType being noted as -1 on response to the click event?
Testing Steps:
1) Bring up program
2) Change the selection in the popupbutton
3) type in string to search.
4) Click on the popupbutton.
Thanks in advance,
Christopher
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" frameRate="120" width="100%" height="100%" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.*;
import mx.controls.*;
private const SEARCH_GOOGLE:int = 0;
private const SEARCH_WIKIPEDIA:int = 1;
private const SEARCH_DICTIONARY:int = 2;
private var myMenu:Menu;
private var nSearchType:int = SEARCH_GOOGLE;
private function doSearch(strSearch:String) : void
{
if (strSearch == "")
{
Alert.show("The entry cannot be empty");
return;
}
var strUrl:String = "";
Alert.show("doSearch -> nSearchType = " + nSearchType);
switch (nSearchType)
{
case SEARCH_GOOGLE:
strUrl = getGoogleUrl(strSearch);
break;
case SEARCH_WIKIPEDIA:
strUrl = getWikipediaUrl(strSearch);
break;
case SEARCH_DICTIONARY:
Alert.show("I don't know how to search the dictionary yet");
break;
}
if (strUrl != "")
navigateToURL(new URLRequest(strUrl), '_blank');
}
private function getGoogleUrl(strSearch:String) : String
{
var strUrl : String = "http://www.google.com/search?hl=en&q=" + strSearch;
return strUrl;
}
private function getWikipediaUrl(strSearch:String) : String
{
var strUrl : String = "http://en.wikipedia.org/wiki/" + strSearch;
navigateToURL(new URLRequest(strUrl), '_blank');
return strUrl;
}
private function LookupInDictionary(strWord:String) : void
{
Alert.show("Gonna find this for ya in the dictionary " + strWord);
}
// Initialize the Menu control, and specify it as the pop up object
// of the PopUpButton control.
private function initSearchButtonMenu():void
{
myMenu = new Menu();
var dp:Object = [{label: "Google"}, {label: "Wikipedia"}, {label: "Dictionary"}];
myMenu.dataProvider = dp;
myMenu.selectedIndex = 0;
myMenu.addEventListener("itemClick", itemClickHandler);
searchButton.popUp = myMenu;
searchButton.label = "Search: " + myMenu.dataProvider[myMenu.selectedIndex].label;
}
// Define the event listener for the Menu control's itemClick event.
private function itemClickHandler(event:MenuEvent):void
{
var label:String = event.item.label;
searchPod.title = String("Search " + label);
searchButton.label = String("Search: " + label);
searchButton.close();
myMenu.selectedIndex = event.index;
Alert.show("myMenu.selectedIndex = " + myMenu.selectedIndex);
nSearchType = myMenu.selectedIndex;
Alert.show("nSearchType = " + nSearchType);
}
]]>
</mx:Script>
<mx:Panel id="searchPod">
<mx:VBox>
<mx:TextInput id="src" text=""/>
<mx:PopUpButton id="searchButton" label="Search" creationComplete="initSearchButtonMenu();" click="doSearch(src.text);" width="135" />
</mx:VBox>
</mx:Panel>
</mx:Application>
Ok now if you run this (I've made sure it runs in the Online Flex Compiler (http://try.flex.org/)) you'll see the alerts in itemClickHandler being shown twice once showing -1 values and once showing non-negative values. Then if you click on the button, you'll see it tell you the nSearchType is -1. Which, if you follow the alerts is not true it is a positive value.
Now my questions are why is itemClickHandler called twice, and why is nSearchType being noted as -1 on response to the click event?
Testing Steps:
1) Bring up program
2) Change the selection in the popupbutton
3) type in string to search.
4) Click on the popupbutton.
Thanks in advance,
Christopher