Visual Studio C# multiple Button array - c#

So in my Windows forms application (C#) i have a grid of button elements (btn0, btn1, ... , btn200) . I've been looking around for some time now but I couldn't find the answer I was looking for.
The question is how can I change the property of all the button at the same time.
At first I tried formatting a string like this: "btn" + id, (the id being the button number) so that I would have every button name in a string. But then I had a problem changing from string to button (type). Is there a way to do that ?
The other thing I tried to do was to create an array of buttons, but haven't had success with that either.
Is one of those two ways possible and how?

Assume that the buttons are all in a control named parent:
foreach(Button btn in parent.Controls.OfType<Button>())
{
// do something with btn
// eg btn.Text = "New Text";
}
If the buttons are directly in the form, use this instead of parent. If the buttons are in some kind of a panel, use panelName instead of parent.
The above works because as you stated, all the buttons share the same parent. However, if in some other scenario you have buttons in different parents (say panels), you would then need to do some recursion or specify multiple specific parents to loop on).

You can find it by
Controls.Find("btn1", true);
Where btn1 is the name of the button (the string you generated)

FIrst of all why don't you use an array or List of buttons in place of button1,button2.
Button[] buttons=new Button[size];
....................
button[i].Property="value";
If you don't want to change then other option would be to use a linq query on Form.Controls like this,
var buttons = from button in Controls.OfType<Button>() where
button.Name.StartsWith("btn") select button ;
This will give you list of all buttons that matches your pattern.
or use "button.Name=="btn" + id" to get the exact button you are looking for.
var buttons = from button in Controls.OfType<Button>() where
button.Name=="btn"+id select button ;

Related

Xamarin: how do I change the color of a button added in code?

My Xamarin app creates buttons in code. I can change the color of a button I click, as the button is referenced in its Clicked handler's sender argument, but I want to change the color of buttons that I didn't click. The problem is, how do I find the buttons I want to change?
I thought about using FindByName, but Name doesn't appear to be an attribute.
One way I can think of: loop through all of the buttons until I find the one with the StyleId of the desired button. Is there an easier way than that?
when you create a button in code you need to keep a reference to it, like you would with any C# object you want to reference later
Button MyButton = new Button { ... };
MyButton.BackgroundColor = Color.Purple;
if you need to access it from multiple places in your code, you should declare it as a class level variable to that it has scope throughout your class
If you want to find the button created in code behind without XAML, like #Jason said, use the variable name of the Button you created or set it directly.
Button button = new Button { ClassId = "button1", BackgroundColor = Color.Red };
If you want to access a view with FindByName, you need to create the button with x:Name in XAML.
There is an XAML example:
<Button x:Name="button1" ></Button>
And in your code behind you could do something like:
button1.BackgroundColor = Color.Red;
If you need to find a view in xaml for some reason, do this:
var button = this.FindByName<Button>("button1");
button.BackgroundColor = Color.Red;

Is it possible to hide multiple winforms objects with array?

I'm new to C#, I mean I did not code for a while so I forgot like everything. I'm trying to hide three objects with an array to make it easier with one line of code
This is how my code looks now.
button1.Hide();
button2.Hide();
button3.Hide();
But I tried every method that I could but nothing worked. This is how I think it should work.
Object[] Buttons = new Object{button1, button2, button3};
Buttons.Hide();
Could you place your buttons in a panel and hide that instead? That will scale to anything else in future too should you choose to modify your UI as long as everything which needs to be hidden is in the same Panel?
So instead of doing something like
void HideButtons(IEnumerable<Button> buttons)
{
foreach(var button in buttons)
button.Hide();
}
...
HideButtons(new[]{ button1, button2, button3 });
you can simply call Hide() on the containing panel
panel.Hide();
you can do it via for or froeach:
List<Button> buttons=
new List<Button>(){button1,button2,button3};
foreach(var button in Buttons)
button.hide();
or
new List<Button>(){button1,button2,button3}.ForEach(item=>item.hide());

C# Increment for loop every time button is pressed

I googled a few things before posting, but I couldn't find anything like this. Basically, I want to take text from a textbox, save as a variable (say history1) to then be able to call that in the future to display the text. I can do that, but what I'm stuck with is that I want 3 variables (history1, history2 and history3, for example) and each time the button is pressed the string is moved to the next variable.
For example, the button is pressed, the text is saved as variable history1. The text is changed and the button is pressed again, the text from history1 is moved to variable history2, and the new text is saved as history1. This would only need to work for 3 instances though, not infinitely, so when text is stored in history3 and the button is pressed the text is just overwritten.
The way I had thought of approaching this was:
string history1;
string history2;
string history3;
for (int i = 1; i < 4; i++)
{
history1 = txtOutput.Text;
btnToFile_Click()
{
history2=history1;
btnToFile_Click()
{
history3=history2;
}
}
}
However, this isn't going to work because the btnToFile_Click doesn't take any arguements. Is there an easier way to go about this or just a way to fix the method not taking arguements ?
Thanks in advance!
Make sure that you delcare history1, history2, and history3 on the form level (not inside any method).
Then, have the following code inside the handler of the click event of the button:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
You don't need to call the btnToFile_Click() method multiple times in your loop, just move the text from end textbox to another in reverse order. Nor do you need a loop because you only have three textboxes.
Why reverse order? So you move the value to the next textbox before it is overwritten by the new value.
So:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
btnToFile_Click() is a Click event handler for btnToFile (a button). You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). By the way, it does receive a parameter, then event source (since you can assign the same event handler to multiple buttons and do something based on which one sent the event)
You can try saving in a string array and move the strings within it when you call the button clicked event

Dynamically show forms

Please I need help with this issue. I have a dynamic menu with buttons. Each button has name of form(VP,VZPAL,PAL,...).and when I click on some button, I want to activate form with the same name that the button has. I try do it with this code, but its escape with exception:
Value cant be null.
string Formname = "FISpanel.Form" + b.Name.ToUpper().ToString();
Form frm = (Form)Activator.CreateInstance(Type.GetType(Formname));
frm.Show();
Value of Formname is right(FISpanel.FormVP).Any ideas?
This is how I've achieved the same thing in the past. In my application I have 'links' that are set inside of .ini files, but these can be different for each user, so (like you), I needed a solution that would launch forms depending on what was clicked:
LinkLabel lnk = (LinkLabel)sender;
Type type = Type.GetType("Valhalla." + lnk.Tag.ToString());
Form thisFrm = (Form)Activator.CreateInstance(type);
// DISPLAY THE FORM //
thisFrm.ShowDialog();
Also, as #King King has mentioned. The dot is very important. Are your forms actually named: "FormVP" Or just "VP" (etc...) If the later then you will have to call them as: Form.VP and not FormVP.

problem in opening next form on button click

i've two groups of radio buttons, a drop down list & a submit button..on corresponding selected radio buttons & drop down item i need to open next specific form..
say if i selected in first radio button say Existing user(of website) and in another say individual, and in drop down if i select residential property then it should open a residential form,if i select commercial property then it should open a commercial form etc.
can u please help me out??
I assume there is button and on click of this button you have open either of the forms.. You can add a Button_Click() event handler and in there check
Psuedo-code looks like this
if(radioButton1.selected && dropdownList.SelectedValue =="Residential")
{
frmResidential frmRes = new frmResidential();
frmRes.Show();
}
Code example in previous post will work for window forms. Looks like you have Web Application. In this case you can use Response.Redirect method to go to different web form.
if(radioButton1.selected && dropdownList.SelectedValue =="Residential")
{
Response.Redirect("YouPage.aspx");
}

Categories