I am trying to code a quick program and the error shows up when I assign Properties.Settings to variable s.
I am trying to assign it to this variable because a lot of my text boxes need to be assigned with a value of the setting and also because there is a lot of settings which need saving.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpDock
{
public partial class SettingsWindow : Form
{
public Properties.Settings s = new Properties.Settings(); // This is the error.
public SettingsWindow()
{
InitializeComponent();
}
private void Settings_Load(object sender, EventArgs e)
{
app1.Text = s.app1;
app2.Text = s.app2;
app3.Text = s.app3;
app4.Text = s.app4;
app5.Text = s.app5;
app6.Text = s.app6;
ico1.Text = s.ico1;
ico2.Text = s.ico2;
ico3.Text = s.ico3;
ico4.Text = s.ico4;
ico5.Text = s.ico5;
ico6.Text = s.ico6;
}
private void abutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app1.Text = ofd.FileName;
s.app1 = ofd.FileName;
}
}
private void Accept_Click(object sender, EventArgs e)
{
s.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void abutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app2.Text = ofd.FileName;
s.app2 = ofd.FileName;
}
}
private void abutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app3.Text = ofd.FileName;
s.app3 = ofd.FileName;
}
}
private void abutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app4.Text = ofd.FileName;
s.app4 = ofd.FileName;
}
}
private void abutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app5.Text = ofd.FileName;
s.app5 = ofd.FileName;
}
}
private void abutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app6.Text = ofd.FileName;
s.app6 = ofd.FileName;
}
}
private void ibutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico1.Text = ofd.FileName;
s.ico1 = ofd.FileName;
}
}
private void ibutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico2.Text = ofd.FileName;
s.ico2 = ofd.FileName;
}
}
private void ibutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico3.Text = ofd.FileName;
s.ico3 = ofd.FileName;
}
}
private void ibutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico4.Text = ofd.FileName;
s.ico4 = ofd.FileName;
}
}
private void ibutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico5.Text = ofd.FileName;
s.ico5 = ofd.FileName;
}
}
private void ibutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico6.Text = ofd.FileName;
s.ico6 = ofd.FileName;
}
}
}
}
And the error is:
Error 1 Inconsistent accessibility: field type 'SharpDock.Properties.Settings' is less accessible than field 'SharpDock.SettingsWindow.s' C:\Users\Lewis\Documents\Visual Studio 2013\Projects\SharpDock\SharpDock\SettingsWindow.cs 14 36 SharpDock
Please help! I am stuck with this error.
~Lewis
You need to use Properties.Settings.Default and you don't need to instantiate a variable for it. If you want an alias to make the code smaller, remove this code:
public Properties.Settings s = new Properties.Settings();
And add this before the class declaration:
using s = Properties.Settings.Default;
And as deathismyfriend commented, you could reduce a lot your code by creating methods for the repeated code.
Your Settings are saved inside Properties.Settings.Default so you should use it instead of declaring your own settings. Also, you can subscribe multiple controls to the same event. What you should do is, subscribe all of your abuttons to aButton_Click event and ibuttons to iButton_Click event. Here is your code fixed:
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpDock
{
public partial class SettingsWindow : Form
{
public SettingsWindow()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
app1.Text = Properties.Settings.Default.app1;
app2.Text = Properties.Settings.Default.app2;
app3.Text = Properties.Settings.Default.app3;
app4.Text = Properties.Settings.Default.app4;
app5.Text = Properties.Settings.Default.app5;
app6.Text = Properties.Settings.Default.app6;
ico1.Text = Properties.Settings.Default.ico1;
ico2.Text = Properties.Settings.Default.ico2;
ico3.Text = Properties.Settings.Default.ico3;
ico4.Text = Properties.Settings.Default.ico4;
ico5.Text = Properties.Settings.Default.ico5;
ico6.Text = Properties.Settings.Default.ico6;
}
private void Accept_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void aButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == abutton1)
{
app1.Text = ofd.FileName;
Properties.Settings.Default.app1 = ofd.FileName;
}
else if (sender == abutton2)
{
app2.Text = ofd.FileName;
Properties.Settings.Default.app2 = ofd.FileName;
}
else if (sender == abutton3)
{
app3.Text = ofd.FileName;
Properties.Settings.Default.app3 = ofd.FileName;
}
else if (sender == abutton4)
{
app4.Text = ofd.FileName;
Properties.Settings.Default.app4 = ofd.FileName;
}
else if (sender == abutton5)
{
app5.Text = ofd.FileName;
Properties.Settings.Default.app5 = ofd.FileName;
}
else if (sender == abutton6)
{
app6.Text = ofd.FileName;
Properties.Settings.Default.app6 = ofd.FileName;
}
}
}
}
private void iButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == ibutton1)
{
ico1.Text = ofd.FileName;
Properties.Settings.Default.ico1 = ofd.FileName;
}
else if (sender == ibutton2)
{
ico2.Text = ofd.FileName;
Properties.Settings.Default.ico2 = ofd.FileName;
}
else if (sender == ibutton3)
{
ico3.Text = ofd.FileName;
Properties.Settings.Default.ico3 = ofd.FileName;
}
else if (sender == ibutton4)
{
ico4.Text = ofd.FileName;
Properties.Settings.Default.ico4 = ofd.FileName;
}
else if (sender == ibutton5)
{
ico5.Text = ofd.FileName;
Properties.Settings.Default.ico5 = ofd.FileName;
}
else if (sender == ibutton6)
{
ico6.Text = ofd.FileName;
Properties.Settings.Default.ico6 = ofd.FileName;
}
}
}
}
}
}
You also need items that you're referencing from your settings to be set inside your settings:
Related
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
MessageBox.Show("Files Selected");
foreach(string filename in ofd.FileNames)
{
}
}
}
in this situation, i just wanna kill process which is selected by OpenFileDialog so that I can't use selectedprogram during this code is running... help me plz
As you can see from comments, the solution is partial, but you could search if among the running processes there are some with the same name as the selected files
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "All files(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
MessageBox.Show("Files Selected");
foreach (string filename in ofd.FileNames)
{
var proc = System.Diagnostics.Process.GetProcesses().Where(w => w.ProcessName == System.IO.Path.GetFileNameWithoutExtension(filename)).FirstOrDefault();
if (proc != null)
proc.Kill(); // proc.Kill(true); to kill entire process tree
}
}
In my WpfAPP I use a button to select file to read the fileName the code is
private void Select_File(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "|*.csv;*.xls;*.xlsx",
Title = "Read File",
Multiselect = false
};
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}
}
but the fileName is ""
How to read the fileName successfully?
your filter syntax is wrong. Try this
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "CSV files (*.csv)|*.csv|Excel (*.xls)|*.xls";
ofd.Title = "Read File";
ofd.Multiselect = false;
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}
so what I'm trying to do is load a .txt file and once the .txt file is loaded it will show the contents from the .txt file in a listView.
Here is my load code.
List<String> proxies = new List<string>();
private void loadProxiesToolStripMenuItem_Click(object sender, EventArgs e)
{
loadProxies();
}
private void loadProxies()
{
this.Invoke(new MethodInvoker(delegate
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
proxies = new List<string>();
Parallel.ForEach(System.IO.File.ReadLines(ofd.FileName), (line, _, lineNumber) =>
{
if (line.Contains(":"))
{
//loadedCombo.Add(line);
proxies.Add(line);
}
else
{
//MessageBox.Show("Hmm, thats not a combolist - please try again");
}
});
}
txt_proxies.Text = "Proxies Loaded: " + proxies.Count.ToString();
}));
}
and I'm wanting it to show in the listView which is named "proxyView".
So what I'm trying to say it, I can get the .txt to load and it changes the count but it's not adding the contents from the .txt file into the listview.
Thanks much appreciated.
To add an item to a ListView you can use yourListView.Items.Add(text)
For example:
private void loadProxies()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
foreach (var line in System.IO.File.ReadLines(ofd.FileName))
{
if (line.Contains(":"))
proxyView.Items.Add(line);
}
}
}
I have a question about the opfiledialog function within c#. When i dont select a file with openfiledialog it put's a text automaticly in my textbox. That text will be "filedialog1". What can i do to fix this.
using System;
using System.Windows.Forms;
using System.IO;
namespace Flashloader
{
public partial class NewApplication : Form
{
private toepassinginifile _toepassinginifile;
private controllerinifile _controllerinifile;
//private controllerinifile _controlIniFile;
public NewApplication(toepassinginifile iniFile)
{
_controllerinifile = new controllerinifile();
_toepassinginifile = iniFile;
InitializeComponent();
controllerComboBox.DataSource = _controllerinifile.Controllers;
}
public bool Run()
{
var result = ShowDialog();
return result == System.Windows.Forms.DialogResult.OK;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
openFileDialog1.Title = ("Choose a file");
openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
}
}
private void button3_Click(object sender, EventArgs e)
{
Toepassing toepassing = new Toepassing();
toepassing.Name = nameBox.Text;
toepassing.Controller = (Flashloader.Controller)controllerComboBox.SelectedItem;
toepassing.TabTip = descBox.Text;
toepassing.Lastfile = openFileDialog1.FileName;
fileBox.Text = openFileDialog1.FileName;
if (nameBox.Text == "")
MessageBox.Show("You haven't assigned a Name");
else if (controllerComboBox.Text == "")
MessageBox.Show("You haven't assigned a Controller");
else if (descBox.Text == "")
MessageBox.Show("You haven't assigned a Desciption");
else if (fileBox.Text == "")
MessageBox.Show("You haven't assigned a Applicationfile");
_toepassinginifile.ToePassingen.Add(toepassing);
_toepassinginifile.Save(toepassing);
MessageBox.Show("Save Succesfull");
DialogResult = DialogResult.OK;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
var newcontroller = new Newcontroller(_controllerinifile);
newcontroller.ShowDialog();
controllerComboBox.DataSource = null;
controllerComboBox.DataSource = _controllerinifile.Controllers;
}
}
}
Thanks all for the help
private void button3_Click(object sender, EventArgs e)
{
toepassing.Lastfile = openFileDialog1.FileName;// Dont do this
fileBox.Text = openFileDialog1.FileName; //or this
Its unclear to me why you are holding onto an Open file dialog I would personally do the following
using(OpenFileDialog ofd = new OpenFileDialog())
{
if(ofd.ShowDialog() == DialogResult.OK)
{
classStringVariable = ofd.FileName;
fileBox.Text = ofd.FileName;
}
}
Then in button 3
toepassing.LastFile = classStringVariable ;
fileBox.Text = classStringVariable ;
When you use the Form Designer to add an OpenFileDialog control on you form, the designer assigns at the property FileName the value openFileDialog1.
I suppose you have set something as the initial value for the property FileName. Then in button_click3 you have no mean to check for the DialogResult and thus you get inconditionally this default back.
Fix it removing this default from the designer FileName property
Just add openFileDialog1.FileName= ""; before you show the dialog.
openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
openFileDialog1.Title = ("Choose a file");
openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
{
fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
}
In your button3_Click event you're checking for an empty string file name anyways, so they would get the correct error message and they wouldn't have some weird arbitrary default name show up when they open the dialog.
I need to use an specific file from the ones you select from a file dialog.
OpenFileDialog ofd = new OpenFileDialog();
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
label23.Text = ofd.SafeFileName;
}
else
{
label23.Text = "No files selected...";
}
}
What i need to do is select and use the files I pre-define, so if I define an event with 01.wav if the user selects a file named 01.wav, that one will be used like so:
using (SoundPlayer player = new SoundPlayer(Beatpadpc.Properties.Resources._01))
{
player.Play();
}
I currently have it adapted as it will play it from the resources, what i need to do is to play the file from the file selecion, but only if the file is named "01".wav
Is there a way for doing it?
Well, just filter the ofd property called Filenames.
OpenFileDialog ofd = new OpenFileDialog();
string strAudioFilePath = String.Empty;
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
// Here you make a small filter with linq
label23.Text = strAudioFilePath = ofd.FileNames.FirstOrDefault(x => x.EndsWith("01.wav")); // you change 01.wav to something that make more sense to you
}
else
{
label23.Text = "No files selected...";
}
}
Then if you want to get rid of your resource file, just give your sound player the path of the file and that's all.
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.Play();