Dynamic names for panels - c#

I'm working on a program that is supposed to use with multiple users. I'm getting the users from a database. So far so good...
I'm dynamically adding a panel to the form, which would hold some data. I'm using this piece of code to achieve that:
string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = false;
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
splitContainer2.panel_name.Controls.Add(new_item);
as you will probably notice, this line of code is not working:
splitContainer2.panel_name.Controls.Add(new_item);
How can I achieve this, so when I want to make panel email_in_1 visible, I can use that, and make email_in_8 not visible?
### EDIT 1 ###
the code now looks like this:
string panel_name = "email_in_" + user_credentials[counter_snel][0];
Panel new_user_panel = new Panel();
new_user_panel.AutoSize = true;
new_user_panel.Dock = System.Windows.Forms.DockStyle.Fill;
new_user_panel.Location = new System.Drawing.Point(0, 0);
new_user_panel.Name = panel_name;
new_user_panel.Visible = true;
user_email_in_panels.Add(new_user_panel);
splitContainer2.Panel1.Controls.Add(new_user_panel);
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
int aantal_panels = 0;
foreach (Panel panel in user_email_in_panels) {
aantal_panels++;
}
int counter_1 = 0;
foreach (Panel panel in user_email_in_panels) {
if(counter_1 == (aantal_panels -1)){
MessageBox.Show("We are here");
panel.Controls.Add(new_item);
splitContainer1.Panel1.Controls.Add(panel);
}
counter_1++;
}
But somehow, i don't see any labels shown in the form... am i missing something? The messsagebox with the text We are Here is shown, so it come's to the add statement....
and i've got an another question besides my first question. My question is, how can i make the counter of the list better?
### Edit for Sean Vaughn
i've updated it to this:
public class MyPanelClass {
public string Name {
get;
set;
}
public bool Visible {
get;
set;
}
public string YourLabelsText {
get;
set;
}
}
Label new_item = new Label();
new_item.AutoSize = true;
new_item.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
string new_item_text = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);
new_item.Text = new_item_text;
Panel base_panel = new Panel(); //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>(); //this will keep records of all of your panels.
MyPanelClass panel_name = new MyPanelClass();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0]; ;
panel_name.Visible = true;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2], user_credentials[counter_snel][9]);
//Now add the new created panel to the list.
myPanelList.Add(panel_name);
base_panel.Name = myPanelList[counter_snel].Name;
base_panel.Visible = myPanelList[counter_snel].Visible; //You probably don't need this because base_panel will always be visible
new_item.Text = myPanelList[counter_snel].YourLabelsText;
But i still doesn't see anything...
Does it matter that it execudes in an public void??? i don't think so, but it want to elliminate all the possibilities...

Use a List that will keep track of all the panels.
List<Panel> myPanels = new List<Panel>();
Whenever you need to add a panel, do this:
mypanels.Add(yourPanel);
Now, for the other problem, create a function like this:
private void HideAllOtherPanels(List<Panel> panelList, Int index /*the index of the panel you want to show*/)
{
foreach(Panel panel in panelList)
if(panel.Visible) panel.Hide();
panelList[index].Show();
}
I see you are creating a lot of new panels. My advice, don't do that, you're giving a lot of load to the system.
Instead make a class.
public class MyPanelClass
{
public string Name
{
get; set;
}
public bool Visible
{
get; set;
}
public string YourLabelsText
{
get; set;
}
}
After creating the class, create a base_panel on your form that will change it's contents based on the your intentions.
And then, all you need to do is change the panel's content rather than creating a new Panel. Store the other panels data in a List<MyPanelClass>.
So you'll be doing this:
Panel base_panel = new Panel(); //this is your base panel, you don't need to add this line of code because Visual Studio will do that for you.
List<MyPanelClass> myPanelList = new List<MyPanelClass>(); //this will keep records of all of your panels.
MyPanelClass panel_name = new MyClassPanel();
panel_name.Name = "email_in_" + user_credentials[counter_snel][0];;
panel_name.Visible = false;
panel_name.YourLabelsText = string.Format("{0,0}\n{1,0}\n{2,0}", user_credentials[counter_snel][1], user_credentials[counter_snel][2],user_credentials[counter_snel][9]);
//Now add the new created panel to the list.
myPanelList.Add(panel_name);
Now whenever you need to activate a stored Panel, just do this:
base_panel.Name = myPanelList[yourPanelsIndex].Name;
base_panel.Visible = myPanelList[yourPanelsIndex].Visible; //You probably don't need this because base_panel will always be visible
yourLabel.Text = myPanelList[yourPanelsIndex].YourLabelsText;
This code is much less bulky on the machine and is easy to handle too.

I'd try something like this to add an item.
var panel = splitContainer2.Controls.FirstOrDefault(p => p is Panel && ((Panel)p).Name == panel_name);
if(panel != nul)
{
panel.Controls.Add(new_item);
}
To hide a particular panel given panel_name:
foreach(var control in splitContainer2.Controls)
{
if(control is Panel)
{
((Panel)control).Visible = ((Panel)control).Name == panel_name;
}
}

Related

How to disable labels and lines from pie chart?

actually I'm trying to remove the labels and the line which are leading to the labels. The app is in C# and I used OxyPlot to plot the piechart.
Here is my code. As you can see all what I've tried doesn't helped me.
Thanks for your help!
public class MyViewModel
{
public PlotModel MyModel { get; set; }
public MyViewModel()
{
SingletonItem singletonItem = SingletonItem.Instance;
PieSeries pieSeries = new PieSeries();
pieSeries.Slices.Add(new PieSlice("Done", singletonItem.Done) {IsExploded = true, Fill = OxyColors.PaleVioletRed});
pieSeries.Slices.Add(new PieSlice("Undone", singletonItem.Undone) {IsExploded = true});
pieSeries.LabelField = "";
pieSeries.TickDistance = 0;
pieSeries.ValueField = "";
MyModel = new PlotModel();
MyModel.IsLegendVisible = false;
MyModel.Series.Add(pieSeries);
}
}
Screenshot of the actually page
I solved this problem by myself. For everyone who need the same information for me these lines helped:
pieSeries.OutsideLabelFormat = "";
pieSeries.TickHorizontalLength = 0.00;
pieSeries.TickRadialLength = 0.00;

How can i add feature to custom Layer in Dotspatial?

I need to add Feature type as point , line or polygon to custom layer that user select layer name in combo box.
At the moment , every Feature just insert in the last layer made
This is my code :
public void test(Coordinate coord,string ftype)
{
foreach (var item in map1.Layers)
{
if (item.LegendText ==ftype)
{
int selectedIndex = map1.Layers.IndexOf(item);
((Legend)map1.Legend).ClearSelection();
map1.Layers[selectedIndex].IsSelected = true;
map1.Layers.SelectedLayer = map1.Layers[selectedIndex];
map1.Legend.RefreshNodes();
label1.Text = Convert.ToString(selectedIndex);
}
}
DotSpatial.Topology.Point point = new DotSpatial.Topology.Point(coord);
IFeatureSet ifs = new FeatureSet(FeatureType.Point);
IFeature currentFeature = ifs.AddFeature(point);
DotSpatial.Symbology.CharacterSymbol pcs = new DotSpatial.Symbology.CharacterSymbol('o', "Webdings", Color.Black, 32);
DotSpatial.Symbology.PointCategory pc = new DotSpatial.Symbology.PointCategory(pcs);
pc.Symbolizer.ScaleMode = ScaleMode.Simple;
pointScheme.AddCategory(pc);
mpl.Symbology = pointScheme;
mpl.ApplyScheme(pointScheme);
_tempLayer = mpl;
map1.MapFrame.DrawingLayers.Add(mpl);
map1.MapFrame.Invalidate();
map1.Invalidate();
pointID++;
map1.FunctionMode = FunctionMode.None;
}`
I found it
We should to Change Mappointlayer
first found indexof layer
selectedIndex = map1.Layers.IndexOf(item);
then set it on map point layer
mpl= (MapPointLayer)map1.Layers[selectedIndex];

Clearing controls from a FlowLayoutPanel

I'm trying to remove the controls of the FlowLayoutPanel in the code that is attached below. The problem is that I use a search function in the same frame, so I can't find other objects without clearing it first. I already tried:
fPanelUpperMainScreen.Controls.Remove(a);
This is my method that I am working on.
public void GetArtistLayout()
{
ArtistInformation a = new ArtistInformation();
fPanelUpperMainScreen.Controls.Add(a);
int valueInt = int.Parse(tBMainScreen_Search.Text);
a.pictureBox1.ImageLocation = ar.GetArtist(valueInt).artistPic;
a.lblArtistInformation_ArtistName.Text = ar.GetArtist(valueInt).artistName;
var reviews = rr.getMatchingReviewsArtist(valueInt);
foreach (var review in reviews)
{
UserControl1 u = new UserControl1();
u.lblUser.Text = review.username;
u.lblComment.Text = review.comments;
u.lblDate.Text = review.date;
u.lblRating.Text = review.rating.ToString();
a.fpArtistInformation_Reviews.Controls.Add(u);
}
}

file upload fields get the empty value

here is the my problem.I am adding fileuploadfields dynamically and add each fileupload fields to the collection something like this
static List<FileUploadField> fuploadcollection = new List<FileUploadField>();
,so far so good .but when I tring to upload images inside the fileuploadfield,I getting empty fileupload field.but it seems filled "fuploadcollection" collection when I watch it by breakpoint.
here is the my code ;
addfileupload function;
public partial class UserControl1 : System.Web.UI.UserControl
{
static int? _currentflag = 0;
static List<FileUploadField> fuploadcollection = new List<FileUploadField>();
........
........
.........
protected void addImages_Click(object sender, DirectEventArgs e)
{
int? nn = null;
X.Msg.Alert("hello", "hello").Show();
_currentflag = (!string.IsNullOrEmpty(txtcurrentflag.Text)) ? Convert.ToInt32(txtcurrentflag.Text) : nn;
FileUploadField fileUploadField = new FileUploadField()
{
ID = "FileUploadField" + _currentflag,
Width = 280,
Icon = Icon.Add,
ClientIDMode = ClientIDMode.Static,
Text = "Göz at",
FieldLabel = "Resim-" + _currentflag,
Flex = 1,
ButtonText="resim seç"
};
fileUploadField.AddTo(this.pnlResim, true);
if (string.IsNullOrEmpty(txtcurrentflag.Text) || txtcurrentflag.Text == "0")
{
fuploadcollection.Clear();
}
fuploadcollection.Add(fileUploadField);
_currentflag++;
txtcurrentflag.Text = _currentflag.ToString();
}
here is the this part which give the error(coulnt enter the if statement)
* but it seems fuploadcollection filled (forexample count of it shows more then 1 )
foreach (var item in fuploadcollection)
{
if (item.HasFile)
{
string resimadi = UploadImage.UploadImages(item);
If you create dynamic controls you should recreate them during each request. There are some links, which could be helpful:
http://forums.ext.net/showthread.php?19315
http://forums.ext.net/showthread.php?19079
http://forums.ext.net/showthread.php?9224
http://forums.ext.net/showthread.php?16119
http://forums.ext.net/showthread.php?23402

How to get values from dynamically generated controls in asp.net c#?

I know this is a well asked question and I found some marked as answers but those doesn't solve my problem. Please have a look at my codes..
Method to Display dynamic controls
private void ShowControlsByFormId()
{
List<FormControlsBO> list = new List<FormControlsBO>();
list = new FormControlsDA().FormControls_GetByFormId(Convert.ToInt32(ddlForm.SelectedValue.ToString()));
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
DynamicControl dynamicControl = CommonUtility.GenerateControl(list[i]);
pnlInput.Controls.Add(new LiteralControl("<tr><td>"));
pnlInput.Controls.Add(dynamicControl.GeneratedControlLiteral);
pnlInput.Controls.Add(new LiteralControl("</td><td></td><td>"));
pnlInput.Controls.Add(dynamicControl.GeneratedControl);
pnlInput.Controls.Add(new LiteralControl("</td><tr><br/><br/>"));
}
pnlAction.Visible = true;
}
else
{
pnlAction.Visible = false;
}
}
Method to Generate dynamic controls
public static DynamicControl GenerateControl(FormControlsBO bo)
{
DynamicControl dynamicControl = new DynamicControl();
Control control = new Control();
LiteralControl literal = new LiteralControl();
switch (bo.FieldType)
{
case "TextBox":
control = new TextBox();
control.ID = bo.FieldName;
literal.Text = bo.FieldLabel;
break;
case "RadioButton":
control = new RadioButton();
control.ID = bo.FieldName;
literal.Text = bo.FieldLabel;
break;
case "CheckBox":
control = new CheckBox();
control.ID = bo.FieldName;
literal.Text = bo.FieldLabel;
break;
case "DropDownList":
control = new DropDownList();
control.ID = bo.FieldName;
literal.Text = bo.FieldLabel;
break;
}
control.ClientIDMode = ClientIDMode.Static;
dynamicControl.GeneratedControl = control;
dynamicControl.GeneratedControlLiteral = literal;
return dynamicControl;
}
Method to Save data
private void FormRecords_Save()
{
List<FormControlsBO> list = new List<FormControlsBO>();
FormControlsBO bo = new FormControlsBO();
foreach (Control ctl in pnlInput.Controls)
{
CommonUtility.DataFiller(bo, ctl);
list.Add(bo);
}
Boolean result = false;
result = new FormControlsDA().FormRecords_Save(list);
if(result == true)
{
lblMessage.Text = "Form data saved successfully";
}
else
{
lblMessage.Text = "Form data not saved";
}
}
The problem is, when I debug the code, pnlInput.Controls shows Zero count. Please help !!
As I already answered here all dynamic controls should be reinitiated in Page's Init event, as viewstate manager values are set every request. You can check page's lifecycle.
So right now, when you do not create you control in init event, viewstates misses them while it is setting postback data, and when you do try to get values, they are equal to zeros.
Keep in mind, that you have to create the same control types with the same names.
If you have written the code to generate Dynamic Controls and if it is in the page load event use FindControl("IdofControl"); to retrive its value;
For Example,
TextBox txtInstance = (TextBox)FindControl("IdofControl");
string txtvalueinTextBox =txtInstance.Text;
Make sure that the controls are dynamically generated in all page reloads.If the controls generated on the postback is different the viewState may not restore back properly.

Categories

Resources