I have a listbox in asp.net
I tried when user click on it's item in runtime can change the text of this item without change the order of the the items .
So I try to make Edit button when click on it get the item value
as
protected void btnEditListValue_Click(object sender, EventArgs e)
{
string listvalue = lstParameters.Items.IndexOf(lstParameters.SelectedItem).ToString();
string listText = lstParameters.SelectedItem.ToString();
}
then I will make textbox fill value from string listText to enable user to edit
what shall I do after that to keep listbox order as old without delete and insert again
please help
You can overwrite the ListItem.Text, f.e. when the user changed the text:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(lstParameters.SelectedIndex >= 0 && !String.IsNullOrWhiteSpace(TextBox1.Text))
{
ListItem selectedItem = lstParameters.Items[lstParameters.SelectedIndex];
selectedItem.Text = TextBox1.Text;
}
}
Related
I am confronting with an issue with the ComboBox in a WinForms app. The combo box contains a list with items that are grabbed from a TXT file. To read and update the list I added the following code to Form_Load.
string[] lineOfContents = File.ReadAllLines(Application.StartupPath + #"Items.txt");
foreach(var line in lineOfContents)
{
string[] tokens = line.Split(',');
ComboBox.Items.Add(tokens[0]);
}
All nice and good, the list does update. However, I also have some TextBoxes that are changing their text based on the string of the selected item in the ComboBox. For example, when the selected item in the ComboBox is "Example", the text in the first text box should change from empty to "I am an example!", but it doesn't and remains empty. The code for it is:
if(ComboBox.SelectedItem == "Example")
{
TextBox.Text = "I am an example!";
}
At first I though it's a conversion issue so I tried to use Convert.ToString(tokens[0]); but it did not work as well.
Any suggestions on what I should try next to fix this issue?
You are describing bound behavior so the first thing to check is whether the TextBox is properly connected to the event fired by the ComboBox. Something like this would be the first step to getting the behavior you describe.
public MainForm()
{
InitializeComponent();
// Subscribe to the event here OR using the Designer
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "Example")
{
textBox1.Text = "I am an example!";
}
else textBox1.Text = comboBox1.Text;
}
But the missing step in the code you posted is actually selecting a value in the combo box after the tokens are loaded into it. This leaves the text box blank as demonstrated here:
private void buttonLoad_Click(object sender, EventArgs e)
{
loadMockFile();
}
private void loadMockFile()
{
string[] lineOfContents = MockFile;
foreach (var line in lineOfContents)
{
var token =
line.Split(',')
.Select(_ => _.Trim())
.Distinct()
.First();
if (!comboBox1.Items.Contains(token))
{
comboBox1.Items.Add(token);
}
}
}
string[] MockFile = new string []
{
"Example, A, B,",
"Other, C, D,",
};
So the solution is to make sure that you make a selection after the tokens are loaded into the ComboBox, for example as shown in the handler for the [Load + Select] button:
private void buttonLoadPlusSelect_Click(object sender, EventArgs e)
{
loadMockFile();
var foundIndex = comboBox1.FindStringExact("Example");
comboBox1.SelectedIndex = foundIndex;
}
I try to select the row from grid and show it in text box to make update and delete to the row.
I have tried this code with DataGrid. It worked, but when I use it in infragistics webdatagrid it gives me null
protected void WebDataGrid1_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
{
txtCode.Text = WebDataGrid1.Rows[e.RowIndex].Items[1].Value.ToString();
}
There is no error massage but data does not show in text box.
try this
protected void WebDataGrid1_SelectedIndexChanged(object sender,DataGridViewCellEventArgs e)
{
txtCode.Text = WebDataGrid1.Rows[e.RowIndex].call[1].Value.ToString();
}
I am populating a list-view with passwords.
Then I want to take the selected items text and pass it to a text box when it is clicked.
So far I have:
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem = passwordListView.SelectedItems[0];
passwordTextBox.Text = listViewItem.Text;
}
It works the first time I press it and it populates the textbox but then if I click a different password in the list-view it throws an exception.
Have I left out something blatantly obvious?
When the Selected Index is changed in a winforms ListView the first event is for the item that has now been deselected. So at that point SelectedItems is empty.
Check for this through if (passwordListView.SelectedItems.Count == 0) return;
After this you will get a second event, which will be for the new selection, and you can act on this.
Btw, you don't need to make a new ListViewItem as you are in your snippet, this will save the extra unnecessary creation:
ListViewItem listViewItem = passwordListView.SelectedItems[0];
Maybe you could try this :
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
if(passwordListView.SelectedItems.Count > 0)
passwordTextBox.Text = passwordListView.SelectedItems.First().Text;
}
i want to add selected item from a data bounded listbox (listbox1) to another listbox (listbox2)
Here is the code on a click event of a button.
private void btnrgt_Click(object sender, EventArgs e)
{
string x = listBox1.SelectedItem.ToString();
listBox2.Items.Add(x.ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
When i run this code System.data.datarowview get displayed in the listbox2.
Kindly help.
thank you in advance.
When you bind a ListBox datasource to a DataTable every item inside the ListBox is a DataRowView, not a simple string. In the ListBox you see a string displayed because you set the DisplayMember property of the ListBox with the name of a column in that DataRowView.
So, taking the current SelectedItem doesn't return a string but a DataRowView and calling ToString() for a DataRowView returns the full qualified name of the class (System.Data.DataRowView).
You need something like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
listBox2.Items.Add(x["NameOfTheColumnDisplayed"].ToString());
txttestno.Text = listBox2.Items.Count.ToString();
}
}
EDIT
It is not clear what is the source of the error stated in your comment below, however you could try to avoid adding an item from the first listbox to the second one if that item exists in the second listbox with code like this
private void btnrgt_Click(object sender, EventArgs e)
{
DataRowView x = listBox1.SelectedItem as DataRowView;
if ( x != null)
{
string source = x"NameOfTheColumnDisplayed".ToString();
if(!listbox2.Items.Cast<string>().Any(x => x == source))
{
listbox2.Items.Add(source);
txttestno.Text = listBox2.Items.Count.ToString();
}
}
}
This solutions works if your second listbox is really populated adding simple strings to its Items collection.
On click button use below code.
protected void btnGo_Click(object sender,EventArgs e) {
string x = ListBox1.SelectedItem.Text;
ListBox2.Items.Add(x);
}
attempting to change text based on a combobox selection. C#, windows form, combobox loads on form load.
using an ADO entity data model to map the db, I have the tables added in. I have a combobox which loads the vendors, then a button that says get vendor. upon that I have 3 labels I want to display the vendor name, city and zip. I'm just having trouble figuring out how to make the label binded to the selected item from the combobox (the vendor selected) and make it change.
quick edit: I know the button code is wrong. but its where i'm at so I posted it. Thanks!
PayablesEntities payablesSet = new PayablesEntities();
private void Form1_Load(object sender, EventArgs e)
{
comboBoxVendor.DataSource = payablesSet.Vendors.ToList();
comboBoxVendor.DisplayMember = "Name";
comboBoxVendor.ValueMember = "VendorID";
}
private void buttonGetVendor_Click(object sender, EventArgs e)
{
label5.text = comboBoxVendor.SelectedValue;
}
Take a look at the SelectedIndexChanged & SelectionChangeCommitted events.
Figured it out! needed a query to store that selected item's info, duh!
private void buttonGetVendor_Click(object sender, EventArgs e)
{
int vendorID = (int)comboBoxVendor.SelectedValue;
var selectVendor =
(from vendor in payablesSet.Vendors
where vendor.VendorID == vendorID
select vendor).First();
label5.Text = selectVendor.Name;
label6.Text = selectVendor.City;
label7.Text = selectVendor.ZipCode;
}