How to put string as panel name in c# - c#

I have panel that visibility is false. I want when click on treeview list, that list will retrive the panel name that i stored in database. i know how to retrieve that panel name from database in string. But how to change that 'string' into panel to make able to write like this:
panelNamethatLoadFromDB.visible = true;
My Code:
DataTable dtPanelToView = MyLibrary.getResults("SELECT LEVEL1_ID, LEVEL1_PATH FROM LEVEL1_TREEVIEW WHERE LEVEL1_DESC='" + clickLink + "'");
if (dtPanelToView.Rows.Count > 0)
{
string panelToDisplay = dtPanelToView.Rows[0]["LEVEL1_PATH"].ToString();
}
So, currently this "panelToDisplay" is string that contains panel name. i want to change this panel visibilty. Example : panelToDisplay.visible = true;

WinForms stores the controls diplayed on a form in the Form.Controls collection.
You could loop over this collection to find your panel
foreach(var pan in yourForm.Controls.OfType<Panel>())
{
if(pan.Name == panelToDisplay)
{
pan.Visible = true;
}
}
Using the IEnumerable extensions you could also avoid the explicit loop with
var pan = yourForm.Controls.OfType<Panel>().FirstOrDefault(x => x.Name == panelToDisplay);
if(pan != null)
pan.Visible = true;
Keep in mind that the Controls collection of the form stores only the first level of controls. If a control contained in the Form's Controls collection is a control container then it has its Controls collection where other controls could be stored.
EDIT
As from the comment below from TaW you could also use the Controls collection with a string indexer
if(this.Controls.ContainsKey(panelToDisplay))
this.Controls[panelToDisplay].Visible = false;

Related

C# How to get child control in specific control

I need save image from specific child controls(Picturebox inside GroupBox), refer to this question, but GetAll() is from last to first return controls: ckBox8 -> ckBox3 -> ckBox1, how should save image from ckBox1 to ckBox8?
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
}
private void MyControlsTest()
{
var c = GetAll(this, typeof(CheckBox));
var ckBoxlist = c.OfType<CheckBox>().Where(ckBox => ckBox.Checked == true);
foreach (var i in ckBoxlist)
{
MessageBox.Show(i.Name);
/*Save PictureBox inside CheckBox if ckBox.Checked == true*/
}
}
Because your controls have equivalent names, you can transform the name of the control you know, to find the control you want:
foreach(var g in this.Controls.OfType<GroupBox>()){ //'this' is the Form. If all your GroupBoxes are in some other panel/container, use that panel's name instead
var cb = g.Controls[g.Name.Replace("gp", "ck")] as CheckBox;
var pb = g.Controls[g.Name.Replace("gpBox", "pb")] as PictureBox;
//PUT MORE CODE HERE e.g. if(cb.Checked) pb.Image.Save(...)
}
If cb/pb are null you'll need to look into the hierarchy to see why; I can't tell from a screenshot what the control nesting looks like. Indexing a ControlCollection by [some name] brings the first control that is a direct child member of the collection, but remember that controls exist in a tree - if you had a panel inside a groupbox then the checkbox is a child of the panel, not the groupbox (the panel is a child of the groupbox).
If things are deeply nested, you can look at ControlCollection.Find instead - there is a bool youcan specify to make it dig through all children. Note that it returns an array of controls

Properly reload form to its initial state after saving data

I know there's many answers closely to this one but I really want an option from someone experienced.
So, after fill in form, user have option to Save data and remain in form, save data and close form or save data and add new record.
For saving, saving and close its pretty simple, but for saving and add new record, I have some problems.
So far, I'm resetting all controls to its original state after save data.
Comboboxes to SelectedIndex = -1;
Textboxes to string.Empty;
Radioboxes to checked = false;
Checkboxes to checked = false;
DatetimeEdit to Values = null;
And this works for resetting controls in a small form.
Is there any other, faster and better way to achieve this goal?
Maybe closing and reopening Form?
All my controls, comboboxes fill and other needs are made in constructor. I do not load anything in Load Event.
In order to do it efficiently and reusebale its possible to do something like this, please see comments:
private void RollBackForm()
{
// put here all the containers the contain the controls, panel,groupbox the form itself etc...
Control[] Containers = { panel1, groupBox1, this };
// iterate trough all containers
foreach (Control container in Containers)
{
// check control type, cast it and set to default
foreach (Control childControl in container.Controls)
{
if (childControl is ComboBox)
{
((ComboBox)childControl).SelectedIndex = -1;
}
else if (childControl is TextBox)
{
((TextBox)childControl).Text = string.Empty;
}
else if (childControl is RadioButton)
{
((RadioButton)childControl).Checked = false;
}
else if (childControl is CheckBox)
{
((CheckBox)childControl).Checked = false;
}
}
}
}

Using findControl to find a child element

I have a Placeholder and I have a dynamically created panel in the placeholder, I also have some dynamically added radio buttons in the panel, now I can usefindControl() to find the radio buttons if they are direct children of the placeholder.
I've literally spent the whole of yesterday trying to find them when they are the child elements of the Panel. How is there a way to do this?
Here's my code below:
PlaceHolder1.Controls.Add(myPanel); //add the panel to the placeholderenter code here
myPanel.Controls.Add(myRadioButton); //add the radiobutton to the panel
You should make method that recursively searches for a control using it's Id. That mean that the method will search for a control inside of (in your case) placeholder. If method finds control, it will return it. If not, it will go search every placeholder's subcontrol, going "deeper". And then, if nothing is found, it will search one more level down, in every placeholder subcontrols' subcontrol etc.)
private Control FindControl(string ctlToFindId, Control parentControl)
{
foreach (Control ctl in parentControl.Controls)
{
if (ctl.Id == ctlToFindId)
return ctl;
}
if (ctl.Controls != null)
{
var c = FindControl(ctlToFindId, ctl);
if (c != null) return c;
}
return null;
}
and then use it like this:
Control ctlToFind = FindControl(myRadioButton.Id, Placeholder1);
if (ctlToFind != null)
{
//your radibutton is found, do your stuff here
}
else
{
// not found :(
}
Finding Controls recursive is an option, but it also has a couple of down-sides.
If you know the ID's of all the controls you can just use FindControl
RadioButtonList myRadioButton = PlaceHolder1.FindControl("Panel1").FindControl("RadioButtonList1") as RadioButtonList;
Label1.Text = myRadioButton.SelectedValue;
But you will need to give your dynamically added controls an ID.
Panel myPanel = new Panel();
myPanel.ID = "Panel1";
RadioButtonList myRadioButton = new RadioButtonList();
myRadioButton.ID = "RadioButtonList1";
PlaceHolder1.Controls.Add(myPanel);
myPanel.Controls.Add(myRadioButton);

Panel Does not update TreeView

I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.
ReportGroups gr = new ReportGroups();
var Name = gr.GetReportName(groupID);
TreeView tr = new TreeView();
tr.BeginUpdate();
tr.Size = new Size(570, 600);
tr.Name = "Home";
tr.Nodes.Add("Reports Name");
tr.CheckBoxes = true;
if (Name.Count() > 0)
{
foreach (var item in Name)
{
if (item != null)
{
tr.Nodes[0].Nodes.Add(item.reportName);
}
}
}
tr.Nodes[0].ExpandAll();
tr.EndUpdate();
this.pDisplayReportName.Width = tr.Width * 2;
this.pDisplayReportName.Height = 300;
this.pDisplayReportName.Controls.Add(tr);
this.pDisplayReportName.Refresh();
What am I doing wrong?
try to add this.pDisplayReportName.Clear(); so data will not double up. :)
The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.
The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();
And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.
TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);
Cheers

Iterating through textbox controls in a panel C#

I have seen many others with similar problems but I cannot find the flaw in my logic here. Any help would be greatly appreciated.
I have a Panel which I have added numerous label and textbox controls to, ie:
myPanel.Controls.Add(txtBox);
These controls are created and added in a method called previous to the iteration method.
I want to iterate through each textbox and use its Text property as a parameter in another method but I am not having any luck. Here is my attempt to iterate:
public void updateQuestions()
{
try
{
foreach (Control c in editQuestionsPanel.Controls)
{
if (c is TextBox)
{
TextBox questionTextBox = (TextBox)c;
string question = questionTextBox.Text;
writeNewQuestionToTblQuestions(question);
}
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
The problem I am having is that the controls are not in the Panel when I arrive at this updateQuestions() method. Here is the process involved:
A commandButton is clicked and the questions are read from a DB, for each question a method is called which adds 2 labels and a textbox to editQuestionsPanel.Controls. This panel is inside a PlaceHolder which is then made visible.
When a button inside the PlaceHolder is clicked, the updateQuestions() method is called and the editQuestionsPanel.Controls.Count = 1. As there are approx 12 questions in the DB it should be around 36. The one control inside the Panel is of type:
System.Web.UI.LiteralControl
It contains no controls.
I am sure that somwhere in the lifecycle the Panel's controls are being cleared but I do not know how to step thru the life cycle. I have a Page_load method which is called as soon as a button is clicked but once the button which calls updateQuestions() is clicked the editQuestionsPanel.Controls.Count is already back to 1 so it must be cleared before this but I do not know how to correct this...
Any help you can give to help me solve this would be greatly appreciated - its killing me!
This selects from collection controls only that which are of type TextBox.
(the same as control is TextBox or (control as TextBox) != null)
If controls are contained in editQuestionsPanel.Controls:
using System.Linq;
IEnumerable<TextBox> textBoxes = editQuestionsPanel.Controls.OfType<TextBox>();
foreach (TextBox textBox in textBoxes)
{
// do stuff
}
To select all child controls use next extension method:
public static IEnumerable<T> GetChildControls<T>(this Control control) where T : Control
{
var children = control.Controls.OfType<T>();
return children.SelectMany(c => GetChildControls<T>(c)).Concat(children);
}
Using:
IEnumerable<TextBox> textBoxes = editQuestionsPanel.GetChildControls<TextBox>();
When you add controls dynamically, you need to do that on every request - asp.net doesn't do that for you!
Add the controls in the Init or Load phase, then they will get populated with the postback values.
A frequently made mistake: Container.Controls only contains the first level child controls in this container. That is: TextBox1 in PanelA, PanelA in PanelB, you can't get TextBox1 in PanelB.Controls.
My solution is to write an extension method:
public static IEnumerable<Control> AllControls(this Control ctl)
{
List<Control> collection = new List<Control>();
if (ctl.HasControls())
{
foreach (Control c in ctl.Controls)
{
collection.Add(c);
collection = collection.Concat(c.AllControls()).ToList();
}
}
return collection;
}
Now TextBox1 is in PanelB.AllControls(). To filter all controls with type, using PanelB.AllControls().OfType<TextBox>()
If the other answers don't help, try doing your code but add recursivity. Your code would not work if it's editQuestionsPanel => Panel => Textbox
You can do something like this instead:
var questions = from tb in editQuestionsPanel.Controls.OfType<TextBox>()
select tb.Text;
foreach(var question in questions)
{
writeNewQuestionToTblQuestions(question);
}
Try this
int Count = 0;
foreach (Control ctr in Panel1.Controls)
{
if (ctr is TextBox)
Count++;
}

Categories