How can I use a value from the reading function in the button1_Click function?
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
string[] files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
...
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (string file in files_in_folder) // How do I access files_in_folder?
{
ListViewItem li = new ListViewItem(file);
}
}
You need to store it somehow, for example as a private member:
string some_value = null;
public void reading(object sender, EventArgs e)
{
some_value = "Foobar";
}
private void button1_Click(object sender, EventArgs e)
{
if (some_value != null)
{
// ...
}
}
// Make it a member variable
private string[] mFilesInFolder = null;
public void reading(object sender, EventArgs e)
{
DialogResult reading_from_folder = new DialogResult();
reading_from_folder = folderBrowserDialog1.ShowDialog();
if (reading_from_folder == DialogResult.OK)
{
mFilesInFolder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
}
private void button1_Click(object sender, EventArgs e)
{
DoFileInFolderOperation();
}
private void DoFilesInFolderOperation()
{
if(mFilesInFolder != null)
{
foreach (string file in mFilesInFolder)
{
ListViewItem li = new ListViewItem(file);
}
}
}
Related
I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}
I am doing a memory matching game in c#
when the user matches 2 images i want them to disappear or make them invisible
i am still new to coding and this is what i did so far but the images wont be invisible
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
}
As others have stated you'll need to store all of the Resources just once. Here is a possible example:
class MyForm
{
private Dictionary<String, Image> images = new Dictionary<String, Image>();
public void Init()
{
images["apple"] = Properties.Resources.apple;
}
public void Dispose()
{
foreach(var item in myDictionary.Values)
{
item.Dispose();
}
}
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = images["apple"];
bool condition = true;
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = images["apple"];
}
}
When you set apple1.Image = Properties.Resources.apple; Note that a copy of the value (Image) of the Properties.Resources.apple is set to apple1.Image, same thing happens when you assign to apple2.Image, so although visually they seem to have the same image, but they are pointing to different images (in memory).
You may do one of following:
1-Set your images to a dictionary and then load apple1.Image and apple2.Image from it:
Dictionary<string, Image> Images = new Dictionary<string, Image>();
Images.Add("apple", Properties.Resources.apple);
Image apple = Properties.Resources.apple;
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Images["apple"];
if (apple1.Image==apple2.Image)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Images["apple"];
}
2-Just use another property to compare equality:
private void apple1_Click(object sender, EventArgs e)
{
apple1.Image = Properties.Resources.apple;
apple1.Tag = "apple";
if ((string)apple1.Tag==(string)apple2.Tag)
{
apple1.Visible = false;
apple2.Visible = false;
}
}
private void apple2_Click(object sender, EventArgs e)
{
apple2.Image = Properties.Resources.apple;
apple2.Tag = "apple";
}
I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}
I want to remove an item from a list...
I am obviously missing something...I have tried just about every variation including EXCEPT, REMOVE, etc...
When debugging, I step through each ling, but when it gets to btnRemove_Click, it steps through removing but does not remove anything...it acts as if I never sent a command to remove anything???
Help!
public partial class frmUpdate : Form
{
private Student student = new Student();
private string _scores;
public frmUpdate()
{
InitializeComponent();
}
public string GetUpdatedScores(Student s)
{
txtName.Text = s.Name;
_scores = s.Scores;
FillStudentGrades();
this.ShowDialog();
return _scores;
}
private void FillStudentGrades()
{
lstScores.Items.Clear();
string[] grades = splitGrades(_scores);
foreach (string s in grades)
{
lstScores.Items.Add(s.ToString());
}
}
private void lstScores_SelectedIndexChanged(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddScore addScore = new frmAddScore();
_scores += " " + addScore.AddScore();
FillStudentGrades();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (lstScores.SelectedIndex < 0)
{
MessageBox.Show("You Must Select A Grade.");
btnUpdate.Focus();
}
else
{
int i = lstScores.SelectedIndex;
string[] grades = splitGrades(_scores);
string message = "Are you sure you want to remove " + grades[i].ToString() + "?";
DialogResult button = MessageBox.Show(message, "Confirm Remove",
MessageBoxButtons.YesNo);
if (button == DialogResult.Yes)
{
int count = 0;
foreach (char c in grades[i])
{
if (char.IsDigit(c))
{
count++;
}
}
int a = _scores.IndexOf(grades[i].ToString());
_scores = _scores.Remove(a, (count + 1));
FillStudentGrades();
btnOk.Focus();
}
else
{
btnOk.Focus();
}
}
}
private void btnClearAll_Click(object sender, EventArgs e)
{
}
private void btnOk_Click(object sender, EventArgs e)
{
student.Name = txtName.Text;
student.Scores = _scores;
this.Close();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
public string[] splitGrades(string s)
{
string[] grades = s.Split(' ');
return grades;
}
}
In C#, strings are immutable. _scores.Remove(i); doesn't change _scores. Instead it returns a new string object that you can assign to a variable, for example, back to _scores like this:
_scores = _scores.Remove(i);
you need to use RemoveAt methos - as you are trying to remove index and not the value
I have this simple code in my DevExpress LookUp control (should be identical with a normal combobox)
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
lookUpUsers.EditValue = null;
}
The problem is that when I select a value in lookUpUsers, is resets the other lookup which then resets lookUpUsers. So when I pick a value, both combobox become null. What I want is that when you pick a value in combobox 1, combobox 2 resets its value.
How about this:
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if(lookUpUsers.EditValue != null)
lookUpRolesPréÉdit.EditValue = null;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if(lookUpRolesPréÉdit.EditValue != null)
lookUpUsers.EditValue = null;
}
There might be an easier way than this, as my knowledge of C# is limited (especially their libraries like you are using them here). Nevertheless, this is an answer that uses no magic provided by libraries:
private bool localEdit = false;
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpRolesPréÉdit.EditValue = null;
localEdit = false;
}
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!localEdit)
{
localEdit = true;
lookUpUsers.EditValue = null;
localEdit = false;
}
}
Here's a solution I've come up with
private void lookUpUsers_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpRolesPréÉdit.EditValue = null;
}
isEditFinished = false;
}
private void lookUpRolesPréÉdit_EditValueChanged(object sender, EventArgs e)
{
if (!isEditFinished)
{
isEditFinished = true;
lookUpUsers.EditValue = null;
}
isEditFinished = false;
}