How to make entry like editor in Xamarin.Forms? - xamarin

I am creating a mobile application in that I need to send a message for query or support when the user creates that type of message and then click on return or done from keyboard entry height should be adjusted and if the user clicks on the back than it should also be managed. In that, I am not able to use Editor control.
I am using a shared project to develop the application.
I try with the following code:
Sample.xaml
<StackLayout Padding="50" HeightRequest="150">
<Entry TextChanged="Entry_TextChanged" x:Name="EntryCustom"/>
</StackLayout>
Sample.xaml.cs
private void Entry_TextChanged(object sender, TextChangedEventArgs args)
{
int isElse = 0;
if (args != null && args.OldTextValue != null && args.NewTextValue != null)
{
string[] oldval = args.OldTextValue.Split('\r');
string[] Newval = args.NewTextValue.Split('\r');
if (oldval.Count() > Newval.Count())
{
EntryCustom.HeightRequest = EntryCustom.HeightRequest - 33;
isElse = 2;
}
else
isElse = 1;
}
if (args.OldTextValue != null && (args.NewTextValue == "\n" || args.NewTextValue.EndsWith("\r")) && EntryCustom.HeightRequest < 100 && (isElse == 1 || args != null || args.OldTextValue.Count() > args.NewTextValue.Count()))
EntryCustom.HeightRequest = EntryCustom.HeightRequest + 33;
}
Can anyone look into this and suggest me what should I have to change in the code?

You should use an Editor. Use <Editor AutoSize="TextChanges"/> and the editor will adjust it's size based on the entered text.

Related

I'm trying to make upgrade system I cannot find way to save this data after when I Start game

private void Update()
{
Score.text = puanlama.ToString();
if (puanlama < firstbarvalue)//used for first bar value to open button activeness
{
activeness.interactable = false;
}
if (puanlama >= firstbarvalue && counterbars == 1)//counterbar for button activeness
{
textforvalues.text = firstbarvalue.ToString();// button value on it
activeness.interactable = true;
}
if (counterbars == 2)
{
activeness.interactable = false;
}
if (puanlama >= secondbarvalue && counterbars == 2)
{
textforvalues.text = secondbarvalue.ToString();
activeness.interactable = true;
}
if (counterbars == 3)
{
activeness.interactable = false;
}
}
public void Buttonactiveness()// Button activeness that I assign to my button[Here button and bars that I used][1]
{
if (puanlama >= firstbarvalue && counterbars == 1)
{
Fuelbars[0].SetActive(true);
for (int i = 1; i < Fuelbars.Length; i++)
{
Fuelbars[i].SetActive(false);
}
puanlama = puanlama - firstbarvalue;
counterbars++;
}
else if (puanlama >= secondbarvalue && counterbars == 2)//Bars which is second one
{
Fuelbars[0].SetActive(true);
Fuelbars[1].SetActive(true);
for (int i = 2; i < Fuelbars.Length; i++)
{
Fuelbars[i].SetActive(false);
}
puanlama = puanlama - secondbarvalue;
counterbars++;
}
I'm trying to put some code to turn this code to player pref and save this data.
playerpref counter bar is not working any suggest
Button activeness that I assign to my buttonHere button and bars that I used
Fuelbars which I'm trying to add is important part of work
counterbar for button activeness
In update section I tried to control button activeness and I changed button text

LINQ Object Referance not set

I am getting an "Object Reference not set to an instance of an object" error when searching for an item (on Guid) that is selected from a datagrid. I have checked that the item does return the Guid correctly (by writing it to a label on the page), however in my linq query (i assume) i am comparing incorrectly.
ctx is the domaindatasource, I know the element im trying to remove exists.
private void medItemRemove_Click(object sender, RoutedEventArgs e)
{
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem;
Guid Mid = M.MedicineInventoryId;
MedicineInventory toRemove = new MedicineInventory();
toRemove = (from a in ctx.MedicineInventories where (a.MedicineInventoryId == Mid) select a).Single();
ctx.MedicineInventories.Remove(toRemove);
ctx.SubmitChanges();
}
Rewrite your code as follows:
private void medItemRemove_Click(object sender, RoutedEventArgs e)
{
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem;
Guid Mid = M.MedicineInventoryId;
MedicineInventory toRemove = (from a in ctx.MedicineInventories where (a != null && a.MedicineInventoryId == Mid) select a).SingleOrDefault();
if (toRemove != null){
ctx.MedicineInventories.Remove(toRemove);
ctx.SubmitChanges();
}
else { .... } // code if toRemove is null
}
Is a null at any point?
toRemove = (from a in ctx.MedicineInventories where (a != null && a.MedicineInventoryId == Mid) select a).Single();
I think your problem is happening because you're creating a new MedicineInventory.
Replace this:
MedicineInventory toRemove = new MedicineInventory();
With this:
var toRemove = ctx.MedicineInventories.Single(mi => mi.MedicineInventoryId == Mid);
Update:
When it retuns the error message "Sequence contains no elements" it's because EF could not find a row in the database with the same Guid you're using in the where clause. In this case and to avoid an exception, you can try this line of code:
var toRemove = ctx.MedicineInventories.SingleOrDefault(
mi => mi.MedicineInventoryId == Mid);
then use an if to delete if it's not NULL:
if(toRemove != null)
{
ctx.MedicineInventories.Remove(toRemove);
ctx.SubmitChanges();
}
else
{
// Only you know what to do! :-)
}
SingleOrDefault returns the only element of a sequence, or a default
value (NULL in this case) if the sequence is empty; this method
throws an exception if there is more than one element in the sequence.
Note: the way you're comparing the Guids is correct because == is overloaded on Guid so you don't need to compare the string representations.
See http://msdn.microsoft.com/en-us/library/system.guid.op_equality%28v=vs.110%29.aspx#Y474

WP7 Textbox with title inside of textbox

I would like to have a textbox that lets a user enter some text (obviously). Let's say it's 'Title'. Is there a pre-built control that shows the name of the field (Title in this case) inside of the text box and then have it clear out when the user enter the field. Example: The search box at the top of this page has 'Search' but when you enter the box it goes away.
Watermarked TextBox
I think I remember it also being in the Mango Silverlight Toolkit too, correct me if I'm wrong:
Mango Silverlight Toolkit
This is an example. Put the GotFocus and LostFocus event in your textbox(in .xaml page).
<TextBox x:Name="UrlTextBox" Text="Search" Margin="0,0,98,0" GotFocus="UrlTextBox_GotFocus" LostFocus="UrlTextBox_LostFocus"/>
In xaml.cs page, add the following codes-
private void UrlTextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (UrlTextBox.Text == "Search")
{
UrlTextBox.Text = "";
SolidColorBrush Brush1 = new SolidColorBrush();
Brush1.Color = Colors.Gray;
UrlTextBox.Foreground = Brush1;
}
else
{
char[] strDataAsChars = UrlTextBox.Text.ToCharArray();
int i = 0;
for (i = UrlTextBox.SelectionStart - 1; ((i >= 0) &&
(strDataAsChars[i] != ' ')); --i) ;
int selBegin = i + 1;
for (i = UrlTextBox.SelectionStart; ((i < strDataAsChars.Length) &&
(strDataAsChars[i] != ' ')); ++i) ;
int selEnd = i;
UrlTextBox.Select(selBegin, selEnd - selBegin);
}
}
private void UrlTextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (UrlTextBox.Text == String.Empty)
{
UrlTextBox.Text = "Search";
SolidColorBrush Brush2 = new SolidColorBrush();
Brush2.Color = Colors.Gray;
UrlTextBox.Foreground = Brush2;
}
}

session value timeout

i am building a site with a login page and want to prevent user from login after 5 try fails for 20 min
maybe one way is this
const int maxTryCount = 5;
const int minutesToSuspend = 20;
[HttpPost]
public ActionResult Index(PeopleAccount account, string returnUrl)
{
if (Session["TimeStamp"] == null || ((DateTime)Session["TimeStamp"]).AddMinutes(minutesToSuspend) <= DateTime.Now)
{
PeopleAccountService service = new PeopleAccountService();
DataSet ds = service.Authentication(account.UserName, account.Password, null, null);
if (ds.Tables[1].Rows.Count > 0)
{
FormsAuthentication.SetAuthCookie(account.UserName, false);
Session.Clear();
Session["UserAccressibility"] = ds.Tables[0];
Session["UserFullName"] = ds.Tables[1].Rows[0][0];
if (returnUrl != null && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
return Redirect(returnUrl);
return RedirectToAction("Index", "Stuff", null);
}
else
{
Session["TryCount"] = (Session["TryCount"] == null
|| (Session["TimeStamp"] != null && ((DateTime)Session["TimeStamp"]).AddMinutes(minutesToSuspend) <= DateTime.Now)) ?
1 : ((int)Session["TryCount"]) + 1;
if ((int)Session["TryCount"] > maxTryCount)
{
Session["TimeStamp"] = DateTime.Now;
return Redirect("~/UnauthorizedAccess/Index");
}
ModelState.AddModelError("", Paymankaran.Content.Messages.InvalidUsernameAndOrPassword);
ModelState.AddModelError("", string.Format(Paymankaran.Content.Messages.TryCountWarning,
Session["TryCount"], maxTryCount, minutesToSuspend));
return View();
}
}
return Redirect("~/UnauthorizedAccess/Index");
}
}
in fact i am using Session["TimeStamp"] and Session["TryCount"] variables to implement this functionality
is there a better or more secure way?
is this a good way to achieve this functionality?
Using Asp.net membership provider would give you this feature (and much more) for free.

How to know which body collide?

i am using cocos2d and box2d, with contact listener, and lets say i have a body that can hit a number of other bodies, BUT each one of them is turn on the contact listener.
so how can i know who hit who ?
i have this in my tick :
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
b2Body *bodyA=contact.fixtureA->GetBody();
b2Body *bodyB=contact.fixtureB->GetBody();
//check if collision between to bodies
if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL) //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
{
NSLog(#"Ball hit bottom!");
}
thanks a lot .
while creating the body set userdata like this
CCSprite *red=[CCSprite spriteWithFile:#"red.png"];
red.tag=3;
[self addChild:red];
b2BodyDef bd;
bd.type=b2_dynamicBody;
bd.position.Set(w/PTM_RATIO,h/PTM_RATIO);
bd.userData=red;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
b2Body *bodyA=contact.fixtureA->GetBody();
b2Body *bodyB=contact.fixtureB->GetBody();
//check if collision between to bodies
if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL) //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
{
so here
CCSprite *actor = (CCSprite*)bodyA->GetUserData();
if ([actor tag] == 3) {
//red box
}
}
Put some identifier into user data. For example:
struct MyUserData
{
int myUniqueId;
};
When creating bodies attach some unique number to each and then you will be able to understand which body was colliding.

Resources