Properties.Settings.Default Doesn't save the values - c#

I making a wpf application but in settings window, I'm changing the text of textboxes listed in view, then I click save
button to update Properties.Settings.Default. Then, I close settings window. I go back to the MainWindow. But when I shutdown the application, in the next session my values does not show up. How could I solve it?
Save Button:
private void save_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.PC_Name = pcname.Text;
Properties.Settings.Default.Acc_name = name.Text;
Properties.Settings.Default.Acc_sname = sname.Text;
}
Loaded Event(Page):
private void ayarlarekrani_Loaded(object sender, RoutedEventArgs e)
{
pcname.Text = Properties.Settings.Default.PC_Name;
name.Text = Properties.Settings.Default.Acc_name;
sname.Text= Properties.Settings.Default.Acc_sname;
}

Looks like you are missing:
Properties.Settings.Default.Save();
http://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp

Related

Click Event not fired in User Control when multiple instances are created

I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}

c# reset a textbox.text property at runtime to use default

In my application i have a lot of support for different languages(WinForms).
Initially i have set the text in the button to say "Start" in a bunch of different languages.
On a click-event the text changes to "⚫".
And then i have another button that stops the event on click.
Is it possible to revert the "running" (⚫) text to the original text?
textbox.text.ResetText() just clears it.
private void btnStartTest_Click(object sender, EventArgs e)
{
btnStartTest.Text="⚫";
}
private void btnStopTest_Click(object sender, EventArgs e)
{
//reset the text to what it used to be.
}
Solution:
private string languageString;
private void btnStartTest_Click(object sender, EventArgs e)
{
languageString = btnStartTest.Text;
btnStartTest.Text="⚫";
}
private void btnStopTest_Click(object sender, EventArgs e)
{
btnStartTest.Text = languageString;
//reset the text to what it used to be.
}
If you use the internationalization mechanism of WinForms that uses resource files to store the property values of controls for different languages, you can use this source code to reset the button to its initial state using the current UI language:
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyFormClass));
resources.ApplyResources(buttonStart, buttonStart.Name);

Change Path of OpenFileDialog

At the moment I am using this here in my WPF App which works as it should.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
It remembers the last path I was in but I want to change it to a set path now.
I have 3 Radiobuttons and each Radiobutton should lead to a different path so I thought about doing it with a variable I give to the openFileDialog but I am not sure on how to go with that. Has anyone done this and can give me a lead on it ?
You can set IntitialDirectory to the folder you want in code where you show the dialog.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.IntitialDirectory = youFolderPath;
openFileDialogPresentations.ShowDialog();
}
The standard file dialogs have an InitialDirectory property that determines in which folder the dialog opens.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.InitialDirectory = #"X:\Data\Presentations";
openFileDialogPresentations.ShowDialog();
}
You can do it by using InitialDirectory property. You can set three different paths for radio buttons
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory=#"D:\MyDir";
dialog.ShowDialog();
}

C# Forms - Loading images

I'm trying to load a specific image in my picture box if a specific radio button is checked.
The run-time error was a "_Couldn't locate file in this directory.", so I moved the images to that directory, but that just caused the picture box to load it automatically.
I've also imported all of the images I need as well.
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
}
When I run the program pictureBox1 had already loaded the image. What I want my pictureBox1 to do is remain blank until the user selects a radio button and clicks the "change image" button.
You should place pictureBox1.Visible = false; in the form load event
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
You could make it so that pictureBox1.Visible = false;
Then you could say:
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Visible = true;
pictureBox1.Load("10C.jpg");
}
}
This way the image is not seen until the button is clicked.
I would recommend you to set a default image in picture box at startup and if a radiobutton is checked and a button is clicked load the desired image (10C.jpg) into picture box instead of dealing with the visible property of the picture box.
The code will look like:
private void button1_Click(object sender, EventArgs e)
{
// pictureBox1.Visible will be always set to true
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
else
{
pictureBox1.Load("placeholder.jpg");
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox1.Load("placeholder.jpg");
}
Recommended: search google for placeholder image
Hope it helps!
EDIT
In response of:
The run-time error was a "_Couldn't locate file in this directory.",
so I moved the images to that directory, but that just caused the
picture box to load it automatically. I've also imported all of the
images I need as well.
Define a key in config file that holds an image path and use it to access your images by concatenating filename at the end. With this approach you can change image path even after deployment.

How can I navigate and pass data between Pages?

I'm a bit of a beginner with this so i'll try and keep it simple.
I have a a xaml page with a button click event linking it to another xaml page. What I'm trying to do is on the click event take two strings and pass them to a text box on the second page. Can you please show me a simple code example of how to do this?
On the button click event of the first page you do something like the following
private void button1_Click(object sender, RoutedEventArgs e)
{
string urlWIthData = string.Format("/Page2.xaml?name={0}", txtName.Text);
this.NavigationService.Navigate(new Uri(urlWIthData, UriKind.Relative));
}
On the desintation page, you do the following:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Text = this.NavigationContext.QueryString["name"].ToString();
}

Categories