ā Buttons xaml
I have 55 buttons and I want to change the text in each of them after clicking, how can I do this without repeating the code 55 times in each method button1_Click, button2_Click, like this: button1.Text = "+";, button2.Text = "+"
100 Replies
you could put a tag in the buttons and cycle all the controls of the form that have that tag
How to tag button?
Im a noob but, one way might be to make a function that sets the name of every button to a variable. Then clicking any button sets that variable, then runs that function.
there is probably better ways to do this, but i think as your a fellow noob, working with the basics a lot helps you grow at a pace you can handle.
it's a property of the control
the point is recovering the group of controls defined by "every button"
actually, thats an assumption
if we take what he said, and instead assume he is being honest when he said
55 times in each method
and only wants an answer to what he is literally asking for, and not trying to force more complicated things onto someone, then my answer is beginner friendly and easy to work with, and gets people started with functions.
and functions are way more important to get used to then tagsi guess that would be a weird way to introduce methods?
er, i guess i meant to say methods yes
sometimes their called methods, sometimes functions, depends on the language i think >_>;
function is a method that returns a value
but yeah, depends on the language
i didn't touch C# for a month, and i'm already fuzzy again
can you help me with this
I tried for 2 hours
I don't know how to do it
oh, uh
sure, easy
First, lets make a function.
the "NameAllButtons" is the name of the function, and inside the {} is where we can put code
before this function, lets make a variable.
string MyButtonNames = "Test"
is a start
then inside the {} lets do uh, Button.(button1's name) = MyButtonNames;
so it looks like this now.
then in the event for a button being clicked, call that function.
it will probably look something like this.
then test it out. if it works, we can continue onto expanding this
=====================Expanding it====================thanks
to expand, now we are gonna change the string name to something.
put
string MyButtonNames = (Whatever you want);
before calling NameAllButtons
that way the name is whatever you want it to be. in your case, its the text of the button. something like Button.Text or (button'sname.Text)
and then for that Button.(button1's name) = MyButtonNames;
we copy and paste it 50 times, one for each button.
oh also
FYI
Name = The name of the button in code
Text = what the user sees
so actually it should be MyButtonsText or whatever
but you hopefully get the ideabruh
not working
you target a button in code by name, but want to change its text
it needs to be the actual name of the button
ah ok
i take it your in WPF?
no I did it in uwp
oh, i don't know UWP but it should be more or less the same. in Winforsm buttons have names assigned by default, in WPF they have no default name and its blank.
so its (Button name).Text = MyButtonNames
because were editing it's text
well, actually its .Text in winforms, WPF uses the command .content
i suppose i don't know what it's refered to as in UWP
googled it, its called Content
so actually you use
(Buttons name).Content = MyButtonsContent;
show me all of it
the brackets were not literal, they were me trying to convey basic info, and also you have the word "Button"
okay here is an example
if i call my button BigSquare
then the command would be
BigSquare.Content = MyButtonNames;
ok
so its (whatever the fuck is the name of your button).Content = MyButtonNames;
also, "MyButtonNames" can be called whatever you want it to be.
strings can have any name
By the way, whenever you want to make something, you can use private void (pick a name) (){} to create a "method" like we just did
methods are just a way to easily organize information
like this?
uh, no...
when you use a = sign, the left thing becomes the right thing.
So here, you want the buttons displayed text to be MyButtonNames
anyway i would at least change the title from buttons winforms to buttons xaml
sorry
uh, no this is UWP
also you cant change titles
oh he did :0
you seem to still be confused, so lets be really clear
what exactly is the name of the button at the top of the pyramid for example.
button1
then put
button1.Content = MyButtonNames;
yes
its the name of the button (Button1) then ".Content" followed by "= any text" and the any text here is a string
There ya go! to make testing easy, make the default of that string be something like "Test name"
it'll make debugging easier
Uh, sure.
that works
At this point, run to test it out. Sofar button 1 should become a +
yes worked
thank you now I can end my project
did you understand everything?
do you have any questions?
š
actually while im at it, you made the mistake of calling this winforms. Winforms VS WPF VS UWP all use different C# commands, so make sure to not mistake them.
also, its very hard and annoying to google stuff for UWP because answers for XAML will come up instead of C#, don't feel bad if google is not answering things.
hey @Dawnbomb
there is small problem
it changes every button content
namespace strategiob
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public string button_text = "+";
public MainPage()
{
this.InitializeComponent();
}
void buttons_manager()
{
//changing button text after clicking it
button1.Content= button_text;
button2.Content = button_text;
button3.Content = button_text;
button4.Content = button_text;
button5.Content = button_text;
button6.Content = button_text;
button7.Content = button_text;
button8.Content = button_text;
button9.Content = button_text;
button10.Content = button_text;
button11.Content = button_text;
button12.Content = button_text;
button13.Content = button_text;
button14.Content = button_text;
button15.Content = button_text;
button16.Content = button_text;
button17.Content = button_text;
button18.Content = button_text;
button19.Content = button_text;
button20.Content = button_text;
button21.Content = button_text;
button22.Content = button_text;
button23.Content = button_text;
button24.Content = button_text;
button25.Content = button_text;
button26.Content = button_text;
button27.Content = button_text;
button28.Content = button_text;
button29.Content = button_text;
button30.Content = button_text;
button31.Content = button_text;
button32.Content = button_text;
button33.Content = button_text;
button34.Content = button_text;
button35.Content = button_text;
button36.Content = button_text;
button37.Content = button_text;
button38.Content = button_text;
button39.Content = button_text;
button40.Content = button_text;
button41.Content = button_text;
button42.Content = button_text;
button43.Content = button_text;
button44.Content = button_text;
button45.Content = button_text;
button46.Content = button_text;
button47.Content = button_text;
button48.Content = button_text;
button49.Content = button_text;
button50.Content = button_text;
button51.Content = button_text;
button52.Content = button_text;
button53.Content = button_text;
button54.Content = button_text;
button55.Content = button_text;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
private void button5_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
private void button6_Click(object sender, RoutedEventArgs e)
{
buttons_manager();
}
and rest of methods
@Dawnbomb
plaese help
@Dawnbombuhhh
okay
please
@Dawnbomb
please
@Dawnbomb
help me anyone
wasnt this what you wanted
no
then tell us what you want
because when I click one button every change sign
just saying you have a problem then showing code that exactly does what in the first message was aksed, doesnt help...
jea that was what you asked in the first message
sorry
tell us what it should do instead
when I click one button it should change one sign in this button what I clicked not in every
but I also don't want to write this 55 times
In every button1_Click method
remove 54 of them
ok
then for every button you have in your xaml use the same eventhandler
then in cs
sender is the button that called the method
cast it to a button, you then have access to content with which you can set well.. the content
what is "eventhandler"?
buttonX_Click...
ok
wtf
if I change every buttonX_Click to example button1_click
then this will not work
why
because it will change only button1 content if I click any of them
like this?
yes like this.. and i already told you in the cs sthe sender parameter will tell you which button was pressed so dont worry about that
oh I know what you were thinking but I need to write 55 methods
no
so in the cs you can now write this
is copy pasting too hard?
wait
I will just copy
*sigh*
ok
working
no
now try it out
will it now update only the button you pressed to + ?
bro my laptop is so slow
wait 2 minutes
yes
It worked
thanks
now one more question:
When content changes to + how to change their color in second click
you could check if there is already a + and if it is change color instead
no i tried this before
show me what you have tried..
and when I click and content change to plus it immediately changes to other color
*sorry for bad english
I tried in winforms
try here again
ok
most likely your code was wrong
there is other syntax in uwp
I don't know how to do it
google
I did it and problem is when I first click it changes the color, not in second click
yes why do you think that is
Because when I click button content changes to "+" and this if automatically changes color
yes but why
I don't know
look you are first setting the button to +
then the line below you are asking if it is +
which is always true wince you just set it before
it wont just magically stop after the first line
so now think how you could tell the computer that if its the first click to do a + and nothing else and on another click if its already + do something else
like this?
nice idea but this works only for one button and not all
hmm
well good luck, im occupied now, dont bother tagging or adding me
ok
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Was this issue resolved? If so, run /close
- otherwise I will mark this as stale and this post will be archived until there is new activity.