Suppose I have one form where a combo box has some options. Now, at the first run of this program, user selects a option from combo box and saved it through a button click or something. Now, If user terminates the application and run again for the 2nd time, is there any way to retrieve the last saved selection?
That means, if you select option1 from the combo box and terminate the application. after some time, you again start the application, now your combo box should show option1 as selected because at the previous session, you selected it.
I hope you'll understand what i think.
Use Settings
// To Load (after combo box binding / population)
private void LoadSelection()
{
int selectedIndex = 0;
if (int.TryParse(Properties.Settings.Default.comboBoxSelection, out selectedIndex))
{
cbMyComboBox.SelectedIndex = selectedIndex;
}
}
// saving on button click.
private void saveButton_Click(object sender, EventArgs e)
{
//set the new value of comboBoxSelection
Properties.Settings.Default.comboBoxSelection = cbMyComboBox.SelectedIndex;
//apply the changes to the settings file
Properties.Settings.Default.Save();
}
See here for more detail.
You have to manually save the value and load it up again when the program starts.
The easy way to do it with Visual Studio is to create a Settings class. In VS, right click your project, click add new, scroll to "Settings File", add. VS will show you a UI where you can create new properties in the settings object that you can chose the name of.
If I create a new property called "ComboboxValue" of type string, I can reference it in the code as Settings1.Default.ComboboxValue = "hello world";
Here's the MSDN on it:
http://msdn.microsoft.com/en-us/library/a65txexh(v=vs.100).aspx
You can add settings
on solution explorer under the project, properties folder
add resource "string" give it a name "selected" for example
then
// this is save button
Properties.Settings.Default.selected = comboBox1.SelectedIndex;
Properties.Settings.Default.Save();
// this is retrieve (use it in window_load event for example)
comboBox1.SelectedIndex = Convert.ToInt32(Properties.Settings.Default.selected);
Related
I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
I've a list of element with a checkbox. The items are available inside a Checkbox, and I save all the checked value inside my settings app, in particular, I've a field called available_countries and I save all the event inside this field.
Essentially when a item is checked a fire the event associated:
private void AvailableCountries_Checked(object sender, RoutedEventArgs e)
{
var item = sender as CheckBox;
if (item.Content.ToString() == "All")//first item of the list need to check all
{
Vm.AvailableNations.Select(c => { c.IsChecked = true; return c; }).ToList(); //Execute the IsChecked update through linq
}
//Check if the checked value is already added in the settings..
var nationsArr = Properties.Settings.Default.available_countries.Split(';');
foreach (var country in nationsArr)
{
if (country == item.Content.ToString()) //Nation already added!
{
return;
}
}
Properties.Settings.Default.available_countries += item.Content.ToString() + ";"; //Separator
Properties.Settings.Default.Save();
}
Now this code working well and is very simple, but I noticed that .Save() at the end slow down the app performance, infact when I click on the first item of the ComboBox that have as .Content : All I need to change all the IsChecked property of all element.
I need also to check if the value is already added in the Settings.
I noticed that if I remove the last line of code I doesn't see the delay time, about 1/2 seconds.
There is a way to improve it?
Well, the settings are stored in an XML file in the user's profile, so yes, whenever you save your settings, a short delay will be there. No way to change that.
A possible solution in your case would be to not save the settings when the check box is checked, but in some other event (like for example an OK button of the settings dialog, etc).
Another solution would be to start a background task that changes the values and saves the settings, but you'd have to make sure this is properly synchronized.
A third solution would be to just change the settings object, but not save until the application is closed. That would slow down the process of closing the application, but ...
I have a combo box that holds employee names that are selected by that employee. When I add a name to the combo box with a button click is there a way to keep the name in the combo box for the next time the program is started? Below is my code to add the employee names that I know need to be in the list and then the code to add a name to the combo box.
private void employeeSelect_Load(object sender, EventArgs e)
{
cboEmpName.Items.AddRange(new string[] { "John", "Roger", "Bill", "Jason", "James" });
}
private void addTechbtn_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtAddTech.Text))
{
cboEmpName.Items.Add(txtAddTech.Text);
txtAddTech.Clear();
MessageBox.Show("Technician has been added");
}
else
{
MessageBox.Show("Enter a name to add to the list");
}
}
If you just want to persist the last value selected by user, UserSetting is the best place to save such values.
Declare user setting values as -
Right click your project >> Navigate to Properties >> Settings
Say the value is EmployeeNameCombovalue, then you can read the value like this -
var empNameVal = Properties.Settings.Default.EmployeeNameCombovalue;
And to save some value in it -
Properties.Settings.Default["EmployeeNameCombovalue"] = cboEmpName.Text;
Properties.Settings.Default.Save();
So next time, when the program stars, you check if the value is there in user setting, if yes, set it to the combobox.
But, if it is not just about saving a particular value, and you want to
save all the drop drop down values, then I suggest to store these
values in some persistent storage like DB, and bind your combobox from
that data. And whenever you add a new value to it, add the same in DB
as well and update your combobox data source.
https://msdn.microsoft.com/en-us/library/x8160f6f%28v=vs.110%29.aspx
There are various options available.
At some point, the answer boils down to a matter of urgency. I'd say you'll end up with these options:
You could store in the local file system, and read them again when form is loaded.
Serialize and store it in the registry (HKEY_CURRENT_USER - user specific)
Store it in the database and retrieve it again when needed.
.NET Application settings, provides the easiest way to access your
settings at runtime (Check this Example).
I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
The Items property of a checked list box control in Windows forms is of type object, so my naive hope was that I can add a customized User control as item. (Since, usually, my task is to write logic for background tasks I'm not too familiar with UI programming, so this may be a stupid idea..)
More precisely I want to display two labels and a button in each line of the the checked list box. The first label is supposed to display the name of an object the user can select (so that later on a specific operation will be performed on all checked items). For any item checked, the button is supposed to allow the user to choose a file from which custom settings can be read for performing that operation and the second label should display the choice the user has made using the button (i.e. the file name or something like the string "default settings").
So, in the forms designer, I created a custom control CustomControl1 with label1, label2, button1, and methods to set the text properties, set autosize of the labels and the button to false, defined their size manually. Then in the main window I created the checked list box, to which I added custom controls. The constructor of my main window now looks as follows:
InitializeComponent();
UserControl1 uc1 = new UserControl1();
uc1.setLabel1("label1_text");
uc1.setLabel2("label2_text");
uc1.setButtonText("button_text");
this.checkedListBox1.Items.Add(uc1);
uc1.Visible = true;
This compiles without any error and also runs, but the checked list box shows an empty field. I also experimented with the size of the list box. If I reduce the height so that the check box just fits into it then I do see fragments of the button in the corresponding line, but no label.
Is it possible to use a custom form in a checked list box and if yes, what am I missing?
No, you can't do this.
The listbox only shows a list of elements. The listbox uses the property .ToString() for each objects in the list to show the items.
You need to look for a custom listbox
private void Form1_Load(object sender,EventArgs e)
{
checkedListBox1.Items.Add("IIT");
checkedListBox1.Items.Add("CSE");
checkedListBox1.Items.Add("EEE");
checkedListBox1.Items.Add("ICT");
checkedListBox1.Items.Add("URP");
checkedListBox1.Items.Add("ENGLISH");
checkedListBox1.Items.Add("BANGLA");
checkedListBox1.Items.Add("MATH");
}
private void checkedListBox1_SelectedIndexChanged(object sender,EventArgs e)
{
//var item=checkedListBox1.SelectedItem;
label1.Text=checkedListBox1.SelectedItem.ToString();
}