XAML code:
<Window x:Class="rekenmachine_variant_true.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:rekenmachine_variant_true"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="118*"/>
<ColumnDefinition Width="683*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="0*"/>
</Grid.RowDefinitions>
<Button Content="1" Click="num_Click" HorizontalAlignment="Left" Margin="27,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num1" FontFamily="Courier New" FontSize="72"/>
<Button Content="2" Click="num_Click" HorizontalAlignment="Left" Margin="117,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num2" FontFamily="Courier New" FontSize="72" Grid.ColumnSpan="2"/>
<Button Content="3" Click="num_Click" HorizontalAlignment="Left" Margin="90,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num3" FontFamily="Courier New" FontSize="72" Grid.Column="1"/>
<Button Content="4" Click="num_Click" HorizontalAlignment="Left" Margin="27,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num4" FontFamily="Courier New" FontSize="72"/>
<Button Content="5" Click="num_Click" HorizontalAlignment="Left" Margin="117,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num5" FontFamily="Courier New" FontSize="72" Grid.ColumnSpan="2"/>
<Button Content="6" Click="num_Click" HorizontalAlignment="Left" Margin="90,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num6" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="7" Click="num_Click" HorizontalAlignment="Left" Margin="27,155,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num7" FontSize="72" FontFamily="Courier New"/>
<Button Content="8" Click="num_Click" HorizontalAlignment="Left" Margin="117,155,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num8" FontSize="72" FontFamily="Courier New" Grid.ColumnSpan="2"/>
<Button Content="9" Click="num_Click" HorizontalAlignment="Left" Margin="90,155,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num9" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="0" Click="num_Click" HorizontalAlignment="Left" Margin="180,244,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num0" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1"/>
<Button Content="." Click="decimal_Click" HorizontalAlignment="Left" Margin="180,334,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="Nul_Copy" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1"/>
<Button Content="*" Click="keer_Click" HorizontalAlignment="Left" Margin="391,333,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="/" Click="deel_Click" HorizontalAlignment="Left" Margin="483,333,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="=" Click="is_Click" HorizontalAlignment="Left" Margin="575,332,0,0" VerticalAlignment="Top" Height="81" Width="80" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1" />
<Button Content="+" Click="plus_Click" HorizontalAlignment="Left" Margin="391,243,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="-" Click="minus_Click" HorizontalAlignment="Left" Margin="483,243,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="%" Click="modulo_Click" HorizontalAlignment="Left" Margin="392,155,0,0" VerticalAlignment="Top" Height="80" Width="263" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="CE" Click="clear_Click" HorizontalAlignment="Left" Margin="180,155,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Bahnschrift Condensed" Background="#FFFFE74E" Grid.Column="1"/>
<Button x:Name="minmax" Content="-/+" Click="minusPlus_Click" HorizontalAlignment="Left" Margin="575,243,0,0" VerticalAlignment="Top" Height="81" Width="80" FontSize="36" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1" FontWeight="SemiBold"/>
<TextBox x:Name="TxtBox" HorizontalAlignment="Left" Margin="27,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="746" Grid.ColumnSpan="2" Height="110" FontSize="72" FontStyle="Normal" FontFamily="Segoe UI" TextAlignment="Right"/>
</Grid>
</Window>
CS code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace rekenmachine_variant_true
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
decimal valueFirst;
decimal valueSecond;
char operators;
private void decimal_Click(object sender, RoutedEventArgs e)
{
if (!TxtBox.Text.Contains("."))
{
TxtBox.Text += '.';
}
}
private void num_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
TxtBox.Text += btn.Content.ToString();
valueFirst = decimal.Parse(TxtBox.Text);
}
private void minusPlus_Click(object sender, RoutedEventArgs e)
{
if (TxtBox.Text.Contains("-"))
{
TxtBox.Text = TxtBox.Text.Trim('-');
}
else
{
TxtBox.Text = "-" + TxtBox.Text;
}
}
private void minus_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '-';
}
private void plus_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '+';
}
private void keer_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '*';
}
private void deel_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '/';
}
private void modulo_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '%';
}
private void is_Click(object sender, RoutedEventArgs e)
{
valueSecond = decimal.Parse(TxtBox.Text);
decimal result = 0;
if(operators == '+')
{
result = valueFirst + valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '-')
{
result = valueFirst - valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '*')
{
result = valueFirst * valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '/')
{
result = valueFirst / valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '%')
{
result = valueFirst % valueSecond;
TxtBox.Text = result.ToString();
}
if (TxtBox.Text == "0")
{
TxtBox.Clear();
}
}
private void clear_Click(object sender, RoutedEventArgs e)
{
TxtBox.Clear();
}
public MainWindow()
{
InitializeComponent();
}
}
}
**Note: **I'm aware minPlus, modulo, & decimal don't work atm but I want to figure them by myself. (The clear button doesn't really clear values it just clears text within txtbox)
Problem: Everything related to button 2(Name: "num2") messes up the whole calculation and I can't wrap my head around why.
Is there something about decimals that messes it up?
Example:
2 + 6 = 12
2 - 6 results in blank
85 + 2 = 4
Changing content on the number 2 button doesn't seem to work at all, it keeps giving the same results as the examples above.
This code is wrong:
private void num_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
TxtBox.Text += btn.Content.ToString();
valueFirst = decimal.Parse(TxtBox.Text);
}
For the first entry it may be okay, but after an operator button has been pressed the content of TxtBox will be something like: 2+6
Thus valueFirst = decimal.Parse(TxtBox.Text); will have decimal trying to parse "2+6".
Try:
private void num_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
TxtBox.Text += btn.Content.ToString();
valueFirst = decimal.Parse(btn.Content.ToString());
}
Related
So I am created a window with a space to display and show each form, depending on which button click, the issue is I cant seem to find any solution on how to have the form open in the specific window or in the panel of that specific window.
Is this even possible?
My Window design code is as follow:
Title="winHomeDash" Height="650" Width="950"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
AllowsTransparency="True"
materialDesign:ThemeAssist.Theme="Dark"
Background="{x:Null}" Loaded="BtnMenuShort_Click">
<materialDesign:Card UniformCornerRadius="15" Margin="25" Width="900" Height="600" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}">
<materialDesign:Card UniformCornerRadius="15" HorizontalAlignment="Left" Width="225" Background="#0A1828">
<DockPanel Width="225" Height="600">
<StackPanel Height="155" VerticalAlignment="Top" Width="225">
<Image Height="50" Width="50" Margin="0 15 0 15" Source="/UserPic.png"/>
<Label Name="lblName" BorderThickness="0" FontFamily="Arial" FontWeight="Bold" FontSize="17" Margin="0 0 0 5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Height="24" Loaded="Window_Loaded"/>
<Label x:Name="lblDateTime" Content="" FontFamily="Arial" FontWeight="SemiBold" FontSize="12" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Margin="0 0 0 18" Height="22" Width="225" Loaded="LblDateTime_Loaded"/>
<Rectangle Name="rec1" Height="7" Fill="#FF2372FA"/>
</StackPanel>
<materialDesign:Card Name="card2" Width="210" UniformCornerRadius="15" Height="445" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}" Foreground="White" Margin="-210,155,0,0">
<StackPanel VerticalAlignment="Center" Height="445" Name="stpnlMenu">
<Button x:Name="btnCollapse" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="RightToLeft" Margin="195,207.5,0,0" Click="BtnMenuShort_Click">
<Button.Background>
<ImageBrush ImageSource="/more_than_25px.png"/>
</Button.Background>
</Button>
<Button x:Name="btnMenu" Content="MENU" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-227.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" Click="BtnMenuShort_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnHome" Content="HOME" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-187.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnFuel" Content="FUEL" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-147.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu" Click="BtnFuel_Click"/>
<Button x:Name="btnTyres" Content="TYRES" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnService" Content="SERVICE" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-97.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnBags" Content="WASTE BAGS" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-72.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnExpand" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="LeftToRight" Margin="25,-30,0,0" Click="BtnMenuShort_Click" Visibility="Hidden">
<Button.Background>
<ImageBrush ImageSource="/more_than_25px.png"/>
</Button.Background>
</Button>
<Button x:Name="btnChat" Content="QUICK CHAT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnHelp" Content="SUPPORT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnLogOut" Content="LOG OUT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,20" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" Click="BtnLogOut_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
</StackPanel>
</materialDesign:Card>
<StackPanel Width="30" Height="445" Margin="-400,155,0,0" Background="#0A1828">
<Rectangle Name="rec2" Height="10"/>
<Button Name="btnMenuShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Click="BtnMenuShort_Click" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/menu_30px.png" Stretch="Uniform"/>
</Button.Background>
</Button>
<Rectangle Name="rec3" Height="20"/>
<Button Name="btnHomeShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/home_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec4" Height="20"/>
<Button Name="btnFuelShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand" Click="BtnFuel_Click">
<Button.Background>
<ImageBrush ImageSource="/gas_station_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnTyresShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/wheel_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnServiceShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/maintenance_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnBagsShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/waste_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec5" Height="170"/>
<Button Name="btnChatShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/chat_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnHelpShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/help_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnLogOutShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Click="BtnLogOut_Click" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/exit_sign_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec6" Height="10"/>
</StackPanel>
<materialDesign:Clock Name="clClock" Width="675" Height="22" FontFamily="Arial" FontWeight="SemiBold" FontSize="10" Foreground="White" Margin="0,578,0,0"/>
<Grid Width="645" Height="550" Margin="0,25,25,25" Name="stackForms">
</Grid>
</DockPanel>
</materialDesign:Card>
</materialDesign:Card>
The place for the grid is where I want the form to be displayed.
The Window cs code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Configuration;
using Application = System.Windows.Application;
namespace SolAquaPro
{
/// <summary>
/// Interaction logic for winHomeDash.xaml
/// </summary>
public partial class WinHomeDash : Window
{
public WinHomeDash()
{
InitializeComponent();
}
private void LblDateTime_Loaded(object sender, RoutedEventArgs e)
{
}
private void BtnLogOut_Click(object sender, RoutedEventArgs e)
{
this.Close();
Window main = new MainWindow();
main.Show();
}
public void BtnMenuShort_Click(object sender, RoutedEventArgs e)
{
if ((card2.Visibility == Visibility.Visible) && (btnCollapse.Visibility == Visibility.Visible))
{
card2.Width = 45;
card2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
btnBags.Visibility = Visibility.Hidden;
btnChat.Visibility = Visibility.Hidden;
btnFuel.Visibility = Visibility.Hidden;
btnHome.Visibility = Visibility.Hidden;
btnMenu.Visibility = Visibility.Hidden;
btnLogOut.Visibility = Visibility.Hidden;
btnService.Visibility = Visibility.Hidden;
btnHelp.Visibility = Visibility.Hidden;
btnTyres.Visibility = Visibility.Hidden;
btnCollapse.Visibility = Visibility.Hidden;
btnExpand.Visibility = Visibility.Visible;
}
else
{
if ((card2.Visibility == Visibility.Visible) && (btnCollapse.Visibility == Visibility.Hidden))
{
card2.Width = 210;
card2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
btnBags.Visibility = Visibility.Visible;
btnChat.Visibility = Visibility.Visible;
btnFuel.Visibility = Visibility.Visible;
btnHome.Visibility = Visibility.Visible;
btnMenu.Visibility = Visibility.Visible;
btnLogOut.Visibility = Visibility.Visible;
btnService.Visibility = Visibility.Visible;
btnHelp.Visibility = Visibility.Visible;
btnTyres.Visibility = Visibility.Visible;
btnCollapse.Visibility = Visibility.Visible;
btnExpand.Visibility = Visibility.Hidden;
}
}
}
public void Window_Loaded(object sender, RoutedEventArgs e)
{
lblName.Content = Application.Current.Resources["UserAssign"].ToString();
}
private void BtnFuel_Click(object sender, RoutedEventArgs e)
{
Form frmFuelDash = new FuelDash();
frmFuelDash.Show();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
DragMove();
}
}
}
Thank you in advance
You could try to refer to the following code to see if it matches yours.
If you have any questions, please let me know.
Right-click the reference under the project and select Add Reference....
Check System.Windows.Forms and WindowsFormsIntegration and click OK.
Project structure:
MainWindow.xaml:
<DockPanel >
<Grid x:Name="grid" Background="LightYellow" Width="500" Height="400">
</Grid>
<StackPanel x:Name="sp1" Width="200" Height="100" Background="LightSkyBlue" >
<Button x:Name="btn1" Width="80" Height="30" Background="LightGray" Content="form1" Click="btn1_Click" />
<Button x:Name="btn2" Width="80" Height="30" Background="LightGray" Content="form2" Click="btn2_Click"/>
</StackPanel>
</DockPanel>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Forms.Integration;
namespace WpfShowForm
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
Form1 f1 = new Form1();
f1.TopLevel = false;
WindowsFormsHost host1 = new WindowsFormsHost();
host1.Child=f1;
grid.Children.Add(host1);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
Form2 f2 = new Form2();
f2.TopLevel = false;
WindowsFormsHost host2 = new WindowsFormsHost();
host2.Child = f2;
grid.Children.Add(host2);
}
}
}
Add button in Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
The Result:
I'm trying to build a calculator with C# and WPF.
The purpose is to learn and test new things.
Here's the code:
XAML
<Window x:Class="calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:calculator"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click9"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click8"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click7"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click6"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click5"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click4"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click3"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click2"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="Button_Click1"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="Button_Click0"/>
<TextBox HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
<Button Content="Calculate!" HorizontalAlignment="Left" Margin="154,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="+" HorizontalAlignment="Left" Margin="37,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Add"/>
<Button Content="/" HorizontalAlignment="Left" Margin="290,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Multiply"/>
<Button Content="*" HorizontalAlignment="Left" Margin="206,125,0,0" VerticalAlignment="Top" Width="69" Height="57" Click="Button_Click_Divide"/>
<Button Content="-" HorizontalAlignment="Left" Margin="122,125,0,0" VerticalAlignment="Top" Width="68" Height="57" Click="Button_Click_Subtract"/>
</Grid>
</Window>
XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<int> lista = new List<int>();
string number = "";
int res = 0;
private void Button_Click_Add(object sender, RoutedEventArgs e)
{
int numberCreated;
try
{
numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if(lista.Count != 0)
{
foreach(var element in lista)
{
res += element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Subtract(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch(Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res -= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Multiply(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added!");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res *= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click_Divide(object sender, RoutedEventArgs e)
{
try
{
int numberCreated = Int32.Parse(number);
lista.Add(numberCreated);
}
catch (Exception)
{
MessageBox.Show("Number could not be added");
}
number = "";
if (lista.Count != 0)
{
foreach (var element in lista)
{
res /= element;
}
lista.Clear();
}
MessageBox.Show(res.ToString());
}
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
}
private void Button_Click8(object sender, RoutedEventArgs e)
{
number += "8";
}
private void Button_Click7(object sender, RoutedEventArgs e)
{
number += "7";
}
private void Button_Click6(object sender, RoutedEventArgs e)
{
number += "6";
}
private void Button_Click5(object sender, RoutedEventArgs e)
{
number += "5";
}
private void Button_Click4(object sender, RoutedEventArgs e)
{
number += "4";
}
private void Button_Click3(object sender, RoutedEventArgs e)
{
number += "3";
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
number += "2";
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
number += "1";
}
private void Button_Click0(object sender, RoutedEventArgs e)
{
number += "0";
}
}
}
I'm open to any tips and ideas you might give to an absolute beginner.
The reason I'm posting this is because I don't know how to make the "res" variable appear in the TextBox after each operation.
I saw online many people talking about TextBox.text, but I don't know how to use it.
Probably I have to use it in .xaml.cs, but how can I define TextBox? How will my program know which TextBox I want the text to be applied to?
Also, I feel like there's a better approach to create a number using the buttons rather than creating 10 separate functions (from button_click0 to button_click9) and I'd like to hear some ideas.
Thanks!
There are several ways to do this, e.g. Data Binding.
But I guess for learning, since this is your very first step in WPF, you should start more easy.
To access a control via code, you have to give it a name in XAML:
<TextBox x:Name="TxtResult" HorizontalAlignment="Left" Height="35" Margin="37,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="321" FontFamily="Arial" FontSize="20" TextAlignment="Right"/>
By that, you can access your Textbox in Code:
private void Button_Click9(object sender, RoutedEventArgs e)
{
number += "9";
this.TxtResult.Text = number;
}
EDIT to answer your last question:
Also for this there are many ways how to make it better or easier. If you want, you can do some research about Commands, Command Parameters and Command Bindings.
But for this simple Demo case, you can simply use the same event handler for all number buttons:
private void ButtonNumberClick(object sender, RoutedEventArgs e)
{
number += ((Button) sender).Content;
this.TxtResult.Text = number;
}
The sender parameter contains the UI object that triggered the event handler, so you'll get a reference to the button that was clicked. And since the Content property of each button already contains the number you want to add to your string, you can grab it from there and append it to the number string.
Now in XAML you just have to assign that event handler to all ten buttons:
<Button Content="9" HorizontalAlignment="Left" Margin="295,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="8" HorizontalAlignment="Left" Margin="166,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="7" HorizontalAlignment="Left" Margin="37,224,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="6" HorizontalAlignment="Left" Margin="295,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="5" HorizontalAlignment="Left" Margin="166,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="4" HorizontalAlignment="Left" Margin="37,254,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="3" HorizontalAlignment="Left" Margin="295,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="2" HorizontalAlignment="Left" Margin="166,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="1" HorizontalAlignment="Left" Margin="37,285,0,0" VerticalAlignment="Top" Width="63" FontFamily="Arial" Click="ButtonNumberClick"/>
<Button Content="0" HorizontalAlignment="Left" Margin="37,316,0,0" VerticalAlignment="Top" Width="321" FontFamily="Arial" Click="ButtonNumberClick"/>
I have a gridview, which presents all of the users I have in my database, and their details, I want to be able, on the click of a user(on the gridview), that it's details will appear separately on the side of the grid view, and there I could change them accordingly. However, I'm not sure how to do so. I have the ItemClick event in my gridview, and It does recognize if I click on an item, but I don't know how to get that specific clicked user's details, for me to present them on the side. I also have another problem- for some reason It doesn't recognize nor let me click on any of the top row items on the grid view-only the ones below it, does anyone know why?
this is my XAML :
<Page
x:Class="My_Little_Animal.ShowUsers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:My_Little_Animal"
xmlns:data="using:My_Little_Animal"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/Assets/changeUserDetailesBackground.jpg"></ImageBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition Height="147*"/>
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<GridView Name="grid1" ItemClick="grid1_ItemClick" IsItemClickEnabled="True" Margin="-70,151,692,50" Grid.RowSpan="2" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:User">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel Margin="100,20,0,0">
<TextBlock Name="t1" FontSize="20" GotFocus="t1_GotFocus" Text="{x:Bind UserId}"/>
<TextBlock Name="t2" FontSize="20" GotFocus="t2_GotFocus" Text="{x:Bind Password}"/>
<TextBlock Name="t3" FontSize="20" GotFocus="t3_GotFocus" Text="{x:Bind UserName}"/>
<TextBlock Name="t4" FontSize="20" GotFocus="t4_GotFocus" Text="{x:Bind Email}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<Canvas>
<Image Name="loginTitle" Source="/Assets/ShowUsersTitle.png" RenderTransformOrigin="0.5,0.5" Grid.RowSpan="2" Canvas.Left="-325" Canvas.Top="-120" Height="462" Width="1875"/>
<TextBlock Grid.Row="2" Name="ResultTextBlock" FontSize="24" Foreground="Red" FontWeight="Bold" Height="169" Margin="0,0,0,-69"/>
<Image Name="editTheUserName" Width="100" Height="35" Visibility="Visible" Source="/Assets/editIcon.png" RenderTransformOrigin="0.5,0.5" Grid.Row="1" Canvas.Left="852" Canvas.Top="226" />
<Image Name="CancelTheUserNamE" Width="100" Height="35" Visibility="Visible" Source="/Assets/xIcon.png" RenderTransformOrigin="0.485,0.529" Grid.Row="1" Canvas.Left="848" Canvas.Top="224" />
<TextBlock Visibility="Visible" Name="theUserItself" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="your user name " RenderTransformOrigin="0.496,1.165" Foreground="#FF283D6C" Grid.Row="1" Canvas.Left="1103" Canvas.Top="219" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<TextBlock Name="userName" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="User Name : " RenderTransformOrigin="0.496,1.165" TextDecorations="Underline" Grid.Row="1" Canvas.Left="922" Canvas.Top="224"/>
<TextBox Visibility="Visible" Width="200" Name="UserNameText" MaxLength="50" FontFamily="Comic Sans MS" FontStyle="Italic" FontSize="15" BorderThickness="3" BorderBrush="#FF23677D" Grid.Row="1" Canvas.Left="1099" Canvas.Top="223" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<Button Visibility="Visible" Name="changeuserName" Height="49" Width="140" FontSize="20" Content="change" FontWeight="ExtraBold" BorderBrush="White" Foreground="White" BorderThickness="2" Background="#FF191A5C" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.45,0.536" Grid.Row="1" Canvas.Left="1305" Canvas.Top="210" Margin="0,0,0,0"/>
<Image Name="editTheEmail" Canvas.Top="286" Canvas.Left="850" Width="100" Height="35" Visibility="Visible" Source="/Assets/editIcon.png" RenderTransformOrigin="0.49,0.557" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Image Name="CancelTheEmail" Width="100" Height="35" Visibility="Visible" Source="/Assets/xIcon.png" RenderTransformOrigin="0.485,0.529" Grid.Row="1" Canvas.Left="847" Canvas.Top="284" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<TextBlock Visibility="Visible" Name="theEmailItself" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="your email " RenderTransformOrigin="0.496,1.165" Foreground="#FF283D6C" Grid.Row="1" Canvas.Left="1105" Canvas.Top="276"/>
<TextBlock Name="Email" FontFamily="Comic Sans MS" FontWeight="ExtraBold" FontStyle="Italic" FontSize="25" Text="Email : " RenderTransformOrigin="0.496,1.165" TextDecorations="Underline" Grid.Row="1" Canvas.Left="921" Canvas.Top="283" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<TextBox Visibility="Visible" Width="200" Name="EmailText" MaxLength="50" FontFamily="Comic Sans MS" FontStyle="Italic" FontSize="15" BorderThickness="3" BorderBrush="#FF23677D" Grid.Row="1" Canvas.Left="1093" Canvas.Top="284" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"/>
<Button Visibility="Visible" Name="changeEmail" Height="49" Width="140" FontSize="20" Content="change" FontWeight="ExtraBold" BorderBrush="White" Foreground="White" BorderThickness="2" Background="#FF191A5C" HorizontalAlignment="Center" VerticalAlignment="Top" RenderTransformOrigin="0.45,0.536" Grid.Row="1" Canvas.Left="1302" Canvas.Top="274" Margin="0,0,0,0"/>
<Image Name="theReturn" Height="100" Width="100" Tapped="theReturn_Tapped_1" Source="/Assets/theReturnIcon.png" RenderTransformOrigin="0.493,0.496" Grid.RowSpan="2" Canvas.Left="65" Canvas.Top="27" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Canvas>
</Grid>
The c# code :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Xml;
using System.Data;
using Windows.UI.Popups;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace My_Little_Animal
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ShowUsers : Page
{
regitration.regitrationSoapClient cal;
public ShowUsers()
{
this.InitializeComponent();
cal = new regitration.regitrationSoapClient();
getData();
}
public async void getData()
{
regitration.getUserTableResponseGetUserTableResult r = await cal.getUserTableAsync();
List<User> theUserList = new List<User>();
User u = null;
XmlReader xr = r.Any1.CreateReader();
XmlDocument document = new XmlDocument();
document.Load(xr);
XmlNodeList theXmlList = document.GetElementsByTagName("Table");
foreach (XmlElement item in theXmlList)
{
u = new User();
foreach (XmlNode node in item.ChildNodes)
{
switch (node.Name)
{
case "userId": u.UserId = int.Parse(node.InnerText); break;
case "password": u.Password = node.InnerText; break;
case "userName": u.UserName = node.InnerText; break;
case "email": u.Email = node.InnerText; break;
}
}
theUserList.Add(u);
}
grid1.ItemsSource = theUserList;
}
private async void grid1_ItemClick(object sender, ItemClickEventArgs e)
{
var dialog = new MessageDialog("The item that was clicked is : ");
await dialog.ShowAsync();
}
private void t1_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t2_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t3_GotFocus(object sender, RoutedEventArgs e)
{
}
private void t4_GotFocus(object sender, RoutedEventArgs e)
{
}
private void theReturn_Tapped_1(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(Administrator));
}
}
}
I will be so thankful for any help, it's really important for me!
but I don't know how to get that specific clicked user's details, for me to present them on the side.
There are many ways to get the item in item click event. if you have used MVVM pattern, the recommend way is binding command. For more you could refer this case.
In general, you could subscribe ItemClick event and get ClickedItem from event ItemClickEventArgs
Example
private void VideoGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var vedio = e.ClickedItem as VideoItem;
var item = new MediaPlaybackItem(MediaSource.CreateFromUri(new Uri(vedio.videoUri)));
mediaPlayerElement.Source = item;
}
I also have another problem- for some reason It doesn't recognize nor let me click on any of the top row items on the grid view-only the ones below it, does anyone know why?
I could not reproduce this issue, please check if there is some element cover the top row up cause the grid view could not be clicked.
So I have my AML keypad as such :
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
<Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False" Click="btnZzero_Click"></Button>
<Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
<Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
<Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
<Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
<Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnThree_Click"/>
<Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFour_Click"></Button>
<Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFive_Click"></Button>
<Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnSix_Click"></Button>
<Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White" Click="btnSeven_Click"></Button>
<Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnEight_Click"></Button>
<Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnNine_Click"></Button>
Now I want to write a small helper method to run when each button is clicked but I cannot work out how to use the Content from the XAML.
for example this works for button 1 :
private void btnOne_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult();
}
And the helper method uses btnOne.Content but how do I get it to use the Content from any button being pressed? So I can just add the helper methosto each button?
private void addButtonNumberToResult() //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btnOne.Content;
btnTotal.Text = QuantityResult;
}
You can use the sender variable to determine the button clicked:
private void btn_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult((Button)sender);
}
add btn_Click to the Click event of each button (and replace the ones you have already)
private void addButtonNumberToResult(Button btn) //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btn.Content;
btnTotal.Text = QuantityResult;
}
I want to load a rtf file in silverlight using RichTextBox control but this control doesn't load file in proper format. can anyone tell how to do so or convert rtf to xaml?
Here's the xaml :
<Grid x:Name="LayoutRoot" Height="480" Width="640">
<RichTextBox HorizontalAlignment="Left" Margin="89,64,0,0" Name="rtb" VerticalAlignment="Top" Height="301" Width="416"/>
<Button Content="Select File" Height="23" HorizontalAlignment="Left" Margin="268,12,0,0" Name="btnSelect" VerticalAlignment="Top" Width="75" Click="btnSelect_Click" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="FileName:" VerticalAlignment="Top" Width="73" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="89,12,0,0" Name="txtFile" VerticalAlignment="Top" Width="164" />
<Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="430,385,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" Click="btnReset_Click" IsEnabled="False" />
</Grid>
Code behind :
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog odlg = new OpenFileDialog();
odlg.Filter = "Text files|*.rtf";
odlg.Multiselect = false;
string contents = null;
if ((bool)odlg.ShowDialog())
{
txtFile.Text = odlg.File.Name;
StreamReader reader = new StreamReader(odlg.File.OpenRead());
while (!reader.EndOfStream)
{
contents = reader.ReadToEnd();
}
reader.Close();
rtb.Selection.Text = contents;
btnReset.IsEnabled = true;
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
txtFile.Text = "";
rtb.Blocks.Clear();
}