Convert rtf to xaml in silverlight - c#

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();
}

Related

Odd calculator bug (WPF Application)

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());
}

C# Wpf Create Image and labels with for loop

I'm trying to create an image and label with a loop on C#. I created objects using the drag and drop method and I gave names of all objects and create events manuely. But I need to do this on code behind. I need to use the MouseLeftButtonDown event on image objects. How can I create form objects and events on code behind in a loop?
Form Xaml
<Window x:Class="EnerjiKontrolTakipSistemi.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:EnerjiKontrolTakipSistemi"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" >
<Grid>
<Image x:Name="ImageSigorta1" HorizontalAlignment="Left" Height="155" Margin="38,53,0,0" VerticalAlignment="Top" Width="72" Stretch="Fill" MouseLeftButtonDown="ImageSigorta1_MouseLeftButtonDown"/>
<Label x:Name="lblSigorta1Voltaj" Content="Label" HorizontalAlignment="Left" Margin="55,69,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigorta1Amper" Content="Label" HorizontalAlignment="Left" Margin="55,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Image x:Name="ImageSigorta2" HorizontalAlignment="Left" Height="155" Margin="150,53,0,0" VerticalAlignment="Top" Width="72" Stretch="Fill" MouseLeftButtonDown="ImageSigorta2_MouseLeftButtonDown"/>
<Label x:Name="lblSigorta2Voltaj" Content="Label" HorizontalAlignment="Left" Margin="167,69,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigorta2Amper" Content="Label" HorizontalAlignment="Left" Margin="167,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigorta1Watt" Content="Label" HorizontalAlignment="Left" Margin="51,210,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigorta2Watt" Content="Label" HorizontalAlignment="Left" Margin="163,210,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
<Label x:Name="lblSigortaAdi1" Content="Label" HorizontalAlignment="Left" Margin="42,28,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" Foreground="#FFF74B4B" FontWeight="Bold" Background="{x:Null}"/>
<Label x:Name="lblSigortaAdi2" Content="Label" HorizontalAlignment="Left" Margin="154,28,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.708,0.278" FontWeight="Bold" Foreground="#FFF74B4B" Background="{x:Null}"/>
</Grid>
</Window>
Code Behind
List<Tuple<string, string>> SigortaDurumlari = new List<Tuple<string, string>>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SigortaDurumlariniCek();
ResimleriGetir();
}
private void SigortaDurumlariniCek()
{
SigortaDurumlari.Add(new Tuple<string, string>("Sigorta1", "false"));
SigortaDurumlari.Add(new Tuple<string, string>("Sigorta2", "true"));
}
private void ResimleriGetir()
{
ImageSigorta1.Source = new BitmapImage(new Uri(#"Images\salter_acik.jpg", UriKind.Relative));
lblSigortaAdi1.Content = "Sigorta 1";
lblSigorta1Voltaj.Content = "220 V";
lblSigorta1Amper.Content = "500 A";
lblSigorta1Watt.Content = "3800W";
ImageSigorta2.Source = new BitmapImage(new Uri(#"Images\salter_kapali.jpg", UriKind.Relative));
lblSigortaAdi2.Content = "Sigorta 2";
lblSigorta2Voltaj.Content = "220 V";
lblSigorta2Amper.Content = "500 A";
lblSigorta2Watt.Content = "5400W";
}
bool sigorta_durumu1 = false;
private void ImageSigorta1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sigorta_durumu1 == false)
{
ImageSigorta1.Source = new BitmapImage(new Uri(#"Images\salter_kapali.jpg", UriKind.Relative));
sigorta_durumu1 = true;
}
else
{
ImageSigorta1.Source = new BitmapImage(new Uri(#"Images\salter_acik.jpg", UriKind.Relative));
sigorta_durumu1 = false;
}
}
bool sigorta_durumu2 = false;
private void ImageSigorta2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sigorta_durumu2 == false)
{
ImageSigorta2.Source = new BitmapImage(new Uri(#"Images\salter_kapali.jpg", UriKind.Relative));
sigorta_durumu2 = true;
}
else
{
ImageSigorta2.Source = new BitmapImage(new Uri(#"Images\salter_acik.jpg", UriKind.Relative));
sigorta_durumu2 = false;
}
}
}
Form Runtime Image
Image Link

My first project using C# & WPF - how to set a variable from .xaml.cs to appear in the textbox?

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"/>

Button in a different file than the main

I have 2 button but they need to be in a different file than the mainWindow.cs.
I can't figure how to do that.
So the Button_Click_2 must be in the ReadData.cs and the
Button_Click_3 must be in the WriteData.cs
The app don't recognize the button when they are not in the mainWindow.
How can I do that ?
ReadData.cs :
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
string text = verifyCard("5"); // 5 - is the block we are reading
textBlock1.Text = text.ToString();
}
MainWindow.xaml.cs :
public void Button_Click_2(object sender, RoutedEventArgs e)
{
//When I click it don't detected the code in the ReadData.cs
// The code of the button must be in the ReadData.cs
}
MainWindow.xaml :
<Grid>
<Button Content="Connexion" HorizontalAlignment="Left"
Margin="265,142,0,0" VerticalAlignment="Top" Width="236" Height="44"
Click="Button_Click_1"/>
<Button Content="Lire donnée carte" HorizontalAlignment="Left"
Margin="265,276,0,0" VerticalAlignment="Top" Width="236" Height="42"
Click="Button_Click_2"/>
<Button Content="Ecrire donnée carte" HorizontalAlignment="Left"
Margin="265,344,0,0" VerticalAlignment="Top" Width="236" Height="41"
Click="Button_Click_3"/>
<TextBox Name="textBox1" HorizontalAlignment="Left" Height="23"
Margin="321,224,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left"
Margin="291,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
Height="23" Width="177"/>
</Grid>
It must be possible to forced the app to detected and execute the code in another file than the Main.
I'm stuck... do any of you have the solution ?
You can't just place the button code in ReadData.cs class. as the event is related to ui of MainWindow.xaml create an object for ReadData do something like this
public new void Button_Click_2(object sender, RoutedEventArgs e)
{
ReadData rd= new ReadData();
string text = rd.verifyCard("5");
textBlock1.Text = text.ToString();
}

GroupBox object reference error

I have several GroupBox controls, and all but one work fine. Code is the same, I don't see what the problem might be. I get NullReferenceException. Does anyone have any clue? This is XAML:
<GroupBox Header="Load neural network" Height="90" HorizontalAlignment="Left" Margin="416,34,0,0" Name="groupBox3" VerticalAlignment="Top" Width="207" IsEnabled="False">
<Grid>
<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="114,32,0,0" Name="loadBTN" VerticalAlignment="Top" Width="75" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="6,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="93" />
</Grid>
</GroupBox>
And this is code behind where error pops up:
private void loadRBcheck_Checked(object sender, RoutedEventArgs e)
{
newRBcheck.IsChecked = false;
groupBox1.IsEnabled = false;
groupBox2.IsEnabled = false;
inputTB.IsEnabled = false;
hiddenTB.IsEnabled = false;
outputTB.IsEnabled = false;
groupBox3.IsEnabled = true; //this line causes error
label1.IsEnabled = false;
label2.IsEnabled = false;
label3.IsEnabled = false;
}

Categories

Resources