I think it is a small problem but I can't find my mistake.
I create a Form called Inventurbeleg which contains a ComboBox called cbProduktBox.
With a Controller-Class I create an Object of the Form. Now I want to add Items with the create-Methode.
public static void buttonCreate()
{
inventurbeleg = new Inventurbeleg();
create();
inventurbeleg.Show();
}
My ComboBox gets Items from an array:
public static void create()
{
inventurbeleg.cbProduktBox = new ComboBox();
for (int j = 0; j < Program.arrayMatNr.GetLength(0); j++)
{
String item = Program.arrayMatNr[j, 1];
inventurbeleg.cbProduktBox.Items.Add(item);
}
}
This works correctly, cbProduktBox contains all Items. My Problem is, that the Items arn't shown at my Form. There is an empty comboBox.
You can't do it like that, take a look at this line:
inventurbeleg.cbProduktBox = new ComboBox();
You're creating a new combobox, and when the form loads, the cbProduktBox will initialize again and the changes will be gone
Maybe you can move the create method inside the new form, so when the form loads, call the create method.
Related
As I was trying to learn how to use multiple windows in WPF, I stumbled upon a problem. I have a MainWindow containing all the logic for a blackjack game. Now I wanted a second Window with the last 10 games played by the user.
These last 10 games were stored in
public static ListBoxItem[] historiekArray = new ListBoxItem[10];
In the 'Speler' class (Player in Dutch). Now when I try to populate the HistoryWindow Listbox, everything goes well the first time, but the second time I open up the window this error occurs:
System.InvalidOperationException: 'Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.'
This is the C# code in the HistoryWindow.xaml.cs
public HistoriekWindow()
{
InitializeComponent();
// empty the listbox
LbxHistoriek.Items.Clear();
// Populate listbox with array items
for (int i = 0; i < Speler.historiekArray.Length - 1; i++)
{
ListBoxItem temp = new ListBoxItem();
temp = Speler.historiekArray[i];
LbxHistoriek.Items.Add(temp);
}
}
Looking at other posts on StackOverflow and learn.microsoft, I thought the problem might be that my ListBoxItem was being reused, but after placing ListboxItem temp = new ListboxItem() inside the for-loop, nothing much changed.
SOLUTION
The array of ListboxItems was changed into an Array of strings.
public static string[] historiekArray = new string[10];
And the for-loop sets the .Content of the ListBoxItem to the corresponding Array item.
for (int i = 0; i < Speler.historiekArray.Length - 1; i++)
{
ListBoxItem temp = new ListBoxItem();
temp.Content = Speler.historiekArray[i];
LbxHistoriek.Items.Add(temp);
}
Please excuse my formatting, this is my first post on StackOverflow and English is not my native language.
So I do have a WinForm in my Programm, which contains a series of each a ComboBox and two TextBoxs. There are atm 8 Lines, but this will increase to a total of at least 32, therefore I would like to work with an Array or similar. How do I do that?
My current working, method is that a create a new array of TextBoxes/ComboBoxes which I assign the designated Elemt of the WinForm, manually. Therefore I have a list like this:
tbGU[0] = tbGU1;
tbGO[0] = tbGO1;
cbS[0] = cbS1;
Of course, this looks awful and isn't great if it's copied many times. Anyone got a Solution to my Problem?
I need to access the SelectedIndex of the ComboBox and the Text of the TextBoxes.
I was hoping that I could avoid having to create all the Elements manually by code.
One simple solution is to use the array initializer syntax:
ComboBox[] cbS = new[] { cbS1, cbS2, cbS3 ... };
Another way of doing this would be to get rid of the variables cbS1, cbS2 ... cBSn altogether and create the controls in a for loop.
ComboxBox[] cbS = new ComboBox[32];
// declare the text box arrays here as well
for (int i = 0 ; i < cbS.Length ; i++) {
cbS[i] = new ComboBox();
cbS[i].Location = ... // use "i" to help you position the control
// configure your combo box ...
this.Controls.Add(cbS[i]);
// do the same for the text boxes.
}
A third way is to create a custom control:
// name this properly!
public class MyControl: UserControl {
public ComboBox CbS { get; }
public TextBox TbGU { get; }
public TextBox TbGO { get; }
public MyControl() {
// create and configure the combo box and text boxes here ...
}
}
Then you can use a for loop to create lots of MyControls.
I want to write some calendar program. I need buttons in a number of days in a chosen month. I have container called "calendarPanel" and used this method to add the buttons
public void CreateCalendar(DateTime dt)
{
int numberOfDays = DateTime.DaysInMonth(dt.Year, dt.Month);
for (int i = 0; i < numberOfDays; i++)
{
System.Windows.Controls.Button btn = new Button();
calendarPanel.Children.Add(btn);
}
}
It works only added to MainWindow.xaml.cs, but I want to use that method from an instance of my class CalendarManager.cs. My problem is that I don't know how (is it possible anyway?) to reference to my panel in MainWindow from outer class (but still in the same namespace). I cannot use
calendarPanel.Children.Add(btn);
Because it doesn't see/know about "calendarPanel" element.
I have a bunch of different ComboBoxes that were made with the windows form Designer I would like to be able to access them by index, so that I could do something like this:
for (int i = 0; i < numOfBoxes; i++)
{
ComboBoxes[i].visible = false;
}
I tried putting them in an array of ComboBoxes, but this creates an array of nulls.
private ComboBox[] ComboBoxes;
public MainForm()
{
ComboBoxes = new ComboBox[] {ComboBox1, ComboBox2, ComboBox3};
}
What's the right way to do this?
You need to make sure you make your array of ComboBox after InitializeComponent is called.
private ComboBox[] ComboBoxes;
public MainForm()
{
InitializeComponent();
ComboBoxes = new ComboBox[] {ComboBox1, ComboBox2, ComboBox3};
}
Prior to this all of the windows form designer objects will be null because they are only first instantiated in InitializeComponent.
I'm a C# student and I'm a little stuck at on my midterm project.
I dropped my project and spec here: https://www.dropbox.com/sh/eo5ishsvz4vn6uz/CE3F4nvgDf
If you run the program, it will come to the last area I left off at..
private void btnAddScore_Click(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList = tempScore; (was Form1.scoreList[i] = tempScore;)
}
txtScoresList.Text += Convert.ToString(tempScore) + " ";
}
There's a main form, a secondary add form, and a third and fourth form, all the controls are in place, just the wiring is what's left over.
(1) In the above code, there are supposed to be 3 scores passed to the main form, which, along with a student name string, are to populate the ListBox on the main form. I can't figure out how to access that ListBox, anytime I type "listStudents" nothing happens.
(2) I'm also not sure how to limit an input of only 3 scores when I'm clicking the "add" button 1 time, which means I know my for loop is probably completely wrong. I don't know if I should save those scores to an array, list, or individual vars, being that it can be 3 (or more, but 3 is fine) scores.
(3) When I hit "OK" on the AddNewStudent form, do I write my code there to populate the main form ListBox, or does it go in the main form?
Update:
private void Form1_Load(object sender, EventArgs e)
{
lbStudents.Items.Clear();
//something like
foreach (decimal i in scoreList2)
{
scoreList = scoreList2.ToString(); //gives me a cannot implicitly convert error
}
lbStudents.Items.Add(tempInfo1 + " " + scoreList2);
}
//I want the listbox to populate like "Name - |100| |90| |80|"
This code seems to me, to be correct, for getting the ListBox populated, but I'm unsure of how to add the entire contents of the list to a string, and then add that to the listbox.
This will get your code building and running.
Change the following declaration in form1
public static decimal[] scoreList = new decimal[3];
to
public static List<decimal> scoreList = new List<decimal>();
and update your btnAddScore_Click handler to
//save scores to temp static var, populate noread txtbox txtScoresList with scores
for (int i = 0; i < 3; i++)
{
//save score to static var for trans-form data sending
tempScore = Convert.ToDecimal(txtScore.Text);
Form1.scoreList.Add(tempScore);
}
The rest is not too difficult, you should be able to work it out.