I am attempting to fill a combobox with name and values from a appsettings file. Once a name is selected from the combobox I would like to have the value sent to a textbox below. I guess the part I am confused about is how to determine which is selected and display that value.
My goal would be to select Name "cmd" from combobox and have the value of path/to/cmd.exe into textbox below.
public void Form1_Load(object sender, EventArgs e)
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs = ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
comboBox1.Items.Add(names[i]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] names = ConfigurationManager.AppSettings.AllKeys;
NameValueCollection appStgs = ConfigurationManager.AppSettings;
for (int i = 0; i < appStgs.Count; i++)
{
textBox3.Text = appStgs[comboBox1.Text];
}
}
Try something like this
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox3.Text = appStgs[comboBox1.Text];
}
Related
I have a drop down list with a button event that should send it's value for a textbox.But,even if I choose a value that is not the first one in the DDL,it only sends the value of the first item in the DDL. I was told to add the !IsPostBack in the page load,but it didn't help.
Codes:
protected void Page_Load(object sender, EventArgs e)
{
string testeddl;
codProfessor = Request.QueryString["id"];
if (db.conecta())
{
ddlTeste.Items.Clear();
ddlTesteAltDel.Items.Clear();
ddlQuestoes.Items.Clear();
listaX = db.retornaTestes(codProfessor);
for (int i = 0; i < listaX.Count; i++)
{
testeddl = listaX[i].nometeste;
ddlTesteAltDel.Items.Add(testeddl);
}
protected void btnBuscarTeste_Click(object sender, EventArgs e)
{
if (db.conecta())
{
int posic = ddlTesteAltDel.SelectedIndex;
txtNomeTeste.Text = listaX[posic].nometeste;
ddlaltdelTeste.Text = listaX[posic].materiateste;
}
}
}
}
In Page_Load, just need to indicate:
if(!IsPostBack()
{
// rest of the code.
}
I want to capture the selected date on my DropDown list, where there are five days will display on DropdownList.
I'm usually putting the default value on DropDown, but not this time because in the drop down list I want it always display the current date and the next five days. But I don't know how to capture the data.
<asp:DropDownList ID="ddldate" runat="server">
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
List<ListItem> items = new List<ListItem>();
for (int i = 0; i < 5; i++)
{
items.Add(new ListItem(
DateTime.Now.AddDays(i).ToShortDateString(),
DateTime.Now.AddDays(i).ToShortDateString()));
}
ddldate.DataSource = items;
ddldate.DataBind();
ddldate.Items[0].Selected = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
string deliverytime = ddldate.SelectedValue.ToString();
lbltest.Text = deliverytime;
}
You're repopulating the DropDownList for every postback and reloading the page, hence the SelectedValue property value may be different from the posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ListItem> items = new List<ListItem>();
for (int i = 0; i < 5; i++)
{
items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));
}
ddldate.DataSource = items;
ddldate.DataBind();
ddldate.Items[0].Selected = true;
}
}
You should not bind data on PostBack, change your FormLoad code to below sample:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
List<ListItem> items = new List<ListItem>();
for (int i = 0; i < 5; i++)
{
items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));
}
ddldate.DataSource = items;
ddldate.DataBind();
ddldate.Items[0].Selected = true;
}
}
If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.
And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:
protected void Button1_Click(object sender, EventArgs e)
{
if(ddldate.SelectedValue != null)
{
string deliverytime = ddldate.SelectedValue.ToString();
lbltest.Text = deliverytime;
}
}
I have a CheckedListBox with items from a database.
When I check an item in the CheckedListBox and after that I close the form and open the form again, the item is not checked any more, i.e. the "check" has not been saved.
How can I accomplish that if I check an item and then close the form and open it again, that the item is still checked?
I tried this:
void deliveries_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < deliveries.ClbOrdersCheckDelivery.Items.Count; i++)
{
if (deliveries.ClbOrdersCheckDelivery.GetItemChecked(i) == true)
{
Properties.Settings.Default.CheckedItems = deliveries.ClbOrdersCheckDelivery.GetItemChecked(i);
}
}
}
]\
You need to write
Properties.Settings.Default.Save();
To save the settings.
Write it after the for loop.
EDIT
I tried the following code to save all checked items to settings file. It works. Please check.
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.CheckedItems = string.Empty;
foreach (var item in checkedListBox1.CheckedItems)
{
Properties.Settings.Default.CheckedItems += item + "," ;
}
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Properties.Settings.Default.CheckedItems);
}
private void Form1_Load(object sender, EventArgs e)
{
var checkedItems = Properties.Settings.Default.CheckedItems.ToString().Split(',');
foreach (var item in checkedItems)
{
var index=checkedListBox1.FindString(item);
if(index>=0)
{
checkedListBox1.SetItemChecked(index, true);
}
}
}
I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I have found the closest reference of my problem on these links:
ComboBox has its old value after Clear()
Why selected index fires only once the Index of the ListItem is changed?
I have 4 comboboxes: cmbcountry, cmbstate, cmbdistrict and cmbcity. cmbcountry populates on load event by GetCountry() method. cmbstate populates by GetState(countryid) method, which takes cmbcountry's selectedvalue as argument and returns a list of relevant states and so on for cmbdistrict and cmbcity...
Problem is on selecting different item in cmbState, cmbDistrct is not populating with the proper items.
.E.g. states :"madhya pradesh", "Utter pradesh" , "Rajsthan"
For "Rajsthan", cmbdistrict is populating with relavent value, but for "Utter pradesh", cmbdistrict still has old value.
Here is the relevant code:
private void TestForm1_Load(object sender, EventArgs e)
{
cmbCountry.ItemSource = Lookups.Lookup.GetCountries();
}
private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbState.Text = "";
cmbState.Clear();
cmbState.SelectedIndex = -1;
cmbState.SelectedItem = null;
//cmbState.Items.Clear();
int countryId = Convert.ToInt32(cmbCountry.SelectedValue);
cmbState.ItemSource = Lookups.Lookup.GetStates(countryId);
}
private void cmbState_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbDistrict.Text = "";
cmbDistrict.Clear();
cmbDistrict.SelectedIndex = -1;
cmbDistrict.SelectedItem = null;
//cmbDistrict.Items.Clear();
int stateId = Convert.ToInt32(cmbState.SelectedValue);
cmbDistrict.ItemSource = Lookups.Lookup.GetDistricts(stateId);
}
private void cmbDistrict_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbCity.Text = "";
cmbCity.Clear();
cmbCity.SelectedIndex = -1;
cmbCity.SelectedItem = null;
//cmbCity.Items.Clear();
int DistrictId = Convert.ToInt32(cmbDistrict.SelectedValue);
cmbCity.ItemSource = Lookups.Lookup.GetCities(DistrictId);
}
private void cmbBank_SelectionChangeCommitted(object sender, EventArgs e)
{
int bankId = Convert.ToInt32(cmbBank.SelectedValue);
cmbControlBranch.ItemSource = Lookups.Lookup.GetBranches(bankId);
}
I have all above methods to clear previous data in comboboxes ...and items.clear() is giving an error if i am using it.
Please tell me if there is any other relevant reference which I am missing .