Let's say I have 10 textBoxes in my form. They are named textBox1, textBox2 etc. And I'd like to be able to choose the textBox I want to edit - for example I have a comboBox with numers 1-10 and if I pick, let's say "5", then the text of the textBox5 is being changed (typed in an additional, eleventh, textBox for example).
I know it sounds weird but I need to learn how to choose controls and edit them from the GUI.
You should use the combobox (cmb in my code) SelectedIndexChanged,
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
var numberFromComboBox = cmb.Text;
var txtBoxToEdit = Controls.OfType<TextBox>()
.Where(c => c.Name.EndsWith(numberFromComboBox))
.FirstOrDefault();
if(txtBoxToEdit != null)
{
txtBoxToEdit.Text = "was selected";
}
}
Update
To understand what the code does, one needs to understand a bit of Linq.
All Controls (ComboBoxes, DataGridViews, TextBoxes, etc.) are stored in the Controls collection.
But we only want the TextBoxe's that is directly on the form:
List<TextBox> listOfTxtBox = Controls.OfType<TextBox>();
This listOfTextBox now contains all the textboxes. But we only need one that matches the number we selected in the combobox (cmb).
To do that, we "filter" our collection of textboxes with the Where method.
In my expression it starts with - c => c.Name.EndsWith(numberFromComboBox) gives all the Textboxes that has a name (TextBox.Name) that ends with the number from our ComboBox.
The Last part is FirstOrDefault(), it simply takes the first item in our (now filtred) collection. If there is no items in the collection (for whatever reason) FirstOrDefault will return null
Hopes this helps to clearfi what the code does
Related
I have a couple of datagrids that uses a list of items as a source. I would like to move the item from list1 to list2 when the item in the grid is selected when I click a button or when I double click any of the cells in the row where the item is shown. This process will delete the selected item in the first list, and add it to the second one (it will disappear from the grid and be added to another grid which is linked to the 2nd list).
The items in both lists are part of the same class and both use the same constructor, so all of their parameters are the same.
I have been looking around the internet and trying different things myself but i cant quite find the solution, up to now this is what I came up with, but i cant make it to work.
public void Gladiators_Data_Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string ItemMoving =
Data_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
var item = List1.FirstOrDefault(x => x.Name == ItemMoving);
if (item != null)
{
List1.Remove(item);
List2.Add(item);
}
}
The solution to my problem doesn't have to follow the pattern that I tried to use, anything that works will be highly appreciated, thanks in advance.
1) if you are not allowing the user to reorder the items, use the index of the selected row. It will be much faster.
2) the rest of your process is correct. However, you forgot to rebind the datagrids to the new lists. The grids do not update their UI automatically.
Small pseudo example:
var item = List1[grid1.currentrow.index];
List1.Remove(item);
List2.Add(item);
Grid1.datasource=list1;
Grid2.datasource=list2;
I want my combobox to enable the autocomplete if what the user is typing is in the items list and if it doesn't exists, I want to include it in my list.
For example:
A ComboBox with these items: "Rock, Country, Jazz".
If the user starts typing "Ro..." the combobox autocomplete to 'Rock'. But if the user types "Blues", I want to add it to my items. So it would be like: "Rock, Country, Jazz, Blues".
How can I do that?
You can use AutoCompleteMode and AutoCompleteSource for auto complete.
comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
or you can do this via Properties Panel in Visual Studio after selecting your ComboBox...
For adding new items to your ComboBox;
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains(comboBox1.Text))
{
comboBox1.Items.Add(comboBox1.Text);
comboBox1.Items.RemoveAt(comboBox1.Items.Count - 2);
}
}
If I was doing this using MVVM, I would start with a ComboBox and modify it to suit.
You can get this almost for free if you use the built in ComboBox in DevExpress. Simply fill the dropdown list up with items that you want to auto-complete, then set the options for:
Auto dropdown (so when you start to type, it will automatically drop down a list of matches).
Filter list by match (i.e. the only items in the dropdown will be the ones that match what you have typed).
Match on partial (i.e. what you typed will filter the dropdown list, with a match anywhere, even in the centre).
If you wanted to get fancier, you could write a service that listens to what the user has currently typed in the box, then adjust the list of dropdown items to suit. Any items in the dropdown list that matches what the user types will automatically be displayed. I would use Reactive Extensions (RX) and Throttle to do this, see:
How to throttle event stream using RX?
The difference between Rx Throttle(...).ObserveOn(scheduler) and Throttle(..., scheduler).
I have a RadComboBox that allows for custom searching for about 1500 records. Once one is selected the User control changes accordingly. If I were to want to select a new record, the selected record is the only record that displays and when you type in the search field, nothing is found either.
However, if you select a record, do what you need then click the drop down then click off it then click on it again, it resets itself as desired. How can I make it so the drop down list populates itself on the initial click?
Probably you are losing the datasource.
On the SelectedIndexChange event, you can repopulate the datasoure and set currently selected value from arguments that are passed to this event.
So in a pseudocode it would look like:
procedure OnSelectedIndexChanged(object sender, SelectedIndexChangedEventArgs e)
{
var selectedValue = e.Value;
(sender as RadComboBox).DataSource = YourMethodToGetDatasource();
(sender as RadComboBox).DataBind();
(sender as RadComboBox).SelectedValue = selectedValue;
}
and yes i know currently pseudo code doesn't look so pretty but of course you will need to use the id of your control in place of (sender as RadComboBox).
If someone know a better method for this i would be glad to gain this knowledge :)
I have following piece of code:
private void nameTextBox_Leave(object sender, EventArgs e)
{
var names = ConfigurationManager.AppSettings.AllKeys
.Where(k => k.StartsWith("name"))
.ToArray();
// Add names to combobox
comboBox.Items.AddRange(names);
}
Problem is each time I press Tab from textbox, comboBox elements keep on doubling. If it had Ken, John, Tim in there, it will show that twice if I press tab again.
I tried using distinct in the names above but that does not do anything as new instantance is created each time and previous is saved. I cannot make comboBox empty right after adding names as it is being used in a button click latter on in the code.
Only alternative i thought was of declaring a global variable and make sure its value is 0
and then only insert values in comboBox, and change it to 1 once value is inserted. But that does not seem like a good coding practice.
Is there any better way to get this done?
Add comboBox.Items.Clear() before the AddRange. So the whole block should be.
private void nameTextBox_Leave(object sender, EventArgs e)
{
var names = ConfigurationManager.AppSettings.AllKeys
.Where(k => k.StartsWith("name"))
.ToArray();
// Add names to combobox
comboBox.Items.Clear();
comboBox.Items.AddRange(names);
}
i'm not sure if I understand perfectly, but why can't you just clear the items before populating?
comboBox.Items.Clear()
comboBox.Items.AddRange(names);
also you could try not to use Items, but DataSource:
comboBox.DataSource = names;
The suggestions to clear the combo box first should solve your problem, but if you are loading the combo box on page load as well then I don't think this is the best solution (just masking the real problem). If you are populating the combobox on page load then add a Page.IsPostBack check in there:
example:
if(!Page.IsPostBack)
{
//Populate the initial values into the combobox
}
If you aren't then the clear solution should be fine.
I have a pretty simple form with a listbox, a text box, and two buttons.
The list box items are populated from a sql database table. The user may chose to select one or multiple items from the listbox.
The text box is used to write more details about the items in the listbox. One button can then be clicked to update another database table with these details.
I want to make it where if any items are selected from the listbox, those contents are automatically copied into the text box field on the fly as they are selected.
Is this possible?
I've been able to make this happen on the button click event - just not on the fly as they are selected. I want it to occur before the additional details are being sent to the database
I've also tried using several different listbox events, but have been unable to obtain the results I am looking for.
Any suggestions?
try this out
you will have to handle the SelectedIndexChanged event on the listbox.
here is an example with example controls.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = "";
foreach (string nextitem in listBox1.SelectedItems)
{
textBox1.Text += nextitem + " ";
}
}
im not too sure HOW you want the text to appear in the textbox so that would be up to you in the foreach loop.
yes, the SelectedIndexChanged event fires on every selection change, and you can concatenate together the items in the listbox. But if you are talking the description that's not visible too, you need to store the description in each listboxitem tag property, and in your code retrieve the description from there.