Using visual c# I have 5 combobox's. I want all to have numbers 1-5, but when one is selected, all others disappear. I.e. if I select number 3 from combobox 1, none of the others have the number 3. Any ideas? I would post code but pretty stuck for ideas in this. Thanks in advance
You can add the ComboBox programmatically on your Parent ComboBox like:
private void ComboBoxParent_SelectedIndexChanged(object sender, EventArgs e)
{
int numComboBoxes = 5;
for (int i = 0; i < numComboBoxes; i++)
{
ComboBox comboBoxChild = new System.Windows.Forms.ComboBox();
comboBoxChild.Location = new System.Drawing.Point(0, 21 * i);//Position on the Form
comboBoxChild.Size = new System.Drawing.Size(121, 21);//Size of the ComboBox
this.Controls.Add(comboBoxChild);
}
}
private void ComboBoxParent_SelectedIndexChanged(object sender, EventArgs e)
{
int numComboBoxes = 5;
ComboBox SelectedCombo = sender as ComboBox;
for (int i = 0; i < numComboBoxes; i++)
{
ComboBox comboBoxChild = new System.Windows.Forms.ComboBox();
comboBoxChild.Location = new System.Drawing.Point(0, 21 * i);
comboBoxChild.Size = new System.Drawing.Size(121, 21);
this.Controls.Add(comboBoxChild);
comboBoxChild.SelectedIndex=SelectedCombo.SelectedIndex;
}
}
Related
I want to get all items from CheckedBoxList and add it to a new Form so for every checked item I want a new Label with checkedItem name and a TextBox. So far I'm doing that but when I open the form I got no results at all. I don't know how to get the checked item name and I'm doing this:
labels[i].Text = i.ToString();
private void Button4_Click(object sender, EventArgs e)
{
testForm = new Test();
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;
for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
{
txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
labels = new Label[checkedListBox1.CheckedIndices.Count];
labels[i] = new Label();
labels[i].Text = i.ToString();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);
txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}
Controls.Add(tableLayoutPanel);
testForm.ShowDialog();
}
Any suggestions?
Thank you, for the invested time.
The solution was to change the code to:
testForm.Controls.Add(tableLayoutPanel);
i have an array label name title[i], and link[i].
Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
title[i] = new Label();
link[i] = new Label();
}
when i click the label title, i can get the link label information too.
title[i].MouseClick += new EventHandler(hover_title);
i try this code doesnt work.
public void hover_title(object sender, EventArgs e)
{
title[i].text=link[i].text;
}
how i can get the label link text when i click the title label.
Something like following should solve your problem.
public void hover_title(object sender, EventArgs e)
{
var label = sender as Label;
int i = (title as IList).IndexOf(label);
label.Text = link[i].Text;
}
And remember, after you create a control you must give it a new location, in case of Label set a text, a new size (in case of Label you can set AutoSize property to true), and add it to parent control's Controls collection.
You can do this:
Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
var j = i;
title[j] = new Label();
link[j] = new Label();
title[j].MouseClick += (s, e) => title[j].Text = link[j].Text;
}
I have the following code. On form load, I want to create multiples labels, the first should be on position (20,0), the second on the (40,0) and till the last label. But the program just shows the first label, I mean label 0, and that's all.
How to fix this?
private void Form1_Load(object sender, EventArgs e)
{
Label[] nmr = new Label[10];
for(int i=0; i<10; i++)
{
nmr[i] = new Label();
nmr[i].Text = "label " + i;
nmr[i].Left += 20;
this.Controls.Add(nmr[i]);
}
}
nmr[i].Left = 20 * (i+1);
will calculate the distance you want. Yet, you will only see one label because the first label is too long. So you have to adjust its size:
nmr[i].Size = new Size(40, 15);
Then you will see that 20 pixels is way too small as a distance; the labels will overlap
private void Form3_Load(object sender, EventArgs e)
{
Label[] nmr = new Label[10];
for (int i = 0; i < 10; i++)
{
nmr[i] = new Label();
nmr[i].Text = "label " + i;
nmr[i].Location = new Point(0, 25 * i);
this.Controls.Add(nmr[i]);
}
this.Height = this.Height + (25 * nmr.Count());
}
you also need to resize your form as well,
this code will help you,
In your loop, replace
nmr[i].Left +=20;
with
nmr[i].Left = 20 * (i + 1);
You should increase the Left value by 20 for each iteration. I also don't see why you are populating an array since you just add the Labels to the Controls collection. Try this:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i < 11; i++)
{
var label = new Label();
label.Text = "label " + i;
label.Left += 20 * i;
this.Controls.Add(label);
}
}
Actually, you don't edit the right property. The right one would be control.Location which is a Point with the properties x and y.
To add them with 20 for every loop, you actually need to go like (20 * (i+1))
Example code that worked:
private void Form1_Load(object sender, EventArgs e)
{
Label[] nmr = new Label[10];
for (int i = 0; i < 10; i++)
{
nmr[i] = new Label();
nmr[i].Text = "label " + i;
nmr[i].Location = new Point(0, (20 * (i+1)));
this.Controls.Add(nmr[i]);
}
}
EDIT: Work on that 20 pt. Seems like the labels won't show right. Maybe try 30pt?
Try using Linq in order do generate Labels:
using System.Linq;
...
private void Form1_Load(object sender, EventArgs e) {
Label[] nmr = Enumerable
.Range(0, 10)
.Select(i => new Label() {
Text = $"label {i}",
Left = 20 + i * 20, // <- please, notice Left computation
Parent = this, })
.ToArray();
}
I am new to WPF and I am struggling with following thing:
I am creating dynamically buttons and adding them to grid, then I want to give them contextmenu which i am doing, but here is the thing contextmenu consists of integers and now when I click integer from contextmenu, I would like to update buttons content with number chosen from this contextmenu how do I achieve it?
Help and explainations will be greatly appreciated.
Here is my code:
List<int> a = new List<int>();
for (int i = 0; i < boardSize; i++)
a.Add(i + 1);
ContextMenu menu = new ContextMenu();
menu.ItemsSource = a;
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
btn = new Button() {ContextMenu=menu };
btn.Width = 25;
btn.Height = 25;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.SetValue(Grid.ColumnProperty, j);
btn.SetValue(Grid.RowProperty, i);
btn.HorizontalAlignment = HorizontalAlignment.Stretch;
btn.Background = new SolidColorBrush(Colors.Blue);
MyGrid.Children.Add(btn);
}
}
You need do 4 steps to archieve it :
Add a member variable to record which button you right click to show
contextmenu:
private Button currentBtn;
Add event to record the current right click button:
btn.MouseRightButtonUp += (cbtn, e2) => { currentBtn = cbtn as Button; };
Define menuitem:
List<MenuItem> a = new List<MenuItem>();
for (int i = 0; i < boardSize; i++)
{
var item = new MenuItem();
item.Header = (i + 1).ToString();
item.Click += item_Click;
a.Add(item);
}
Add click event to menuitem:
void item_Click(object sender, RoutedEventArgs e)
{
if (currentBtn != null)
{
var i = sender as MenuItem;
Dispatcher.Invoke(() => { currentBtn.Content = i.Header; });
}
}
I'm trying generate dynamically a bunch of buttons. The number of buttons is determined by how many pits are alive, and this is retrieved from a database.
Here is how it looks like :
Here is what I used to generate them :
private void Form1_Load(object sender, EventArgs e)
{
int numOf = pits.numOfPits();
Button[] genButtons = new Button[numOf];
int l = 1;
for (int i = 0; i < numOf; i++)
{
genButtons[i] = new Button();
genButtons[i].Width = 160;
genButtons[i].Height = 80;
Point location = new Point((i + 1) *200,75);
if (location.X > this.ClientSize.Width - 200)
{
location.X = l * 200;
location.Y += 100;
l++;
}
genButtons[i].Location = location;
this.Controls.Add(genButtons[i]);
}
}
The problem is that it will only work for 2 rows and it is kind of hard coded. How can I improve it to support an unlimited number of buttons.
You can ignore the limited "height" of form for the purpose of this question. I'll probably add some pagination later.
Add button controls to the FlowLayoutPanel. Extend the FlowLayoutPanel to include items per page, current page, and data to implement paging.
private void Form1_Load(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
for (int i = 0; i < pits.numOfPits(); ++i)
{
Button btn = new Button();
btn.Width = 160;
btn..Height = 80; //set padding or margin to appropriate values
flp.Controls.Add(btn);
}
this.Controls.Add(flp);
}