Remeber Me C# app setting - c#

I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?

You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}

Related

Checking if certain file\files exist

I want to make a windows form using C# to check if a file exist.
I've tried this one:
private void test_Click(object sender, EventArgs e)
{
var app1 = (#"C:\Users\frangon\Desktop\Spectrum-Check.EXE");
test.Text = File.Exists(app1).ToString();
}
If possible, I don't want to click it. I just want it to show as "True" if the file exist, or "False" if the file doesn't exist.
You can add an event to trigger when the form loads:
And then in your code behind:
private void Form1_Load(object sender, EventArgs e)
{
string app1 = #"C:\Users\frangon\Desktop\Spectrum-Check.EXE";
test.Text = File.Exists(app1).ToString();
}

My labels text wont change on loading form

I have tried to change my labels text in both the form_load and form_shown methods and nothing happens. I don't want to set the text in the properties as I want to use the text in another label later. Ive tried both these methods neither works.
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}
and
private void History_Shown(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}
You must add ToString()
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0].ToString();
}

Trying to save comport settings

So I'm trying to populate a combo-box with the available com ports, on a settings page.
Once the setting as been chosen I would like that setting to be retained and be available globally with the save settings function via a save button. I think there must be an easier way than this!
private void Form2_Load(object sender, EventArgs e)
{
pumpPort = SerialPort.GetPortNames();
this.comboBox1.Items.AddRange(pumpPort);
this.comboBox1.SelectedItem = Properties.Settings.Default.Setting;
Properties.Settings.Default.Save();
switch (Properties.Settings.Default.Setting)
{
case "COM1":
this.comboBox1.SelectedItem = Properties.Settings.Default.COMPORT1;
break;
case "COM2":
this.comboBox1.SelectedItem = Properties.Settings.Default.COMPORT2;
break;
default:
break;
}
Needless to say this does not retain any settings after form2 is closed. I would like it to retain even after the program has exited let alone form2.
When you do this:
this.comboBox1.SelectedItem = Properties.Settings.Default.Setting;`
You are setting the selected item of the combo box.
I think you really want to reverse that.
Properties.Settings.Default.Setting = this.comboBox1.SelectedItem
You would want to do the assignment in the change event of the combo box however, so that when a user selects the value, your settings are updated and saved.
private void Form2_Load(object sender, EventArgs e)
{
pumpPort = SerialPort.GetPortNames();
this.comboBox1.Items.AddRange(pumpPort);
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs eventArgs)
{
Properties.Settings.Default.Setting = this.comboBox1.SelectedItem;
Properties.Settings.Default.Save();
}
As #wbennett notes in his answer, make sure that the indexchanged event is set in either your codebehind, or preferably in your designer.
You need to update the default setting and invoke save on the comboBox1 change event.
Like so:
private void Init()
{
...
this.comboBox1.SelectedIndexChanged +=
new System.EventHandler(ComboBox1_SelectedIndexChanged);
}
private void ComboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
//other event code
...
var comboBox = (ComboBox)sender;
var port = (string)comboBox1.SelectedItem;
Properties.Settings.Default.Setting = port;
Properties.Settings.Default.Save();
...
}

using checkbox to enable textbox

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!
You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}
Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.
You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}
I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

Categories

Resources