0

i have a button that should show some content If the name_checked return true

• C# :

private bool name_checked    = false;  // <<--------#

private string username      = "Smith"; 
private string message       =   null; 

public MainWindow() {           

     if (username == "Josh"){ 
         name_checked = true; // <<--------# if right true
         message      =  "Welcome Josh";        
} 

private void button__Click(object sender, RoutedEventArgs e) {  

    if ( name_checked == true ) MessageBox.Show(message);
}

• wpf xaml :

<Button Name="anyname" Click="button__Click"  
Width="100" Height="50" HorizontalAlignment="Right" 
Margin="0,4,154,0" VerticalAlignment="Top" Grid.Row="2" Grid.RowSpan="2" />

└─ output Error : (InvalidOperationExction was unhandled)

What i want is : to stop the Button from acting if the the Boolean name_checked return false
i dont want any message to show at all if the Boolean return false , even Errors

  • so am using it correct or not ?? . if not please show me the right way.
The Doctor
  • 636
  • 1
  • 7
  • 23
  • the first josh is lower-case? maybe a spelling error? then your message is null i guess? –  Feb 01 '17 at 20:15
  • @Jo. they are same in my original script i just write here in wrong way – The Doctor Feb 01 '17 at 20:19
  • What is a "bool key" exactly? – ΩmegaMan Feb 01 '17 at 20:46
  • 1
    Hello SUB-HDR, I have literally copy-pasted your code in a new xaml application and dont get "InvalidOperationExction" exception! It works as expected (based upon the given code). You can however just use if (name_checked) and if you like you can disable the button this.anyname.IsEnabled = false; in the else block of MainWindow() method. Would it be possible for you to post the exception? – Yawar Murtaza Feb 01 '17 at 21:02
  • @YawarMurtaza Yep same in my Friend pc workd normal , it was bug on my VS , i reinstall it and it work now , thank you for trying help :) – The Doctor Feb 07 '17 at 17:08

2 Answers2

1

Set a bool in your ViewModel (CodeBehind)

public bool OkToContinue { get; set; }  

Then in the XAML do this:

<Button IsEnabled="{Binding OkToContinue}"  Click="ButtonBase_OnClick" />

When OkToContinue==false the button will be disabled, otherwise it will be Enabled.

Kelly
  • 6,992
  • 12
  • 59
  • 76
0

Change message to show a default when it is not Josh. Such as:

private string message = "Unknown";

So as to not have a null pointer. string.Empty could work as well.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • in another way to say it : `MessageBox.Show(message);` should not Process when the `name_checked` return flase – The Doctor Feb 01 '17 at 20:32
  • 1
    @SUB-HDR you do not provide enough code to determine why it is failing in the current state. Also the lack of punctuation in your sentences makes it hard to understand what your intent is.... – ΩmegaMan Feb 01 '17 at 20:36
  • i Edited the code to make this question more understandable , look again - and the main thing i want is stop the Button from acting at all if the the bool key return false i dont want any message to show at all if the bool key return flase – The Doctor Feb 01 '17 at 20:40
  • @SUB-HDR The code you present in the button click will *properly* skip any work when the Boolean is `false`. So yes, it looks ok. – ΩmegaMan Feb 01 '17 at 20:42
  • thats what i thougt from a logical idea ! . but when i click this button the program stop debugging and close automatically and showing me "InvalidOperationExction was unhandled" , its look like he passing the if() and progressing the oder: "message.showbox()" – The Doctor Feb 01 '17 at 20:46