Is there any way to convert e.Key to char in Textbox keyDown event in Windows 8 store app?
Here is my code:
private void OnTextBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
EcgStatementText.Text = (e.Key); // We should convert e.Key to chars
}
This will do the trick,
var foo = ((Char)e.Key).ToString();
string result = ((Char)e.Key).ToString();
If you want to get Numberpad values take a look at this question: How do I convert a "Keys" enum value to an "int" character in C#? :
Related
the Invoke() function on a MethodInfo object accepts parameters as an object[]. I want to be able to send a JSON encoded string instead. Is there any way to do this?
The code which i am basing mine of comes from this MSDN page
....
object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);
//args in this case is an object[]. any way to pass a string?
return mi.Invoke(wsvcClass, args);
I am aware that Newtonsoft provides a way to deserialize strings but can it do so into an object[]? or is there another way to do this?
The method signature you are looking at takes an Object[] representing all parameters in your method. For example:
public void DoStuff(string x, string y, int b);
Could be called like this:
methodInfo.Invoke(wscvClass, new object[] { "x", "y string", 500 });
So in your case you should be able to call Invoke using:
string jsonEncodedString = "{ }"; // whatever you need to do to get this value
mi.Invoke(wsvcClass, new object[] { jsonEncodedString });
MethodInfo MSDN Link
I have TextBox with MaxLength set to 10 but it is accepting 11 characters when Enter key is pressed. Looks like it is counting \n\r as 1 character instead of two. Is there anyway to make it count \n\r as two char length?
If you really want to allow line breaks in your text box and limit its text length, I see two options:
Either bind MaxLength through a converter so that it changes its value according to how many line breaks (\r\n) the text contains, as shown in this question
Alternatively, you might define your own attached property MaxLength that calculates text length correctly. This might look somewhat like the following (just as an example you'll need to adapt that to take into account special cases etc.):
public class TextBoxExtensions: DependencyObject
{
public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.RegisterAttached(
"MaxLength", typeof (int), typeof (MaxLengthBehavior), new PropertyMetadata(default(int), PropertyChangedCallback));
public static void SetMaxLength(DependencyObject element, int value)
{
element.SetValue(MaxLengthProperty, value);
}
public static int GetMaxLength(DependencyObject element)
{
return (int) element.GetValue(MaxLengthProperty);
}
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var tb = dependencyObject as TextBox;
if (tb != null)
{
tb.KeyDown -= TbOnKeyDown;
tb.KeyDown += TbOnKeyDown;
}
}
private static void TbOnKeyDown(object sender, KeyRoutedEventArgs args)
{
var tb = sender as TextBox;
if (tb != null)
{
int max = GetMaxLength(tb);
if (tb.Text.Length >= max)
args.Handled = true;
}
}
}
<TextBox local:TextBoxExtensions.MaxLength="10" />
I have a simple problem. I suggest a list of objects to the user to choose from using p:autocomplete. At the same time the user can create new objects 'on the fly' by typing names that are not in the suggestion. The whole setup is very similar to the vanilla POJO showcase, but I'm posting the code here:
<p:autoComplete id="player" value="#{entry.player}"
completeMethod="{abean.complete}"
var="s1" itemLabel="#{s1.name}" itemValue="#{s1}"
converter="PlayerConverter">
<p:ajax event="itemSelect" listener="#{abean.playerSelected}"/>
</p:autoComplete>
The converter:
#FacesConverter(forClass=Player.class, value = "PlayerConverter")
public class PlayerConverter implements Converter
{
#Override
public Object getAsObject(FacesContext ctx, UIComponent cmp, String value)
{
Player player = null;
if ( !(value==null || value.trim().isEmpty()))
{
// Autocomplete did find a match, the appropriate
// player is returned
try
{
Integer id = Integer.valueOf(value);
player = TeamService.getInstance().getPlayer(id);
}
catch (NumberFormatException e)
{
// Autocomplete found no match, the entered String
// is given. Create an ad-hoc player as response
player = new Player();
player.setName(value);
player.setAdHoc(true);
}
}
return player;
}
#Override
public String getAsString(FacesContext ctx, UIComponent cmp, Object value)
{
String result = null;
if (value instanceof String)
{
result = (String) value;
}
else if (value instanceof Spieler)
{
Integer id = ((Spieler)value).getId();
result = (id == null) ? null : id.toString();
}
return result;
}
}
The problem I am facing seems simple: how do I coerce this construction into allowing the user to simply erase a value? When the user deletes the content of the input field, I would expect to get a call to PlayerConverter with a null or empty value, but that doesn't happen.
How do I get my hands on empty input?
I found a workaround. Use a converter and add a flag to the objects you are displaying in the p:autocomplete. Set the flag to 'false' in the converter's getAsString method. Set it to 'true' in the converter's getAsObject method. The latter is not called when the user emptied the autocomplete field. Therefore the flag remains 'false'. And that you can check in the action method of your command button.
I hope it helps someone.
I need to split a string and convert the results to integers. Is there an easy way to do this?
Here is what I have now:
itemsA = items_str.split("|");
This returns an array with strings, now I need to convert them to integers.
I don't think I can use a "for each" because the string has some undefined values (see the example below)
The original array may look like this:
tmp_arr = new Array();
tmp_arr[1] = 4;
tmp_arr[3] = 5;
items_str = tmp_arr.join("|");
Notice that tmp_arr[0] and tmp_arr[2] are not defined.
Any ideas?
Try something like:
itemsA = itemsA.map(makeInt);
protected function makeInt(value:String, index:int, array:Array):Number {
return parseInt(value);
}
you should give the radix : 10
protected function makeInt(value:String, index:int, array:Array):Number {
return parseInt(value, 10);
}
if you convert a string like "0128" you will have bad surprise.
I have several data manipulation widgets that all implement a custom IPropertyEditor interface. I would like to include a GetValue getter, but the return types could be String, Boolean or int, off the top of my head. In AS3, all of that inherits from Object, so I could have GetValue return an object, but I don't feel great about that approach. At the risk of asking a subjective question, does anyone have any recommendations on how to approach this?
In ActionScript I'm pretty sure you can set a variable return type by defining a function in the following way:
public function getValue():* {
return "Any of these would be fine.";
return true;
return 1;
return new Sprite();
}
Hope that helps.
In practice, there is an actual getter/setter model in ActionScript. For your case, you could use it like this:
private var _value:*;
public function get value() : * {
return _value;
}
public function set value(val:*) : void {
if (typeof val == "int" || typeof val == "boolean" || typeof val == "string") {
_value = val;
}
}
This limits the user to setting (per your requirements) the value of this "value" property to data types int, Boolean, or String.