Change Path of OpenFileDialog - c#

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();
}

Related

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);

Properties.Settings.Default Doesn't save the values

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

Using firefox to open c# url button not default browser

I have a very simple program that has 4 buttons each button opens a url.
What i would like is to get each button to use Firefox as the default browser.
I know i can set it as the default browser and then it will work however if a user sets internet explorer as the defualt it will open in that instead.
These url only work in Firefox as it has certificate issues.
So essentially my question is how can i change my code to make sure that if you click on any of the 4 buttons, that it will open up in firefox and not internet explorer.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=on");
BtnOff.BackColor = Color.Red;
Bnton.BackColor = Color.Chartreuse;
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=off");
Bnton.BackColor = Color.Red;
BtnOff.BackColor = Color.Chartreuse;
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/pause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnDND.BackColor = Color.Chartreuse;
BtnQue.BackColor = Color.Red;
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/unpause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnQue.BackColor = Color.Chartreuse;
BtnDND.BackColor = Color.Red;
}
Thanks for taking the time to look appreciate it.
you can use
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://yoursite.com/dummy.htm");
you can keep the path of firefox.exe in config file instead of hard coding. Alternatively set the PATH environment variable.

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.

SplitterLocation doesn't save in the settings

i have created a splitterLocation setting, Type system.Drawing.Point and Scope : User. to save the splitter location when the user change it. and load again with the new location when the form load.
private void splitter1_LocationChanged(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
splitter1.Location = MailSystem.Properties.Settings.Default.splitterLocation;
}
but it doesnt work i dont know why ?.
Try moving the saving code to the FormClosing event.
private void Form1_FormClosing(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
and make sure your splitter1 control isn't being docked, DockStyle.None.

Categories