I'm new to C# and I have a question which I couldn't find somehwere else.
I created a ComboBox like this:
ComboBox lijst = new ComboBox();
Also I added some items and a location:
lijst.Location = new Point(400, 25);
lijst.Text = "Basis";
lijst.Items.Add("Basis");
lijst.Items.Add("Zuilen");
lijst.Items.Add("Vuur");
lijst.Items.Add("Zigzag");
The thing is you can select a item now and press a button which triggers the next method.
private void bereken(object sender, System.EventArgs e)
{
string nr = Convert.ToString(lijst.SelectedIndex);
Label tekstuitvoer = new Label();
tekstuitvoer.Location = new Point(100, 100);
tekstuitvoer.Size = new Size(70, 20);
tekstuitvoer.Text = nr;
this.Controls.Add(tekstuitvoer);
}
But here is my problem. Once you press the button, the outcome of tekstuitover.Text will always be the same. So if you press the button while "Vuur" is selected then the outcome will always be 2. It doesnt matter if you change the combobox item afterwards. The output will always be 2. But when I restart the program and select the fourth option instead of the third, now the outcome always will be 3.
Can you help me with this problem? Is there a way to reset string nr?
Thanks in advance. I hope you guys can help me!
Took me a while to figure out what you were saying there.
Add a SelectedIndexChanged event handler to your combobox, and trigger your label code from there, you can get rid of the button.
However as things stand you are going to create a new label component every time it changes.
Simpler to just add one at design time and set the text property in the event.
Related
I got a problem with a ListBox in a WinForm application. I have two ListBoxes inside of a tab control and depending on the selection in the first one (lb1), the DataSource of the second one (lb2) changes. This is done in the SelectedValueChanged Event.
private void listBox_ControlUnits_SelectedValueChanged(object sender, EventArgs e)
{
ControlUnit unit = (sender as ListBox).SelectedItem as ControlUnit;
textBox_ProjectNameTab.Text = unit.ProjectName;
listBox_ControlCircuits.DataSource = null;
listBox_ControlCircuits.DataSource = unit.ControlCircuits;
}
lb1 is filled with a DataSource, too.
Now if I select a value in lb1 the selection automatically jumps back to the first item and I can not figure out why. is this some kind of UI update problem?
Even without the SelectedValueChanged event and the connection to the second listbox the issue occures.
Short gif of the problem, sorry for the blurriness
If I select one item more than once it works somehow (as seen in the gif).
Edit:
I found the problem but I do not quite understand what happens.
I have another listBox on another tab of my tab control. This listBox has the same DataSource as lb1. This seems to cause this behavior.
I finally found the problem:
I did not know that if I use the same DataSource for two ListBoxes they share the BindingContext per default.
I created a new BindingContext for the second ListBox and now the selection does no longer change.
listBox_allGroups.DataSource = null;
listBox_allGroups.DataSource = x.y;
listBox_allGroups.DisplayMember = "Name";
listBox_ControlUnits.DataSource = null;
listBox_ControlUnits.DataSource = x.y;
listBox_ControlUnits.DisplayMember = "Name";
listBox_ControlUnits.BindingContext = new BindingContext();
You can use a variable to hold the selected item
object _selecteditem=null;
and check it in ListBox click event.
prive void ListBox1_Click(object sender,EventArgs e)
{
if(ListBox1.SelectItem == _selecteditem) return;
// do ...
}
I'm trying to get the value of items in a dynamically made combobox from a separate event. I need to put the value the user chose into a string, but I can't find a way to do it. Here's an example.
ComboBox player1Role = new ComboBox();
player1Role.Width = 100;
player1Role.Items.Add("Top");
player1Role.Items.Add("Jungle");
player1Role.Items.Add("Mid");
player1Role.Items.Add("Bot");
player1Role.Items.Add("Support");
player1Role.Items.Add("Fill");
player1Role.Location = new Point(200, 200);
And then in the following click event, I need to get the value they selected:
private void CreateParty_Click(object sender, EventArgs e)
{
ComboBox player1SelectedRole = (ComboBox)Controls["player1Role"];
string player1roleString = player1SelectedRole.Items[0].ToString();
MessageBox.Show(player1roleString);
}
That code just gives me an exception saying player1SelectedRole was empty. What am I doing wrong?
You're looking for the control by its name, but you didn't assign a name to the control.
player1Role.Name = "player1Role";
Also, I think player1SelectedRole.Items[0] is always going to select the first item regardless of which one is actually selected. Check out player1SelectedRole.SelectedItem instead.
How do I create a control in a windows form application? I want to generate a textbox or a radio button when I select something from a Combobox. I basically want to query my database, and based on the values of the fields, I want to generate a textbox or a radio button. For example, if my query returns a value of "Textbox", I want to generate a textbox on the form in a specified location. How does one accomplish this? Please help.
The easiest way is to create the control manually in code and then add it to the Controls collection. Deciding which control to create depending on some input data (whether it's database query or a value selected from a combobox) is not much different then in any other case. Simple if will do the job. For instance:
private void AddControl(string control)
{
if (control == "Textbox")
{
TextBox tb = new TextBox();
tb.Location = new Point(100, 100);
this.Controls.Add(tb);
}
else if (control == "Radio")
{
RadioButton rb = new RadioButton();
rb.Location = new Point(200, 100);
this.Controls.Add(rb);
}
}
Of course, it's very naive version. But it's only a starting point. I leave to you adding more advanced logic (like dynamically adjusting location, setting up properties of the radio button or the textbox, relying on Type instead of on a simple string, etc.)
The assumption is that you retrieve a string value from the database. For example in form's constructor:
public Form1()
{
InitializeComponent();
string requestedControl = QueryDatabase();
AddControl(requestedControl);
}
I leave to you implementing the method to query the database.
In the question you also mentioned adding a control after selecting it in the combobox. In this case the only difference is that you rely on an event triggered after combobox's selection changed:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
string res = this.comboBox1.SelectedItem.ToString();
this.AddControl(res);
}
Here you rely on SelectedValueChanged event. Of course, in this case your combobox has to be populated with expected values (here "Radio" and "Textbox"). Also, you have to attach the event handler to the specific event on the combobox. You can do that in designer or by adding in the constructor the following line:
combobox1.SelectedValueChanged += comboBox1_SelectedValueChanged;
Hope that clarifies the issue and sets up some starting point for you to continue from.
its my first time using tabs and I've ran into a problem. I'm trying to make it so whenever I press a button, combo boxes will appear inside the tab that is open, and then if the button is pressed again it will add another combo box underneath the first one.
Here is how I did it:
private void buttonLevel4Add_Click(object sender, EventArgs e)
{
for (int i = 0; i < 8; i++)
{
comboBoxModuleSelect.Add(new ComboBox());
System.Drawing.Point p = new System.Drawing.Point(176, 114 + i * 25);
(comboBoxModuleSelect[i] as ComboBox).Location = p;
(comboBoxModuleSelect[i] as ComboBox).Size = new System.Drawing.Size(183, 20);
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
}
}
But the problem is that the combo box won't be created in the tab page but underneath the tab page (i.e. on the form). Does a tab page not replace the form area? Please if someone could help I would appreciate it. Thanks
EDIT:
Oh God, another noob moment for me. I changed this line:
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
to:
tabpage.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
really sorry, I guess it just helps me to ask the question and think about it.
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
to:
tabpage.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
i have 10 textboxes and i need to select text from each one of them.
The problem is that i cant select text from multiple textboxes.
Is there any solution for this problem my code is.
private void Form1_Load(object sender, EventArgs e)
{
createTextBoxes(10);
((TextBox)textBoxes[0]).Select(1, 4);
((TextBox)textBoxes[1]).Select(1, 4); // <- it will not select text
((TextBox)textBoxes[2]).Select(1, 4); // same here
}
Control[] textBoxes;
private void createTextBoxes(int cnt)
{
textBoxes = new Control[cnt];
for (int i = 0; i < cnt; i++)
{
TextBox tb = new TextBox();
tb.Name = i.ToString();
tb.Location = new Point(5, 5 + 14 * i);
tb.Size = new Size(600, 20);
tb.BorderStyle = BorderStyle.None;
tb.Text = "sample text" + i.ToString();
textBoxes[i] = tb;
this.Controls.Add(tb);
}
}
Set the HideSelection property of the texboxes to false. They will maintain selection after losing focus.
Only one control can have a "Focus" at a time... you can't select (ie:highlight) text of multiple controls.
I also just tested by adding a button to the form and posted your 3 "select" snippets there too... nothing showed highlighted. However, when I did a TAB through each control, the first 3 respectfully showed the highlighted section. When I tabbed through the rest, the entire field of the rest of the textboxes were fully selected.
Or are you really trying to accomplish something else...
The text is selected you just can't see it cause of focus.
I ran your code and after doing so tabbed through the controls. The first 3 are selected as specified.
This is possibly not working because even though you've added the TextBox instances to the Form, they have not yet been displayed. Until they are displayed and initially rendered it's likely not possible to initiate a selection on them.
Actually it does, the problem is that the other 2 of your textboxes ([1] and [2]) don't have focus. Only one control can have focus at a time. If you hit tab to give focus to the next TextBox, you'll see the text selected.