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());
Related
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 ;
So I have an application that uses a single master form with a menu along the left-side of the form.
On the right-side I have a panel that acts as a placeholder for any UserControl I have created.
Each time a user clicks on a menu item, the related UserControl will display using code like this:
//Display UserControl first
ucMyUserControl uc = new ucMyUserControl ();
uc.Dock = DockStyle.Fill;
pnlContainer.Controls.Add(uc);
Is there a better way than to just copy and paste this for each button or whenever I want to display a UserControl. Should I be creating a function to call each time and just passing the name of the UserControl I want to use?
Maybe I've got it all wrong in the first place with the way I'm using it - I'm new to this concept and just trying things out.
Sure, don't repeat yourself. Write DRY code with a helper method. It could look like this:
public void ShowPage(UserControl uc) {
while (pnlContainer.Controls.Count > 0) pnlContainer.Controls[0].Dispose();
uc.Dock = DockStyle.Fill;
pnlContainer.Controls.Add(uc);
}
And now you simply call ShowPage(new ucMyUserControl());
Note that the quirky looking while-loop is important, you don't just want to use the Controls.Remove() method. Disposing controls that you remove is very important, if you don't then they'll live forever and ultimately crash your program.
I have a Windows Form that contains only buttons. The final goal is to make a simple logic game I saw but for now the problem is that I want to perform different actions when my New button is clicked, but now it is part from all the buttons in the form so sometimes an action is performed on him too which should not happen. To make myself clear I have two screenshots :
So this is how I want it to be - I have a matrix - 3x3 (in this case, at the end it can be NxN). By clicking New I want to be able to do various things one of which is to make N buttons colored red. What happens now is sometimes my New button also get painted because I go over the buttons like this:
foreach (Control c in this.Controls)
{
if (c is Button)
{
...
and thus sometimes New get selected too, so I end up with this:
What I'm thinking right now is just to perform check whenever I need in the code and exclude my New button explicitly but I don't think it's a good way cause I may end up with a code doing this thing in a lot of places in my program so what is the right solution in this case? If some code is needed please ask.
Quite possibly the easiest solution is to put the Grid in its own Panel (pnlGrid). Put all of the buttons in there, then you could just do the following instead:
foreach (Control ctl in pnlGrid.Controls) {
if (ctl is Button) {
// Do your logic here
}
}
Instead of looping through controls, add all the matrix buttons to a list, and have the new button separated:
private Button[] buttons;
private Button newButton;
Now you can add as many buttons as you'd like to:
for (int i = 0; i < 9; i++)
{
buttons[i] = new Button();
buttons[i].Text = "Button" + i;
Controls.Add(buttons[i])
}
And lastly, your New button will loop through buttons:
private void newButton_Click(object sender, EventArgs e)
{
foreach (Button b in buttons)
{
...
}
}
You could inherit from the button class. Make your own button, use this control (that will have the same functionality that the parent one) for the set, and check for it when you iterate over the controls.
You could also use the Tag property for this pourpose, but I think that inherit will be more clear, adding semantic meaning to your code.
HI
Let me explain what i want to do.
I have a Form with 10 PictureBoxes on it.
When I click at one of them I want to hide all other except the clicked one.
It is possible that on ClickEvent of all of them hide others.but I ask for efficent way.for example with a single function call from click event maybe.
I do not have .net installed on this computer but here is my solution.
Create a Tag for each control, then select all 10 pictureboxes and create one click event for them.
in the click event you can use this code, to loop through all controls and only hide the pictureboxes.
foreach (Control ctrl in Form1.Controls)
{
if (ctrl.GetType() == typeof(PictureBox))
{
if (((PictureBox)ctrl).Tag == ((PictureBox)sender).Tag)
{
ctrl.Hide();
}
else
{
ctrl.Show();
}
}
}
You might be able to compare the objects without Tags, but i can not test this without c# installed.
Just write a function that accepts the Object. In that function you can loop through all those pictureboxes and compare it to the Object. If it's the Sender object you don't hide, otherwise you will.
I have been asked to write c# winforms app that will give users the ability to select options from a checkbox list and have it automatically redraw/repaint a toolstrip with the selected items.
I am new to winforms so I am not sure how to approach it. Should I be using the BackgroundWorker Process? Invalidate()?
Just alittle confused.
Any assistence of pointing in the right direction would be appreciated.
You probably don't want a BackgroundWorker as that's run on a non-UI thread and would cause problems when you try to modify the toolstrip (you can only work with the UI on the thread the UI was created on). Handle the CheckedChanged events on the checkboxes and then add or remove items from the toolstrip. The repainting should be automatic.
You need to keep tooltips for all options some where (if Tag property of checkboxes is free the put it there). Then when an option is selected or deselected, you need to update tooltips.
Let's suppose you are adding all the checkboxes in a IList. then things will work as follows:
private IList<CheckBox> options= new List<CheckBox>();
private void UpdateTTip()
{
toolTip1.RemoveAll();
foreach (CheckBox c in options)
{
if (c.Checked)
toolTip1.SetToolTip(c, c.Tag.ToString());
}
}
Now you need to call this on checkedchanged event of options check boxes:
private void chk_CheckedChanged(object sender, EventArgs e)
{
UpdateTTip();
}
A toolstrip contains controls by itself - it does not just "paint" buttons you can press. In order to have the toolstrip display different buttons depending on different conditions, you can:
Clear the toolstrip items and re-create the ones that are needed in the current context in code when items are checked in the list you mentioned
Add all the items and design time (with property Visible = false) and only set the necessary ones to Visible = true upon selection in your check listbox
No need to do any painting :-)