accessing form elements from a class - c#

i was trying to make this code:
public void _open_Click_1(object sender, EventArgs e)
{
_playList.Items.Clear();
_openFile.Multiselect = true;
_openFile.Filter = "Mp3 Files|*.mp3|Avi Files|*.avi|Mp4 Files|*.mp4";
_openFile.ShowDialog();
doc = _openFile.SafeFileNames;
path = _openFile.FileNames;
for (int i = 0; i < doc.Length; i++)
{
_playList.Items.Add(doc[i]);
}
}
from a class
so i created a class called mplayer
and then an instance of the form
and put all that code in there
what suppose to happen is when i click a button
file dialog opens
and all selected song names would go into a listbox
for some reason it doesn't open and no compilation nor exception errors pops
please advise

As I can see the event handler is correctly assigned. Btw try this code, it is working in my project:
public Form1()
{
InitializeComponent();
_open.Click += new EventHandler(_open_Click_1);
}
private void _open_Click_1(object sender, EventArgs e)
{
_openFile = new OpenFileDialog();
_playList.Items.Clear();
_openFile.Multiselect = true;
_openFile.Filter = "Mp3 Files|*.mp3|Avi Files|*.avi|Mp4 Files|*.mp4";
_openFile.ShowDialog();
var doc = _openFile.SafeFileNames;
var path = _openFile.FileNames;
for (int i = 0; i < doc.Length; i++)
{
_playList.Items.Add(doc[i]);
}
}
Note: I was using local variables for the selected file names and path.

maybe i wasn't explaining myself very well. im trying to call a class which does that. like this
public static class mplayer
{
public static void openMusic()
{
PhoenixDownloader.searchForm frm = new searchForm();
frm._playlist.items.clear();
frm._openfile.multiselect = true;
frm._openfile.filter = "mp3 files|*.mp3|avi files|*.avi|mp4 files|*.mp4";
frm._openfile.showdialog();
frm.doc = frm._openfile.safefilenames;
frm.path = frm._openfile.filenames;
for (int i = 0; i < frm.doc.length; i++)
{
frm._playlist.items.add(frm.doc[i]);
}
}
}
}
and on the event part:
public void _open_Click_1(object sender, EventArgs e)
{
mplayer();
}
and it it doesn't happen

Related

C# Windows form application how to delete selected listbox line and text file

I have problem with one function. Function "Usun" must delete both line in listbox and the same data from txt file** Can anyone help me?
using System;
namespace BazaKlientow2
{
public partial class Form1 : Form
{
private Klient[] lista = new Klient[1];
public Form1()
{
InitializeComponent();
}
private void Write(Klient obj)
{
StreamWriter sw = new StreamWriter("Klienci.txt");
sw.WriteLine(lista.Length + 1);
sw.WriteLine(obj.Imie);
sw.WriteLine(obj.Nazwisko);
sw.WriteLine(obj.Firma);
sw.WriteLine(obj.NIP);
for(int x = 0; x <lista.Length; x++)
{
sw.WriteLine(lista[x].Imie);
sw.WriteLine(lista[x].Nazwisko);
sw.WriteLine(lista[x].Firma);
sw.WriteLine(lista[x].NIP);
}
sw.Close();
}
private void Read()
{
StreamReader sr = new StreamReader("Klienci.txt");
lista = new Klient[Convert.ToInt32(sr.ReadLine())];
for (int x = 0; x < lista.Length; x++)
{
lista[x] = new Klient();
lista[x].Imie = sr.ReadLine();
lista[x].Nazwisko = sr.ReadLine();
lista[x].Firma = sr.ReadLine();
lista[x].NIP = sr.ReadLine();
}
sr.Close();
}
private void Display()
{
listaKlientow.Items.Clear();
for( int x=0; x < lista.Length; x++)
{
listaKlientow.Items.Add(lista[x].ToString());
}
}
private void ClearForm()
{
txtImie.Text = String.Empty;
txtNazwisko.Text = String.Empty;
txtFirma.Text = String.Empty;
txtNip.Text = String.Empty;
}
private void dodaj_Click(object sender, EventArgs e)
{
Klient obj = new Klient();
obj.Imie = txtImie.Text;
obj.Nazwisko = txtNazwisko.Text;
obj.Firma = txtFirma.Text;
obj.NIP = txtNip.Text;
Write(obj);
Read();
Display();
ClearForm();
}
private void Form1_Load(object sender, EventArgs e)
{
Read();
Display();
}
private void sortuj_Click(object sender, EventArgs e)
{
Sortowanie();
Display();
}
private void Sortowanie()
{
Klient temp;
bool swap;
do
{
swap = false;
for(int x=0;x<lista.Length -1;x++)
{
if(lista[x].Imie.CompareTo(lista[x+1].Nazwisko) >0)
{
temp = lista[x];
lista[x] = lista[x + 1];
lista[x + 1] = temp;
swap = true;
}
}
} while (swap == true);
}
private void usun_Click(object sender, EventArgs e)
{
Usun();
}
private void Usun()
{
**//i cant do this. this function must delete both line in listbox and the same data from txt file**
}
}
}
Function "Usun" must delete both line in listbox and the same data from txt file** Can anyone help me?
You need to read the lines from the file and store then in a List<string> memory. You also need to set the DataSource property of the ListBox to those lines. When you want to remove a line, remove it from the List<string> in memory. Then write the whole list back to the file. Then reset the DataSource property of the ListBox.
Here is an example. In this example, I create a file with "1", "2" and "3" as the lines. Then when the user clicks a button, I remove "1" from the list in memory and write the list to the file. Then I refresh the listbox.
public partial class Form1 : Form {
private List<string> linesInFile;
public Form1() {
InitializeComponent();
File.WriteAllLines( "Lines.txt", new string[] { "1", "2", "3" } );
this.linesInFile = File.ReadAllLines( "Lines.txt" ).ToList();
this.listBox1.DataSource = this.linesInFile;
}
private void Remove_Click(object sender, EventArgs e) {
this.linesInFile.Remove( "1" );
File.WriteAllLines( "Lines.txt", this.linesInFile );
this.listBox1.DataSource = null;
this.listBox1.DataSource = this.linesInFile;
}
}

How to keep multiple folder choices?

I have a folder browser button and a combobox which shows selected path. I want it keep previously selected paths. I wrote this code and it works fine. But I wanna know if there is more efficient way to do this.
private void Form1_Load_1(object sender, EventArgs e)
string hurrem, hurrem2, hurrem3, hurrem4, hurrem5;
hurrem = Settings.Default["com1"].ToString();
hurrem2 = Settings.Default["com2"].ToString();
hurrem3 = Settings.Default["com3"].ToString();
hurrem4 = Settings.Default["com4"].ToString();
hurrem5 = Settings.Default["com5"].ToString();
comboBox2.Items.Add(hurrem);
comboBox2.Items.Add(hurrem2);
comboBox2.Items.Add(hurrem3);
comboBox2.Items.Add(hurrem4);
comboBox2.Items.Add(hurrem5);
comboBox2.SelectedIndex = 0;
and
private void button1_Click(object sender, EventArgs e)
{
//..code
Settings.Default["com5"] = Settings.Default["com4"];
Settings.Default["com4"] = Settings.Default["com3"];
Settings.Default["com3"] = Settings.Default["com2"];
Settings.Default["com2"] = Settings.Default["com1"];
Settings.Default["com1"] = path1.ToString();
Settings.Default.Save();
You can use the LimitedStack from here: Limit size of Queue<T> in .NET?. You could then add methods for serializing/deserializing the data using Settings.Default.
public void Save()
{
int i = 0;
foreach (var item in _stack)
{
Settings.Default["com" + i++] = item;
}
Settings.Default.Save();
}
public void Load()
{
for (int i = 0; i < Limit; i++)
{
_stack.Add((T)Settings.Default["com" + i++]);
}
}
Storing the items (assuming you have the limited stack saved in a variable called myStack):
myStack.Push(path1.ToString());
myStack.Save();
Adding the items to the combobox:
for (int i = 0; i < myStack.Limit; i++)
{
comboBox2.Items.Add(myStack.Pop());
}

Get the index of array of picturebox clicked

I am creating some picturebox dynamically and click event for picturebox as follows
Image myImage = Image.FromFile("image/Untitled6.png");
PictureBox[] txtTeamNames = new PictureBox[5];
for (int i = 0; i < txtTeamNames.Length; i++)
{
var txt = new PictureBox();
txtTeamNames[i] = txt;
txtTeamNames[i].Image = myImage;
txtTeamNames[i].Height = 53;
txtTeamNames[i].Width = 48;
this.panel1.Controls.Add(txtTeamNames[i]);
txtTeamNames[i].Visible = true;
txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);
}
When someone clicks on any picture box, how do I find its array index and name?
void clickEventHandler(object sender, EventArgs e)
{
//???
}
You can access the PictureBox via the sender argument. So try this:
PictureBox[] txtTeamNames;
void YourMethod()
{
Image myImage = Image.FromFile("image/Untitled6.png");
txtTeamNames = new PictureBox[5];
//The same as your code
}
void clcikeventhandle(object sender, EventArgs e)
{
int index = txtTeamNames.IndexOf(sender As PictureBox);
}
EDIT: Approach #2
But if you are not happy with declaring that array in the class scope you can try this approach:
//Same as your code
for (int i = 0; i < txtTeamNames.Length; i++)
{
//Save as your code
txtTeamNames[i].Tag = i; // ADD THIS LINE
}
Then:
void clcikeventhandle(object sender, EventArgs e)
{
int index = int.Parse((sender as PictureBox).Tag.ToString());
}
Another suggestion - create a custom class, which inherits from PictureBox. It will have an extra Index property. And you can set it between these two lines:
txtTeamNames[i].Visible = true;
//assign the index here
txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);
like so:
txtTeamNames[i].Index = i;
Then in the handler:
void clickEventHandle(object sender, EventArgs e)
{
PictureBox pbox = sender As PictureBox;
int index = pbox.Index();
string name = pbox.Name();
}
You keep the same scope of variables, which may be useful if you are concerned about it. If you are okay with upgrading scope of txtTeamNames to class level, see another answer by Hossein Narimani Rad.
namespace your_name_project
{
public partial class Form_Begin : Form
{
PictureBox[] pictureBoxs = new PictureBox[6];
public Form_Begin()
{
InitializeComponent();
pictureBoxs[0] = pictureBox1; pictureBoxs[1] = pictureBox2; pictureBoxs[2] = pictureBox3;
pictureBoxs[3] = pictureBox4; pictureBoxs[4] = pictureBox5; pictureBoxs[5] = pictureBox6;
}
//continue
List<PictureBox> pictureBoxes = new List<PictureBox>();
private void buttonX1_Click(object sender, EventArgs e)
{
for (int i = 0; i <3; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.image_1;// load image1 and Image_2from resource in property of picturebox
}
for (int i = 3; i < 6; i++)
{
pictureBoxs[i].Image = your_name_project.Properties.Resources.Image_2;
}
}
}
}
I've tried the 1st approach cited above, but it did not work for me. I needed to change the syntax in order to work. I am unsure why, but my way for approach 1 to work would be:
PictureBox[] txtTeamNames;
void YourMethod()
{
Image myImage = Image.FromFile("image/Untitled6.png");
txtTeamNames = new PictureBox[5];
//The same as your code
}
void clcikeventhandle(object sender, EventArgs e)
{
int index = Array.IndexOf(txtTeamNames, sender); //DIFFERENT HERE
}
If anyone has an idea...

Delete a set of panels with UserControls created in a loop

I created a panel with a button "add" and a button "delete". If you click on "add" a panel is created right below this header, you can create as much as you want, they are listed.
On each panel there is a checkbox and I would like to delete the panel if the checkbox is checked once the button delete is clicked.
I can get the intuition of a loop : for, but still too novice to get through this without a little tip.
-
public partial class Test_auto : Form
{
ArrayList strategyFutureList = new ArrayList();
public Test_auto()
{
InitializeComponent();
Instance = this;
}
//Create a new future strategy
public void CreateStrategyFuture()
{
ConsoleStrategyItem strategyItemFuture = new ConsoleStrategyItem();
strategyItemFuture.Location = new Point(3, 3);
futureContainer.Height += 85;
strategyFutureList.Add(strategyItemFuture);
futureContainer.Controls.Add(strategyItemFuture);
ConsoleStrategyItem.Instance.txtStrategyName.Text = "Strat Future " + strategyFutureList.IndexOf(strategyItemFuture) + " ";
ConsoleStrategyItem.Instance.Name = "strategyFuture" + strategyFutureList.IndexOf(strategyItemFuture);
ConsoleStrategyItem.Instance.cbxDeleteStrategy.Name = "cbxDeleteFuture" + strategyFutureList.IndexOf(strategyItemFuture);
}
//Makes it appear
private void btnAddStrategyFuture_Click_1(object sender, EventArgs e)
{
CreateStrategyFuture();
}
//Delete a-some selected strategies
public void DeleteStrategyFuture()
{
for (int i = 0; i < strategyFutureList.Count; i++)
{
if (ConsoleStrategyItem.Instance.cbxDeleteStrategy.Checked = true)
{
}
}
}
private void btnDeleteStrategyFuture_Click(object sender, EventArgs e)
{
DeleteStrategyFuture();
}
}
you don't have to create a separate ArrayList to maintain the list of UserControls being added to the futureContainer, you can simply iterate through the Controls collection of it.
simply do this in your DeleteStrategyFuture() method
public void DeleteStrategyFuture()
{
var length=futureContainer.Controls.Count;
foreach(int i=0; i< length; i++)
{
if(futureContainer.Controls[i].GetType()==typeof(ConsoleStrategyItem))
{
bool isChecked =((ConsoleStrategyItem)futureContainer.Controls[i])
.Instance.cbxDeleteStrategy.Checked;
if(isChecked)
{
futureContainers.Controls.Remove(futureContainers.Controls[i]);
}
}
}
}

Save dynamic control state when clicking on save button

I have a small program that generates four dynamic buttons on load in a flowLayoutPanel1 and a static save button.
My aim is to save the dynamic button colors so that when the form is reloaded or opened again the colors of the dynamic buttons are loaded back up in the same state as saved.
Below I will include my code which has been commented to demonstrate what I have attempted.
I am using an xml file to save the button state and included a class that holds the state. However the method I am using works fine if a create my buttons to save statically. (I have only tried it with one static button)
Interface:
class that holds state:
public class MyFormState
{
public string ButtonBackColor { get; set; }
}
Form code:
public partial class Form1 : Form
{
//Form Member
MyFormState state = new MyFormState();
Button btn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//loading xml file if it exists
if (File.Exists("config.xml"))
{
loadConfig();
}
//Genrating dynamic buttons
// flowLayoutPanel1.Controls.Clear();
for (int i = 0; i <= 3; ++i)
{
btn = new Button();
btn.Text = " Equation " + i;
flowLayoutPanel1.Controls.Add(btn);
//click event of buttons
btn.Click += new EventHandler(btn_Click);
}
btn.BackColor = System.Drawing.ColorTranslator.FromHtml(state.ButtonBackColor);
}
//method to load file
private void loadConfig()
{
XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
using (FileStream fs = File.OpenRead("config.xml"))
{
state = (MyFormState)ser.Deserialize(fs);
}
}
//saving the xml file and the button colors
private void writeConfig()
{
using (StreamWriter sw = new StreamWriter("config.xml"))
{
state.ButtonBackColor = System.Drawing.ColorTranslator.ToHtml(btn.BackColor);
XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
ser.Serialize(sw, state);
}
}
int count = 0;
//backcolor change
void btn_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (count == 0)
{
button.BackColor = Color.Red;
count++;
}
else if (count == 1)
{
button.BackColor = Color.Blue;
count--;
}
}
//save file
private void btnSave_Click(object sender, EventArgs e)
{
writeConfig();
}
}
Any suggestions on what i should change so that it works for me? Thanks
Your code works fine but only for the last button. You have 4 buttons but only have one MyFormState object. When creating the buttons you always use the same private field btn. So you need arrays of 4 MyFormState objects and 4 buttons.
MyFormState[] states = new MyFormState[4];
Button[] buttons = new Button[4];
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists("config.xml"))
{
loadConfig();
}
for (int i = 0; i < 4; ++i)
{
buttons[i] = new Button();
buttons[i].Text = " Equation " + i;
flowLayoutPanel1.Controls.Add(buttons[i]);
buttons[i].Click += new EventHandler(btn_Click);
if (states[i] != null)
{
buttons[i].BackColor = ColorTranslator.FromHtml(states[i].ButtonBackColor);
}
}
}
private void loadConfig()
{
XmlSerializer ser = new XmlSerializer(typeof(MyFormState[]));
using (FileStream fs = File.OpenRead("config.xml"))
{
states = (MyFormState[])ser.Deserialize(fs);
}
}
private void writeConfig()
{
for (int i = 0; i < 4; i++)
{
if (states[i] == null)
{
states[i] = new MyFormState();
}
states[i].ButtonBackColor = ColorTranslator.ToHtml(buttons[i].BackColor);
}
using (StreamWriter sw = new StreamWriter("config.xml"))
{
XmlSerializer ser = new XmlSerializer(typeof(MyFormState[]));
ser.Serialize(sw, states);
}
}

Categories