Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following : var navitems = new Button[5] .. Now how do I add to the array? I keep getting a null reference at runtime. To add, i am doing the following in a for loop :
for(int i=0;i<6;i++)
{
button b=new Button();
navitems[i]=b;
}
Note : No need to worry about how the buttons will be formatted, I already have that covered.
Fine here is the actual code, didn't wanna give too much away..
var groups = Connection.Groups();
var navitems = new TileNavItem[5];
for (int i=0;i > groups.Count; i++)
{
TileNavItem item = new TileNavItem()
{
Caption = groups[i].Description,
TileText = "Dashboards"
};
navitems[i] = item;
}
I'm using devexpress trial and i would like to create my tilenavpane items dynamically.. If i do the following navitems.Items.AddRange(new TileNavItem[] { item1, item2, item3 });, it works great so I figured I could easily implement this dynamically.
You should define your array the correct size for your items, and be more careful with your for-loops:
var groups = Connection.Groups();
var navitems = new TileNavItem[groups.Count];
for (int i=0; i < groups.Count; i++)
{
navitems[i] = new TileNavItem
{
Caption = groups[i].Description,
TileText = "Dashboards
};
}
Note that I also removed a superfluous variable.
This code may still fail if Connection.Groups can return null items.
You need increase array size
var navitems = new Button[6]
of decrease loop
for(int i=0;i<5;i++)
{
button b=new Button();
navitems[i]=b;
}
You can add TileNavItem(s) to the collection at run time after adding the initial 5 (not tested):
TileNavItem item6 = new TileNavItem() { TileText = "Item 6" };
navitems.Items.Add(item6);
https://documentation.devexpress.com/WindowsForms/18127/Controls-and-Libraries/Navigation-Controls/TileNav-Pane/How-to-Create-and-Customize-the-TileNavPane-Control-in-Code
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need fill a ComboBox (Windows Forms) where have a Text and ID values. Example:
("Team1", 15)
("Team2", 27)
...
My code didnt work :/
List<Team> teams = new List<Team>();
teams = sq.loadTeams();
foreach (Team t in teams){
Combobox.Items.Add(t.getName(), t.getId());
}
heeelp please
Use data-binding, best feature Windows Forms come up with ;)
var list = new[]
{
new Team { Id = 1, Name = "One" },
new Team { Id = 2, Name = "Two" },
new Team { Id = 3, Name = "Three" }
};
combobox.ValueMember = "Id"; // Name of property to represent a Value
combobox.DisplayMember = "Name"; // Name of property to represent displayed text.
combobox.DataSource = list; // Bind all items to the control
Selections can be accessed by Selected.. properties of combobox.
var selectedTeamId = (int)combobox.SelectedValue;
var selectedTeamName = combobox.SelectedText;
var selectedTeam = (Team)combobox.SelectedItem;
Notice that SelectedValue and SelectedItem return object type, so you need cast it to correct type before using.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am using for loop to generate some worksheets, and I want to give each one a unique name. All I get is Sheet1, Sheet2, Sheet3, and so on.
Below is my code:
var package = new ExcelPackage();
for (var i = 0; i < ds.Tables.Count; i++)
{
var ws = package.Workbook.Worksheets.Add(String.Format("Sheet{0}", i));
ws.Cells["A1"].LoadFromDataTable(i == 0
? Transpose(ds.Tables[i].Copy()).DefaultView.ToTable()
: ds.Tables[i], true, TableStyles.Medium1);
ws.Cells[ws.Dimension.Address].AutoFitColumns();
ws.Cells["A:J"].Style.Numberformat.Format = "#,##0";
}
You haven't shown where the names are coming from, but probably the best way would be to use the name from some field in the same data source that you're using to populate the sheet.
Here's one one way to give them each unique names would be to have them stored in a list, which you can access using the same index that you're using currently. Of course you have to somehow ensure that the names are in the correct order in your list:
var sheetNames = new List<string> { "Summary", "EmployeeData", "Benefits" };
for (var i = 0; i < ds.Tables.Count; i++)
{
// Choose a name from the list or use 'Sheet1, 2, 3' if we don't have enough names
var sheetName = i < sheetNames.Count
? sheetNames[i]
: String.Format("Sheet{0}", sheetNames.Count - i);
var ws = package.Workbook.Worksheets.Add(sheetName);
In this line of code here:
var ws = package.Workbook.Worksheets.Add(String.Format("Sheet{0}", i));
You're setting the worksheet name, that's why you end up with 'Sheet1', 'Sheet2', etc.
Changing that to something else, you get your worksheets named differently.
Now, I'm not sure, if your problem is where you can change the name or how you can do it, to make it unique. Depending on what you're aiming for, you could use Guid's.
This question already has an answer here:
Entity Framework, get list for database first case
(1 answer)
Closed 5 years ago.
I have a program in C# (Visual Studio) and part of the code looks like this:
Class 1:
private void part_TextChanged(object sender, EventArgs e)
{
List<string> parameters = part.selectedParameters;
// Lots of code
List<string> allParameters = parameters.ToList();
for (int k = 0; k < limit; k++)
{
allParameters.Add("text");
}
}
Class 2:
public bool DoAnalysis()
{
List<string> partParameters = part.selectedParameters;
// Lots of code
List<string> allPartParameters = partParameters;
for (int k = 0; k < limit; k++)
{
allPartParameters.Add("text");
}
}
My question is concerning the use of ToList(). I want to copy List1 to List2 and working with List2 without List1 being changed too. Applying this to my code, I used .ToList() to deal with this. Adding ToList() was not a problem in Class 1 --> parameters.ToList() but when I want to add it in Class 2 to List<string> allPartParameters = partParameters, it is not possible. I only get the options ToArray() and ToString().
So, why can I add ToList() to a List in one part of the code and not in the other? Am I missing some property dependency somewhere else in the code?
Usually with dependency problems you can right click the red highlighted code and choose "Quick Actions and Refactoring" then a list will come up of dependencies you can possibly use. This will save you time typing it and has helped me tons.
I found this question but it's being used with an XML file so I don't really understand what is going on.
What I want to do is get my list of objects to get populated in my for loop. Right now I have this:
for (int i = 0; i < dogs.Length; i++)
{
dogs[i] = new Dog();
}
dogs[0].PictureBox = picDog0;
dogs[1].PictureBox = picDog1;
dogs[2].PictureBox = picDog2;
dogs[3].PictureBox = picDog3;
I want to do something like this:
for (int i = 0; i < dogs.Length; i++)
{
dogs[i] = new Dog();
dogs[i].PictureBox = StringToVariable("picDog" + i);
}
PictureBox is a property field in case that makes a difference.
StringToVariable() is the thing I don't know about. I don't even know what it would be called to search for it.
It's impossible to say for sure without a good, minimal, complete code example. But I would expect that the following statement should work in your scenario:
dogs[i].PictureBox = (PictureBox)Controls.Find("picDog" + i, true)[0];
That will search the children of the current control (which I assume in this case is your Form subclass) for each control in turn. This is somewhat inefficient, as it has to search the controls collection for each item, but as long as you have a relatively small number of items, this is likely not a problem.
Depending on how your Form is set up, the following might also work:
string prefix = "picDog";
foreach (PictureBox pictureBox in Controls.OfType<PictureBox>())
{
if (pictureBox.Name.StartsWith(prefix))
{
int index;
if (int.TryParse(pictureBox.Name.Substring(prefix.Length), out index))
{
dogs[index] = pictureBox;
}
}
}
That version inspects each child control just once, attempting to parse an index appended to the initial text of "picDog", and if it's successful, using that index to assign to your array directly. This has the advantage of scaling well to larger lists of controls, but may be overkill in your case.
Note that in both of the above examples I've left out any error checking. In either example, you would probably want to add some kind of handling in case (for the first example) the desired control couldn't be found, or (for the second example) if you find a control for which you can't parse the index, or fail to fill in one of the elements of the dogs array.
If for some reason neither of the above examples seem to work for you, please edit your post so that it includes a better code example.
Sometimes a simple solution can work well. How about this?
var picDogs = new [] { picDog0, picDog1, picDog2, picDog3 };
for (int i = 0; i < dogs.Length; i++)
{
dogs[i] = new Dog();
dogs[i].PictureBox = picDogs[i];
}
You could even do this:
var dogs = new [] { picDog0, picDog1, picDog2, picDog3 }
.Select(picDog => new Dog() { PictureBox = picDog })
.ToArray();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a list and I have populated it with myObjects. I do processing with the the objects and then I would like to have the objects garbage collected by releasing references to the objects. How would this be best acheived?
I can't use foreach loop because you can't alter the collection within the loop.
This updates the list, and doesn't create a new list with new contents.
for (i = 0; i < list.Count; i++) {
list[i] = null;
}
You basically want to iterate through the list, and assign each object to null, like so:
var myList = new List<object>();
for (var i = 0; i < 9; i++)
{
myList.Add(new object());
}
for (var i = 0; i < myList.Count; i++)
{
myList[i] = null;
}
Use the List(T).Clear() method: http://msdn.microsoft.com/en-us/library/dwb5h52a.aspx
This dereferences all objects and sets Count to 0.
If you really need to keep the same count and set each item in your list to null, you can simply use ForEach():
list.ForEach(x => x = null)