I have 56 comboBox in my program and I need to fill all of them with the same information. The fastes way that I found is creating a private function for fill the comboBox. And I put 56 times the same function.
But my question is... I can do a loop for fill this 56 comboBox?
Assuming this question is about Windows Forms. The best way to do it is probably to have the similar naming convention for all ComboBox controls you're willing to fill with the same data. Fill them with items within for-loop, adding different suffix to the control you want to find before adding data.
// Lets say you have 56 ComboBox controls with names like : cbMyComboBox_1, cbMyComboBox_2, ..., cbMyComboBox_56
for (int i = 1; i <= 56; i++)
{
ComboBox comboBox = (ComboBox)this.Controls.Find
(string.Format("cbMyComboBox_{0}", i), true)[0];
ComboBoxFill(comboBox);
}
private void ComboBoxFill(ComboBox comboBox)
{
// Fill that ComboBox with data here
}
Of course you can.
foreach (var c in Controls)
{
if (c is ComboBox)
((ComboBox)c).Text = "I'm a combobox~";
}
try adding the same class to all 56 combobox, and create a function to populate then like:
$(".class").html(value of the options);
Related
I want to select a number of files using OpenDialog and display them in a listbox. If I have more file paths than can fit in the listbox, I'd like the list box to automatically grow vertically to fit all of the filenames.
How can this be done, please? Thank you for your help.
foreach (string FileName in oOpenDialog.FileNames)
{
//lstbx_Box1.IntegralHeight = false; //This doesn't auto-grow the list box.
lstbx_Box1.Items.Add(Path.GetFullPath(FileName));
}
Yep you can, since the height of listbox when having a single value is 21 you can add 21 for the height of listbox when a new item is added.
foreach (string FileName in oOpenDialog.FileNames)
{
lstbx_Box1.Height += 21;
lstbx_Box1.Items.Add(Path.GetFullPath(FileName));
}
I have a form frmControlSystem that connects to an exterior data source from which I receive strings that go into an array. This form opens another form frmResult_Vote to display that information.
The 2nd form is passed the string array as a parameter along with an integer of how many items to display and one other parameter. This 2nd form will properly enable the .Visible for the needed textBoxes. but only the first textBox will actually show text in it; the others are blank.
The first form uses this code to display the 2nd form:
frmResult_Vote results = new frmResult_Vote(memberDisplayName, iMemberQty, ConstMembersPlusOne);
results.Show()
The second form creates an array of textBoxes so I can loop and make visible only the ones that I want to use as well as populating the .Text attribute. This code is:
public frmResult_Vote(string[] myVoteMembers, int iMemberQty, int ConstMembersPlusOne)
{
InitializeComponent();
textBox1.Text = "FU";
TextBox[] memberTextBoxes = new TextBox[ConstMembersPlusOne];
memberTextBoxes[1] = txtDisplayName1; memberTextBoxes[2] = txtDisplayName2; memberTextBoxes[3] = txtDisplayName3; memberTextBoxes[4] = txtDisplayName4;
memberTextBoxes[5] = txtDisplayName5; memberTextBoxes[6] = txtDisplayName6; memberTextBoxes[7] = txtDisplayName7; memberTextBoxes[8] = txtDisplayName8;
memberTextBoxes[9] = txtDisplayName9; memberTextBoxes[10] = txtDisplayName10; memberTextBoxes[11] = txtDisplayName11; memberTextBoxes[12] = txtDisplayName12;
for (int i = 1; i <= iMemberQty; i++)
{
memberTextBoxes[i].Text = myVoteMembers[i];
memberTextBoxes[i].Visible = true;
}
}
When the 2nd form displays, the proper quantity of textBoxes will enable, but only the first one will show text.
The second form has 12 textBoxes on it named txtDisplayName1 thru txtDisplayName12. These are what I am using to populate the TextBox array.
Last bits of oddity (at least to me): Every other textBox on the second form was drawn with a different BackColor. That never shows when the first form show() the second form; they all show with the color of white.
In trying to get something to show in the other textBoxes, I have also tried to specifically call by the non-array names like this:
txtDisplayName1.Text = myVoteMembers[1];
This didn't do anything at all.
I have also placed a random textBox on the second form and tried to populate it with this line:
textBox1.Text = "FU";
that textBox also would not populate... but if I comment out the for loop, that textBox1.Text assignment will work. It is called before everything else, as well.
I have verified that the array myVoteMembers has strings in it and when I step through the code I can see the assignment happening here:
memberTextBoxes[i].Text = myVoteMembers[i];
What am I missing here?
You have an exception - possibly here:
for (int i = 1; i <= iMemberQty; i++)
{
memberTextBoxes[i].Text = myVoteMembers[i];
memberTextBoxes[i].Visible = true;
}
myVoteMembers array will start from 0 and have iMemberQty number of items. Which means 0-iMemberQty-1 indexes. While you are accessing iMemberQty'th index which throws Index out of Bound Exception
my winForms app has a tab control which consists of two tabs (tab1 & tab2). In tab2 data is fetched in a datagridview fron a database(Product infomations).
In tab1, I've a combobox [sales analyse]which makes a user to select an option.
I now want to get access to tab2 from tab1 on cb selection, displaying me a regional sales information from the data in tab2 datagrid.
Is it possible? I don't really know wher to start
tab1 image
tab2
Expectation:
if the combobox in tab1 is selected, it should then look through the datagridview in tab2 where the (regions) North, East, West ect are and then sum the sale 13, sales 14 .. and display in the textBoxes respectively.
As your controls all sit in one Form their methods can all reference each other without any additional help.
So you can write in the SelectedIndexChanged of the ComboBox cbAnalyse
cbAnalyse_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbAnalyse.SelectedItem.ToStringndex == "Sales Analysis"
{
someTextbox1.Text = ColumnSum(yourDataGridView, someColumn1) + "$";
someTextbox2.Text = ColumnSum(yourDataGridView, someColumn2) + "$";
}
This uses a small helper function, which sums up all values from one column in a DataGridView:
decimal ColumnSum(DataGridView dgv, int columnIndex)
{
decimal sum = 0m;
for (int row = 0; row < DGV.Rows.Count; row++)
if (DGV[columnIndex, row].Value != null) sum += Convert.ToDecimal(DGV[1, row].Value);
return sum;
}
Folks often run into problems when they need to refer to controls that are not sitting in either the same Form but in a 2nd, 3rd etc Form. Or when they are part of a Usercontrol, which is a custome container for holding controls.
In both cases those controls are by default private members of the other Forms or of the UserObject.
In these cases one needs to create some kind of public accessor to them, usually by a Property. And in the case of Forms, one also need to provide a reference to the other forms, often stored when opening them.
In this case, the 2nd Form often also needs a back-refrence to the 1st Form; this is often pass in in the constructor.
But in your case none of these complications matter. All you need is the patience to wire up all those TextBoxes ;-)
Update: As you also seem to have a problem getting the intermediate sums and need to allow for repeating regions in the rows of Tab 2, you also want to use a function that will calculate with a where clause:
decimal ColumnSumWhere(DataGridView dgv, int columnIndex, string region)
{
decimal sum = 0m;
for (int row = 0; row < DGV.Rows.Count; row++)
if (DGV[columnIndex, row].Value != null) &&
(DGV[regionColumn, row].Value.ToString() == region)
sum += Convert.ToDecimal(DGV[1, row].Value);
return sum;
}
If I got it right, whenever you change the value in sales analyze combo, the tab page containing data grid should should be activate.
You can set the selected index of the tab control to the the data grid tab, and it should work
this.tabControl1.SelectedIndex = 1;//Index of data grid tab
I have, in a form, consecutive textboxes and labels named tb1,tb2,tb3... and label1,label2,label3....
I have a dictionary holding number of Key value pairs.
How to populate the labels and textboxes corresponding to the value pairs in the dictionary?
Eg: dic.Key[1] -> label1 and dic.value[1] to tb1... like that.
I don't get any idea to try this.
In the other answer it is suggested to create a collection of labels and textboxes. My concern with that approach is that a developer may forget to do that or the order may get changed.
Every control has the Name property which stores that control's name. This property is set by Visual Studio. In your code, if you are not playing (read changing) with the Name property of controls, you can use the below code to achieve what you wanted to.
for(int i = 0; i < dic.Count; i++)
{
// As Control.Find returns an array of controls whose name match the specified string,
// in this example I had picked the first control
// you can make it more robust by checking
// - the number of controls returned,
// - the type of control, etc
TextBox txt = (TextBox) this.Controls.Find("tb" + (i + 1).ToString(), true)[0];
Label lbl = (Label) this.Controls.Find("label" + (i + 1).ToString(), true)[0];
txt.Text = dic[i].Value;
lbl.Text = dic[i].Key;
}
Your best bet would simply be to initialize a List (or two) in your form's constructor, putting all your labels and text boxes inside, so you can check them while looping through your dictionary.
private List<Label> labels = new List<Label>();
private List<TextBox> textBoxes = new List<TextBox>();
public MyForm()
{
labels.Add(myLabel1);
labels.Add(myLabel2);
labels.Add(myLabel3);
textBoxes.Add(myTB1);
textBoxes.Add(myTB2);
textBoxes.Add(myTB3);
}
private void addValuesFromDictionary(Dictionary<string, string> dic)
{
for (int i = 0; i < dic.Count; i++)
{
labels[i].Text = dic[i].Key;
textBoxes[i].Text = dic[i].Value;
}
}
i use this code to add a numbers to combobox
for (int i = 15; i < 250; i++)
{
cbSumFrom.Items.Add(i);
}
the problem is that i get something like
100
101
......
but i want like
15
16
17
......
how to fix it ?
The problem is is that it appears the combo box is sorting the item and it's doing an ASCII comparison on each character to do it, so 100 comes before 15 because 10 is before 15. Take the sorting off the combo box and it should list them in the order you;ve added them
Take a look at your ComboBox.Sorted property. If it is True then you get your unwanted behavior (default, string-based sort.) Since you are populating the combo box from what looks like a presorted list, make sure that ComboBox.Sorted is set to False.
Try this...didn't tested it but try this...
cbSumFrom.Items.Clear();
for (int i = 15; i < 250;)
{
cbSumFrom.Items.Add(Convert.toString(i));
}