WPF radio button checking - c#

Now im coding my first gui program and i have a problem(I know its very simple,but i cant find an answer for it).I have 2 radio buttons,separeted from eachother,and i cant check if radio button is checked ,here`s my code:
<RadioButton Content="Metinės"
Checked="RadioButton_Checked_1"
HorizontalAlignment="Left"
Margin="393,124,0,0"
Height="21"
Width="101"
FontSize="14"
ClickMode="Press"
VerticalAlignment="Top"
FontFamily="Segoe WP Semibold"/>
And c#
if (RadioButton_Checked == true)
{
//program code
}

Give x:Name or Name to your RadioButton like
<RadioButton x:Name="MyRadioButton" Content="Metinės"/>
and then in code behind you can check
if(MyRadioButton.IsChecked == true)
{
}

You can find out like this
Give your Radio Button name using x:Name ="RBMetLines" and access that in code behind
<RadioButton Content="Metinės"
x:Name="RBMetLines"
Checked="RBMetLines_Checked"
HorizontalAlignment="Left"
Margin="393,124,0,0"
Height="21"
Width="101"
FontSize="14"
ClickMode="Press"
VerticalAlignment="Top"
FontFamily="Segoe WP Semibold"/>
and in C# code behind
private void RBMetLines_Checked(object sender, RoutedEventArgs e)
{
if(Convert.ToBoolean(RBMetLines.IsChecked))
{
//program code
}
}
I have converted IsChecked to Boolean because in WPF IsChecked is bool?.

Related

returning a radio button value to another window in WPF to use as variable

Im trying to create a dodge game, in the said game i created a settings window in which i would like the user to decide whether he want to have bombs in the playboard or not and decide on the difficulty level using radio buttons. then i would like to take the "answer" from the choosed radio buttons and turn them into variables which will control the game:
for example. if bombs are enabled radio button is checked i would like to ask my gamewindow to create bombs on the board and vice versa.
same goes for the difficulty radio buttons
public GameWindow()
{
GameB = new Board(boardsize, 3, true );
VillianSpirit = new Image[GameB.NumberofVillians];
bombs = new Image[GameB.BombCount];
}
as you can see im creating a board (from class board i already have which contructs a board accoring to 3 paramters - boardsize, difficulty (which i set to 3 just to check its working) and bool which says if bombs are enabled (set true just to check its working).
now i cant figure out how to make my settings window send a bool or int to use as variables to create the GameB (game board)
here is the settings window code
<Window x:Class="rotket.SettingsWindow"
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:rotket"
mc:Ignorable="d"
Title="SettingsWindow" Height="200" Width="400">
<Grid x:Name="SettingsButtons" Height="400" Width="400">
<RadioButton GroupName="Group1" x:Name="YesBombs" Content="Yes" HorizontalAlignment="Left" Margin="10,43,0,0" VerticalAlignment="Top" Checked="YesBombs_Checked" />
<RadioButton GroupName="Group1" Content="No" HorizontalAlignment="Left" Margin="10,66,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="Description" HorizontalAlignment="Left" Margin="10,5,0,0" TextWrapping="Wrap" Text="Do you want to have bombs enabled?" VerticalAlignment="Top" Height="34" Width="120"/>
<TextBlock x:Name="Description2" HorizontalAlignment="Left" Margin="251,6,0,0" TextWrapping="Wrap" Text="Which difficulty would you like to play?" VerticalAlignment="Top"/>
<RadioButton GroupName="Group2" x:Name="EasyD" Content="Easy" HorizontalAlignment="Left" Margin="251,43,0,0" VerticalAlignment="Top" />
<RadioButton GroupName="Group2" x:Name="NormalD" Content="Normal" HorizontalAlignment="Left" Margin="251,63,0,0" VerticalAlignment="Top"/>
<RadioButton GroupName="Group2" x:Name="BrutalD" Content="Brutal" HorizontalAlignment="Left" Margin="251,81,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.459,0.168" />
<Button x:Name="ExitSettings" Content="Exit To Menu" HorizontalAlignment="Left" Margin="301,144,0,0" VerticalAlignment="Top" FontSize="14" Click="ExitSettings_Click"/>
</Grid>
here is cs code
public partial class SettingsWindow : Window
{
public SettingsWindow()
{
InitializeComponent();
}
private void ExitSettings_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void YesBombs_Checked(object sender, RoutedEventArgs e)
{
var YesBombs = sender as RadioButton;
YesBombs.IsChecked =
}
}
}
i couldn't figure it out past this, if anyone can help, i will really appreciate it
You can add a settings file to your project with a bool setting that will be your radio button value, then you can read that setting when creating the game window.

Trying to hide password on image click

Trying to hide password while clicking on the image, The code are provided below.In the xaml part i have taken textbox for accepting password in the code behind i had written the code for hiding but it is giving error while making visibility false.
Refer image
<TextBox Name="txtBoxPassw" Height="45" Width="246" InputScope="Password" Margin="10,5,5,5" Background="White" VerticalAlignment="Center"
FontSize="25" FontFamily="Segoe UI Light" Visibility="Visible" Foreground="Black" Padding="50,5,5,5" TextWrapping="Wrap"
BorderThickness="0,0,0,2" BorderBrush="Gray" />
<Canvas Margin="58,-45,136,0">
<Image x:Name="passLogo" Source="Assets/ic_pass.png" Height="41" Width="41" />
</Canvas>
<!--19oct-->
<TextBlock Name="txtBlockPasswFieldError" Grid.Row="2" Margin="-60,0,0,0" Foreground="Black" Text="*Please enter valid name"
HorizontalAlignment="Center" Visibility="Visible" FontSize="11"/>
<!--19oct-->
<Image Name="showimg" Source="Assets/show_pass.png" Width="25" Height="50" Margin="50,15,40,10" Tapped="Image_Tapped" Stretch="Uniform"/>
<TextBlock Name="showPass"
Text="Show Password"
Foreground="#303030"
FontSize="15"
FontFamily="Koblenz Serial Medium"
Margin="200,-45,15,20" />
cs file
private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
BitmapImage bitcurrentimage = showimg.Source as BitmapImage;
//if (showimg.Source == new BitmapImage(new Uri("ms-appx:///Assets/show_pass.png", UriKind.RelativeOrAbsolute)))
if (bitcurrentimage.UriSource.AbsoluteUri == "ms-appx:///Assets/show_pass.png")
{
//passBox.PasswordRevealMode = PasswordRevealMode.Visible;
txtBoxPassw.Visibility = Visibility.Visible;
showPass.Text = "Hide Password";
SetImage("ms-appx:///Assets/hide_pass_.png");
}
else
{
//passBox.PasswordRevealMode = PasswordRevealMode.Hidden;
txtBoxPassw.Visibility = Visibility.Visible.Equals(false);
showPass.Text = "Show Password";
SetImage("ms-appx:///Assets/show_pass.png");
}
}
actually i want to put validation(if (Utility.isEmptyString(txtBoxPassw.Text)) on the string typed in passwordbox, but when i am trying it is giving error "'PasswordBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'PasswordBox' could be found (are you missing a using directive or an assembly reference?)"
This is your last comment, and from this comment I assume that you accept the method to use the built-in control PasswordBox instead of creating your own one.
Then you can refer to PasswordBox class, unlike a TextBox or a TextBlock, PasswordBox does not have a property named "Text". I think what you need is the PasswordBox.Password property.
For example:
<PasswordBox x:Name="passwb" Height="30" IsPasswordRevealButtonEnabled="True" />
<Button Content="click" VerticalAlignment="Bottom" Click="Button_Click" />
Code behind:
private void Button_Click(object sender, RoutedEventArgs e)
{
var pass = passwb.Password;
}
The pass here indicate the password currently held by the PasswordBox which named "passwb".
In Winform there is a textbox parameter 'PasswordChar', if you set it to a character( for example *) it will replace all characters in the textbox to that character. If you set the passwordChar to '\o' (PasswordBox.PasswordChar = '\0';) it will display all the characters like normal. I hope there is something similar in UWP - I am not into UWP.

clicking messagebox.show() hides showdialog() window

I have following code in WPF:
private void BtnTicketPrice_Click(object sender, RoutedEventArgs e)
{
TicketPrice TP = new TicketPrice();
TP.ShowDialog();
}
in New Window form i have following code:
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("sometext");
}
on clicking messagebox button the form (TicketPrice) also is closing;
how to show messagebox without closing the form?
If i'd change TP.ShowDialog(); to TP.Show();, it works correctly. I have this problem only with this TP.ShowDialog();
xaml of button
<Button x:Name="BtnSave" HorizontalAlignment="Left" Margin="619,362,0,0" VerticalAlignment="Top" Width="165" Height="66" IsCancel="True" TabIndex="4" Click="BtnSave_Click">
<StackPanel Orientation="Horizontal">
<TextBlock Text="save " HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="SemiBold" />
<Image Source="pack://siteoforigin:,,,/Resources/Save.png" Height="50" Width="59" />
</StackPanel>
</Button>
You're seeing the IsCancel property on the Button working as advertised. Don't set it to true and clicking the button won't automatically close the dialog.

Bind NavigateUri property of HyperlinkButton Windows Phone 8

I am parsing some JSON data using Newtonsoft.NET for my WP8 app in C#/XAML and everything works fine except the binding of NavigationUri to the HyperlinkButton. Here is the DataTemplate of my databound LongListSelector:
<DataTemplate>
<StackPanel Margin="10 10 10 20" Background="{StaticResource PhoneAccentBrush}">
<TextBlock Text="{Binding MovieTitle }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding ImdbCode }" TextWrapping="Wrap" FontSize="18" />
<HyperlinkButton NavigateUri="{Binding ImdbLink}">View on IMDB</HyperlinkButton>
<Image Source="{Binding MovieCover}" Width="300" Height="300" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding ImdbLink}" TextWrapping="Wrap" FontSize="18" />
</StackPanel>
</DataTemplate>
This way everything is loaded correctly but the HyperLinkButton doesn't navigate to the Uri.
What should I do to make sure the navigation works?
If it is not possible with HyperLinkButton then can I add a regular button to which I could pass the url so that OnClick event can navigate to url.
Assuming the url is correct the webbrowser can be activated like this:
XAML
<HyperlinkButton NavigateUri="{Binding ImdbLink}"
Click="NavigateButton_Click">View on IMDB</HyperlinkButton>
C#
private void NavigateButton_Click(object sender, RoutedEventArgs e)
{
var hyperlinkButton = sender as HyperlinkButton;
if(hyperlinkButton == null)
{
return;
}
ShowInBrowser(hyperlinkButton.NavigateUri);
}
private void ShowInBrowser(Uri url)
{
Microsoft.Phone.Tasks.WebBrowserTask wbt =
new Microsoft.Phone.Tasks.WebBrowserTask();
wbt.Uri = url;
wbt.Show();
}
This is because you have no RequestNavigate method. Try:
<HyperlinkButton NavigateUri="{Binding ImdbLink}" RequestNavigate="Hyperlink_RequestNavigate">View on IMDB</HyperlinkButton>
and in your code behind for the view put
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
or however you want to deal with that click event (i.e. open a new webview with that as the url)

How focus out the Cursor from textbox in wpf?

<Grid x:Name="LayoutRoot">
<TextBox x:Name="TxtFocusOut" Height="74" Margin="186,149,225,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="26.667" TextChanged="TextBox_TextChanged"/>
</Grid>
private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
if(TxtFocusOut.Text.Length>=4)
{
MessageBox.Show("Four you typed four values");
}
}
After I clicked Ok Button On message box,the cursor should be focused out from the texbox.But now it is Blinking in the textbox.What should i do to focused out from the testbox?
FocusManager.SetFocusedElement(AnotherElementID);
by doing this the this will loose its foucs and foucs can be use for another element.
or
Keyboard.ClearFocus();
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.clearfocus.aspx
try any of these

Categories