Issue in displaying "-" symbol in calculator application in C# WPF - c#

Hi i am trying to learn windows presentation foundation WPF and was trying to develop the simplest application Calculator. But i am having issue in displaying the '-' when i substract a higher value from smaller. For eg. if i do something like this "10 - 20" the output should be "-10" in the screen i.e. textbox. But it is displaying "10-". Somehow the '-' is coming in the end. my xaml code for textbox looks like following:
<TextBox Height="33" HorizontalAlignment="Left" Name="outputbox"
VerticalAlignment="Top" Width="278"
FontFamily="Tahoma" FontSize="18"
FlowDirection="righttoleft" IsReadOnly="True" />
and the code for doing substraction and display looks something like this
if (entry1 > entry2)
{
outputbox.Text = (entry1 - entry2).ToString();
}
else
{
outputbox.Text = "-" + (entry2 - entry1).ToString();
}
while debugging it shows the proper string as "-10" but while displaying in the textbox it is showing the string "10-". Any idea about what is missing???

Just remove the FlowDirection attribute from your TextBox and your result will be fine.
<TextBox Height="33"
HorizontalAlignment="Left"
Name="outputbox" VerticalAlignment="Top"
Width="278"
FontFamily="Tahoma"
FontSize="18" IsReadOnly="True" />
Or you may specify FlowDirection="LeftToRight" which is the default for the TextBox

Thanks All. I removed the "FlowDirection" property and it displayed the proper "-10" but the text was now displayed on the left side of textbox. So i used the "TextAlignment = right" property and it seems to be working fine. Thanks again all of you.

Related

Accessing values of e.ClickedItem object from C# GridView on Windows Phone 8.1

I'm building a Windows Phone 8.1 App for a client and have run into something I can't source an answer for, hence the question. I apologize in advance as it's probably something obvious and I'm oblivious to it.
I have a GridView bound to a vendor list from my data model. I can select / click a returned value from the Grid as planned.
XAML extract:
<GridView x:Name="VendorsGridView"
ItemsSource="{Binding}"
Grid.Row="1"
Margin="10,8,10,12"
Foreground="White"
SelectionMode="Single"
IsSwipeEnabled="False"
ItemClick="VendorsGridView_ItemClick"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource VendorTemplate}">
</GridView>
</Grid.RowDefinitions>
<TextBlock Text="{Binding StrVendor}"
Name="VNo"
Grid.Row="0"
Margin="0,0,0,0"
VerticalAlignment="Top"
HorizontalAlignment="Center"
FontSize="18"
FontWeight="SemiBold"/>
<TextBlock Text="{Binding StrVName}"
Name="VName"
Grid.Row="1"
Margin="0,0,0,0"
VerticalAlignment="Center"
TextWrapping="Wrap"
FontSize="14"/>
If I examine e.ClickedItem in the debug window I can see the value I am trying to find, (StrVendor or vendor number). The design is then to write this back to a static table for use as a default when looking at other data.
Debug Window Output:
e.ClickedItem
{MOQ_v301.ViewModel.VendorViewModel}
base: {MOQ_v301.ViewModel.VendorViewModel}
IsDirty: true
isDirty: true
StrVendor: "40520"
strVendor: "40520"
StrVName: "Forest Garden PLC"
strVName: "Forest Garden PLC"
My question is how can I get the vendor number captured to a string variable to achieve it?
Because your grid is bound to a model, you are getting 1 item of that model back through the click. So just cast that item to the model and get the Vender number... Not sure what the actual model class is but assuming it's VendorViewModel the code should be: ((VendorViewModel)e.ClickedItem).StrVendor this will give you the actual vendor number.
Best to check if you are actually getting a model so the final code could be as following:
if(e.ClickedItem is VendorViewModel)
{
var vendornr = ((VendorViewModel)e.ClickedItem).StrVendor;
}

WPF : Text loaded in a Wpf textbox is cutted ( not entire )

I'm trying to load a text ( as a string ) in a wpf textbox.
string str;
string = myFunction();
textBox.Text = str;
Here's my XAML :
<TextBox Height="174" Name="textBox1" Width="811" TextWrapping="Wrap" />
My TextBox result is not entire. I trye to write the str in a file on my disk, and the file contains all my data.
Anyone have some idea ?
Thanks a lot,
You've constrained the size of the TextBox, so if you have too much text you won't be able to see it all.
Remove the constraints, or specify a VerticalScrollBarVisibility value:
<TextBox Height="174" Name="textBox1" Width="811" TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />

TextBox.ContextMenuOpening not firing in Windows Phone 8.1 App

I'm writing a universal windows app and am trying to get my own Context Menu for a TextBox. Everything works as expected in the Store App, but on the Phone App the ContextMenuOpening event isn't firing. I've tried with holding and tapping a selected text, but it isn't working, the only thing that is happening, is the little circle for Copy showing up.
Here's where I register the Event Handler: (the method is called at page loading)
public void FlipViewLoaded()
{
TextBox textBox = GetChildControl<TextBox>
(_imagesFlipView, "ReadOnlyTextBox");
textBox.ContextMenuOpening +=
new ContextMenuOpeningEventHandler(Open);
}
And this is the handler:
private async void Open(object sender, DoubleTappedRoutedEventArgs e)
{
e.Handled = true;
TextBox textbox = (TextBox)sender;
if (textbox.SelectionLength > 0)
{
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Get Word", null, 1));
menu.Commands.Add(new UICommand("Get Text", null, 2));
var chosenCommand = await menu.ShowAsync(new Point());
if (chosenCommand != null)
{
switch (chosenCommand.Id.ToString())
{
// different commands implementations
}
}
else
{
Debug.WriteLine("The chosen command is null !!");
}
}
else
{
Debug.WriteLine("The selected _text is null !!");
}
}
As I said, it works perfectly in the Store App (the menu shows up when I hold the selected Text or when I right click on it), but the event doesn't even get fired in Phone App.
EDIT Here's the part of the xaml code with the TextBox (the rest is just the standard code that comes with the page + a hub):
<HubSection>
<DataTemplate>
<FlipView x:Name="ImagesFlipView" ItemsSource="{Binding Images}"
viewmodel:ImagesPageViewModel.FlipView="{Binding ElementName=ImagesFlipView}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding ImageURL}" />
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Stretch" >
<TextBox x:Name="TranslationTextBox" Visibility="Visible"
Height="80" IsReadOnly="True" TextWrapping="Wrap"
BorderThickness="0" Margin="5"
Style="{StaticResource MyTextBoxStyle}"
Background="{StaticResource TextBoxButtonBackgroundThemeBrush}"
Foreground="White" FontSize="25" VerticalAlignment="Bottom" />
<TextBox x:Name="ReadOnlyTextBox" FontSize="25" IsReadOnly="True"
Height="80" TextWrapping="Wrap" Text="{Binding Path=Translations[english]}"
BorderThickness="0" Foreground="White" Margin="5"
Style="{StaticResource MyTextBoxStyle}"
Background="{StaticResource TextBoxButtonBackgroundThemeBrush}"
VerticalAlignment="Bottom" />
</StackPanel>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</DataTemplate>
</HubSection>
The answer is simple, the TextBox does not have a ContextMenuOpening event in WindowsPhone.
So even if you put that code in an universal apps, it cannot happen.
Universal apps only TRY to match windows 8.1 with windows phone. If an event, or a property is not found, and a correspondance is not found, it is simply ignored.
EDIT: To complete the answer, what you have to do, is to think of another behavior when you are in your windows phone app.
Universal apps projects define pre-processing variables, so you can use code like
#if WINDOWSPHONE
var myWindowsPhoneVar = "windowsPhone";
#else
var myWindowsPhoneVar = "!windowsPhone";
#endif
I'm not quite sure the preprocessing variable for windows phone is exactly "WINDOWSPHONE" but you won't have trouble finding it.

C# store app. Add "X"button at the end of a textbox or searchbox

I have a C# store app where I have a search box or a textbox. I noticed that when I enter text in either the text or searchbox I don't get the "X" button at the end of the box to clear the text.
In this example there is someone with a similar problem: Remove "X" button at the end of a TextBox But I want to reverse that action.
As I understand in the above link, the button should appear automatically, but it does not. Does anyone know how I can get that "X" button to appear on either a searchbox or textbox?
the boxes:
<TextBox
x:Name="textboxNoX"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="401,66,0,0"
TextWrapping="Wrap"
Text=""
VerticalAlignment="Top"
Width="259"/>
<SearchBox
x:Name="searchbarNoX"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="400.998,33.478,0,0"
VerticalAlignment="Top"
RenderTransformOrigin="0.5,0.5"
UseLayoutRounding="False"
Width="256.565"
d:LayoutRounding="Auto">
Ah the property TextWrapping="Wrap" should be set to TextWrapping="NoWrap".
Removing the TextWrapping property from the TextBox XAML worked for me.

wp7 unicode characters display as square

i want to add some emotions to my twitter client app, such as: ಥ_ಥ (,,Ծ‸Ծ,,)
and some one has special character in his user name,
but the characters don't show up, just displayed as 口口.
here is my code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button
Content="(,,Ծ‸Ծ,,)
(,,Ծ‸Ծ,,)
√
ಠ_ಠ
✧ (≖ ‿ ≖)✧"
/>
</Grid>
otherwise, they displayed well on SMS and Internet Explorer,
screenshot is here: http://sdrv.ms/171Qf7t
It looks like you have changed your default font somewhere to one that doesn't have those Unicode characters. Either return to the default font or use something like:
<Button
Content="(,,Ծ‸Ծ,,)
ಠ_ಠ
✧ (≖ ‿ ≖)✧" FontSize="24" FontFamily="Segoe WP Semibold"
/>
which displays correctly when I test it here.

Categories