C
C#ā€¢2y ago
tajgel

ā” 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
boiled goose
boiled gooseā€¢2y ago
you could put a tag in the buttons and cycle all the controls of the form that have that tag
tajgel
tajgelā€¢2y ago
How to tag button?
Dawnbomb
Dawnbombā€¢2y ago
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.
boiled goose
boiled gooseā€¢2y ago
it's a property of the control the point is recovering the group of controls defined by "every button"
Dawnbomb
Dawnbombā€¢2y ago
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 tags
boiled goose
boiled gooseā€¢2y ago
i guess that would be a weird way to introduce methods?
Dawnbomb
Dawnbombā€¢2y ago
er, i guess i meant to say methods yes sometimes their called methods, sometimes functions, depends on the language i think >_>;
boiled goose
boiled gooseā€¢2y ago
function is a method that returns a value but yeah, depends on the language
Dawnbomb
Dawnbombā€¢2y ago
i didn't touch C# for a month, and i'm already fuzzy again
tajgel
tajgelā€¢2y ago
can you help me with this I tried for 2 hours I don't know how to do it
Dawnbomb
Dawnbombā€¢2y ago
oh, uh sure, easy First, lets make a function.
private void NameAllButtons()
{

}
private void NameAllButtons()
{

}
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.
string MyButtonNames = "Test"
private void NameAllButtons()
{
Button.(button1's name) = MyButtonNames;
}
string MyButtonNames = "Test"
private void NameAllButtons()
{
Button.(button1's name) = MyButtonNames;
}
then in the event for a button being clicked, call that function. it will probably look something like this.
private void Button1_Click(object sender, RoutedEventArgs e)
{
NameAllButtons();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
NameAllButtons();
}
then test it out. if it works, we can continue onto expanding this =====================Expanding it====================
tajgel
tajgelā€¢2y ago
thanks
Dawnbomb
Dawnbombā€¢2y ago
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 idea
tajgel
tajgelā€¢2y ago
tajgel
tajgelā€¢2y ago
bruh not working
Dawnbomb
Dawnbombā€¢2y ago
you target a button in code by name, but want to change its text it needs to be the actual name of the button
tajgel
tajgelā€¢2y ago
ah ok
Dawnbomb
Dawnbombā€¢2y ago
i take it your in WPF?
tajgel
tajgelā€¢2y ago
no I did it in uwp
Dawnbomb
Dawnbombā€¢2y ago
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;
tajgel
tajgelā€¢2y ago
Dawnbomb
Dawnbombā€¢2y ago
show me all of it
tajgel
tajgelā€¢2y ago
Dawnbomb
Dawnbombā€¢2y ago
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;
tajgel
tajgelā€¢2y ago
ok
Dawnbomb
Dawnbombā€¢2y ago
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
tajgel
tajgelā€¢2y ago
Dawnbomb
Dawnbombā€¢2y ago
methods are just a way to easily organize information
tajgel
tajgelā€¢2y ago
like this?
Dawnbomb
Dawnbombā€¢2y ago
uh, no...
tajgel
tajgelā€¢2y ago
Smadge
Dawnbomb
Dawnbombā€¢2y ago
when you use a = sign, the left thing becomes the right thing. So here, you want the buttons displayed text to be MyButtonNames
boiled goose
boiled gooseā€¢2y ago
anyway i would at least change the title from buttons winforms to buttons xaml
tajgel
tajgelā€¢2y ago
sorry
Dawnbomb
Dawnbombā€¢2y ago
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.
tajgel
tajgelā€¢2y ago
button1
Dawnbomb
Dawnbombā€¢2y ago
then put button1.Content = MyButtonNames;
tajgel
tajgelā€¢2y ago
tajgel
tajgelā€¢2y ago
yes
Dawnbomb
Dawnbombā€¢2y ago
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
tajgel
tajgelā€¢2y ago
Dawnbomb
Dawnbombā€¢2y ago
Uh, sure. that works At this point, run to test it out. Sofar button 1 should become a +
tajgel
tajgelā€¢2y ago
yes worked thank you now I can end my project
Dawnbomb
Dawnbombā€¢2y ago
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.
tajgel
tajgelā€¢2y ago
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 @Dawnbomb
Dawnbomb
Dawnbombā€¢2y ago
uhhh okay
tajgel
tajgelā€¢2y ago
please @Dawnbomb please @Dawnbomb
pendramon
pendramonā€¢2y ago
stare
tajgel
tajgelā€¢2y ago
help me anyone
ACiDCA7
ACiDCA7ā€¢2y ago
wasnt this what you wanted
tajgel
tajgelā€¢2y ago
no
ACiDCA7
ACiDCA7ā€¢2y ago
then tell us what you want
tajgel
tajgelā€¢2y ago
because when I click one button every change sign
ACiDCA7
ACiDCA7ā€¢2y ago
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
tajgel
tajgelā€¢2y ago
sorry
ACiDCA7
ACiDCA7ā€¢2y ago
tell us what it should do instead
tajgel
tajgelā€¢2y ago
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
ACiDCA7
ACiDCA7ā€¢2y ago
remove 54 of them
tajgel
tajgelā€¢2y ago
ok
ACiDCA7
ACiDCA7ā€¢2y ago
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
tajgel
tajgelā€¢2y ago
what is "eventhandler"?
ACiDCA7
ACiDCA7ā€¢2y ago
buttonX_Click...
tajgel
tajgelā€¢2y ago
ok wtf if I change every buttonX_Click to example button1_click then this will not work
ACiDCA7
ACiDCA7ā€¢2y ago
why
tajgel
tajgelā€¢2y ago
because it will change only button1 content if I click any of them
tajgel
tajgelā€¢2y ago
tajgel
tajgelā€¢2y ago
like this?
ACiDCA7
ACiDCA7ā€¢2y ago
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
tajgel
tajgelā€¢2y ago
oh I know what you were thinking but I need to write 55 methods
ACiDCA7
ACiDCA7ā€¢2y ago
no so in the cs you can now write this
private void button1_Click(object sender, RoutedEventArgs e)
{
((Button)sender).Content = "+";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
((Button)sender).Content = "+";
}
tajgel
tajgelā€¢2y ago
ACiDCA7
ACiDCA7ā€¢2y ago
is copy pasting too hard?
tajgel
tajgelā€¢2y ago
wait I will just copy
ACiDCA7
ACiDCA7ā€¢2y ago
*sigh*
tajgel
tajgelā€¢2y ago
ok working no blobthumbsup
ACiDCA7
ACiDCA7ā€¢2y ago
now try it out will it now update only the button you pressed to + ?
tajgel
tajgelā€¢2y ago
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
ACiDCA7
ACiDCA7ā€¢2y ago
you could check if there is already a + and if it is change color instead
tajgel
tajgelā€¢2y ago
no i tried this before
ACiDCA7
ACiDCA7ā€¢2y ago
show me what you have tried..
tajgel
tajgelā€¢2y ago
and when I click and content change to plus it immediately changes to other color *sorry for bad english I tried in winforms
ACiDCA7
ACiDCA7ā€¢2y ago
try here again
tajgel
tajgelā€¢2y ago
ok
ACiDCA7
ACiDCA7ā€¢2y ago
most likely your code was wrong
tajgel
tajgelā€¢2y ago
there is other syntax in uwp I don't know how to do it
ACiDCA7
ACiDCA7ā€¢2y ago
google
tajgel
tajgelā€¢2y ago
I did it and problem is when I first click it changes the color, not in second click
tajgel
tajgelā€¢2y ago
ACiDCA7
ACiDCA7ā€¢2y ago
yes why do you think that is
tajgel
tajgelā€¢2y ago
Because when I click button content changes to "+" and this if automatically changes color
ACiDCA7
ACiDCA7ā€¢2y ago
yes but why
tajgel
tajgelā€¢2y ago
I don't know
ACiDCA7
ACiDCA7ā€¢2y ago
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
tajgel
tajgelā€¢2y ago
tajgel
tajgelā€¢2y ago
like this?
ACiDCA7
ACiDCA7ā€¢2y ago
nice idea but this works only for one button and not all
tajgel
tajgelā€¢2y ago
hmm
ACiDCA7
ACiDCA7ā€¢2y ago
well good luck, im occupied now, dont bother tagging or adding me
tajgel
tajgelā€¢2y ago
ok
Accord
Accordā€¢2y ago
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.