c# - Changing the text of the button (at runtime) -


this question exact duplicate of:

i have button shows "click me!" on it. want see after clicking, shows "do not click on me!" permanently.

i below error message in buttonname_click() function , not know how resolve (i have spent time on it).

the name 'buttonname' not exist in current context

i attaching search.xaml , search.xaml.cs in question.

<sdk:page    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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  xmlns:local="clr-namespace:tmtemplate" xmlns:mocal="clr-namespace:dynamicnavigation" datacontext="{binding relativesource={relativesource self}}" xmlns:contactform="clr-namespace:contactform" mc:ignorable="d" x:class="tmtemplate.search"  title="search" d:designwidth="640" d:designheight="480" height="530" width="700"> <grid x:name="layoutroot" >     <stackpanel margin="50,45,25,45">         <button name="buttonname" content="click me!" click="buttonname_click" margin= "0,100,0,0"  height="93" width="518"></button>     </stackpanel> </grid> 

using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using system.windows.media.imaging;  namespace tmtemplate {     public partial class search : page     {         public search() {           initializecomponent();         }         private void buttonname_click(object sender, routedeventargs e)         {          buttonname.content = "do not click on me!";         }        } } 

you try instead:

    private void buttonname_click(object sender, routedeventargs e)     {      ((button)sender).content = "do not click on me!";     }  

but, run code is, need specify name x:name instead of name:

<button x:name="buttonname" content="click me!" click="buttonname_click" margin= "0,100,0,0"  height="93" width="518"></button> 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -