C# ListBox syncing with each other - c#

I am trying to make a program that has 3 listBoxes an I need to alphabetize 1 of them but the and the other 2 listboxes info needs to still match the first listBox. As of now I don't have any code for that to show, but I do have my add item code to give a general idea of what I am looking for.
private void addButton_Click(object sender, EventArgs e)
{
if (itemTextBox.Text == "")
MessageBox.Show("Please enter an item to add");
else
groceryListBox.Items.Add(itemTextBox.Text);
itemTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
//if (quanityListBox.Text == "")
// MessageBox.Show("Please enter an item to add");
//else
quanityListBox.Items.Add(quanityTextBox.Text);
quanityTextBox.Clear();
// countTextBox.Text = quanityListBox.Items.Count.ToString();
//if (priceListBox.Text == "")
// MessageBox.Show("Please enter an item to add");
//else
priceListBox.Items.Add(priceTextBox.Text);
priceTextBox.Clear();
//countTextBox.Text = priceListBox.Items.Count.ToString();
itemTextBox.Focus();
}
private void removeSingleButton_Click(object sender, EventArgs e)
{
if (groceryListBox.SelectedIndex != -1)
{
while(groceryListBox.SelectedItems.Count != 0)
{
groceryListBox.Items.Remove(groceryListBox.SelectedItems[0]);
}
}
else
{
MessageBox.Show("Please Select an item(s) to remove");
}
}
private void saveButton_Click(object sender, EventArgs e)
{
Settings.Default["List1"] = groceryListBox.Items.ToString();
Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
groceryListBox.Text = Settings.Default["List1"].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
groceryListBox.Items.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().Equals(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
private void colorButton_Click(object sender, EventArgs e)
{
groceryListBox.BackColor = Color.DeepSkyBlue;
}
private void containsToolStripMenuItem_Click(object sender, EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().Contains(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
}
private void startsWithToolStripMenuItem_Click(object sender, EventArgs e)
{
{
groceryListBox.SelectedItems.Clear();
for (int i = groceryListBox.Items.Count - 1; i >= 0; i--)
if (groceryListBox.Items[i].ToString().ToLower().StartsWith(searchTextBox.Text.ToLower()))
groceryListBox.SetSelected(i, true);
}
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
editTextBox.Text = groceryListBox.SelectedItem.ToString();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
editTextBox.Paste();
}
private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (editTextBox.Text == "")
MessageBox.Show("Please enter an item to add");
else
groceryListBox.Items[groceryListBox.SelectedIndex] = editTextBox.Text;
editTextBox.Clear();
countTextBox.Text = groceryListBox.Items.Count.ToString();
}
Any help would be appreciated. Thank you.
Edit.... I figured out how to post screenshot....
At least they allow me a link for now, I guess until my reputation goes up. :-)
This is what I want it to do, if I add an item to the list with quantity and price, and I have multiple items in the list I still want the quantity and price to match that item across from it.
This might help too.....
With the items matching the price and quantity.

If I'm reading your question right, you're looking for an easy way to show the same info between multiple ListBoxs? If so you could try the following:
private List<string> testList = new List<string>();
private void BindTestListData()
{
listBox1.DataSource = null;
listBox1.DataSource = testList;
listBox2.DataSource = null;
listBox2.DataSource = testList;
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(InputTextBox.Text))
{
testList.Add(InputTextBox.Text);
InputTextBox.Clear();
BindTestListData();
}
}
Feed all your text from your text box into a list instead of adding it directly to the ListBox. Then bind the list to the datasource of your ListBox.
EDIT
Scratch what I had before. I think I better understand your problem now. try something like this:
private struct Item
{
public string Name { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
}
private List<Item> items = new List<Item>();
private void BindTestListData()
{
listBox1.DataSource = null;
listBox1.DataSource = items;
listBox1.DisplayMember = "Name";
listBox2.DataSource = null;
listBox2.DataSource = items;
listBox2.DisplayMember = "Quantity";
}
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(InputTextBox.Text))
{
Item newItem = new Item()
{
Name = InputTextBox.Text,
Quantity = "1",
Price = "$10"
};
items.Add(newItem);
InputTextBox.Clear();
BindTestListData();
}
}
Obviously, the numbers in the button1_click() method are just place holders, but something like this should work.

Related

Customization censored in textBox

I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character

simple adding up value in windows forms ASP.NET trough button not working

i have a problem where when i try to add a value trough the click_button event, the value will remain the same, could someone help me out with this?
int parallelgroepnummer = 0;
protected void Page_Load(object sender, EventArgs e)
{
CheckBox1.Checked = false;
checkparallel = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (checkparallel == true)
{
{
parallelgroepen.Add(parallelgroepnummer, "test");
parallelgroepnummer = parallelgroepnummer + 1;
ListBox1.Items.Add("parallele groep " + parallelgroepnummer);
}
}
else
{
ListBox1.Items.Add("notparallel");
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkparallel == false)
{
checkparallel = true;
}
else
{
checkparallel = false;
}
}
the answer always returns 1 here.
int parallelgroepnummer = 0;
change in:
private static int parallelgroepnummer = 0;

How can I amend a record that I have inputting?

I am creating a To Do List using Windows Forms.
This is what I have so far.
The code for this form is as follows
namespace To_Do_List
{
public partial class To_Do_List : Form
{
public To_Do_List()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
{
Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
}
private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form
}
private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
{
AmmendItem CreateForm = new AmmendItem(this);
CreateForm.Show();
}
private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
{
AddItem CreateForm = new AddItem(this);
CreateForm.Show();
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
{
}
private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
{
if (listView1.SelectedItems != null)
{
var confirmation = MessageBox.Show(
"Are you sure you want to delete this?",
"WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
}
}
}
}
}
To add a task, the form looks like this
And again, the code is as follows
namespace To_Do_List
{
public partial class AddItem : Form
{
To_Do_List home;
public AddItem(To_Do_List parent)
{
InitializeComponent();
home = parent;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void openListToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
{
}
public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
{
}
public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close(); //causes the window to close but keeps the application running
}
private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form displaying copyright information
}
public void button1_Click(object sender, EventArgs e) //create task button
{
ListViewItem item1 = new ListViewItem(Title.Text);
item1.SubItems.Add(Description.Text);
item1.SubItems.Add(Priority.Text);
item1.SubItems.Add(Date.Text);
string value = "";
bool isChecked = Completed.Checked;
if (isChecked)
item1.SubItems.Add(Completed.Text);
else
value = null;
home.listView1.Items.Add(item1);
this.Close();
}
private void Date_ValueChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Closes the window
{
this.Close();
}
}
}
The Amend form is exactly the same as the New Task form. How would I go about being able to select a record, pressing the amend button and actually changing?
I'm pretty stumped.
Also, this isn't really part of this question but I'll ask it just in case someone knows.
How would I go about being able to actually save these records so that they are there when I open the application back up again?
Thank you very much for reading, I know that I have just dumped everything but I'm really new to Windows Forms so I didn't want to accidentally miss something important out.
Edit
Am I on the right road with something like this?
public void button1_Click(object sender, EventArgs e)
{
To_Do_List editform = new To_Do_List(Title.Text);
editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;
Still can't get it to work :/
This code for popup window.
public partial class frmToDoDetails : Form
{
public string TaskTitle { get; set; }
public string Description { get; set; }
public bool EditMode { get; set; }
public frmToDoDetails()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
TaskTitle = txtTitle.Text;
Description = txtDesc.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void frmToDoDetails_Load(object sender, EventArgs e)
{
if (EditMode)
{
txtTitle.Text = TaskTitle;
txtDesc.Text = Description;
}
else {
txtTitle.Text = string.Empty;
txtDesc.Text = string.Empty;
}
txtTitle.Focus();
}
}
And this for list
public partial class frmToDo : Form
{
public frmToDo()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
frmToDoDetails frm = new frmToDoDetails();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] arr = new string[2];
ListViewItem itm;
arr[0] = frm.TaskTitle;
arr[1] = frm.Description;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
private void frmToDo_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("Title", 100);
listView1.Columns.Add("Desc", 70);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem currentItem= listView1.SelectedItems[0];
frmToDoDetails frm = new frmToDoDetails();
frm.TaskTitle = currentItem.SubItems[0].Text;
frm.Description = currentItem.SubItems[1].Text;
frm.EditMode = true;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentItem.SubItems[0].Text=frm.TaskTitle;
currentItem.SubItems[1].Text=frm.Description;
}
}
}
I am not added your complete fields(priority, date,etc..).
I hope it will help you.

Text property not changing?

What is happening is, even though the questionNr is set to 1, it's not changing the .Text properties of ans1-4, as well as the questionLabel. Any help would be appreciated. Also as a sub-question, is it possible to do something along the lines of if(ans1.Clicked = true)?
public partial class Form1 : Form
{
int pointCounter = 0;
private SoundPlayer _soundPlayer;
int questionNr = 1;
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.amazon.com/Chuck-Seasons-One-Five-Blu-ray/dp/B007AFS0N2");
}
private void Form1_Load(object sender, EventArgs e)
{
_soundPlayer.PlayLooping();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void muteButton_Click(object sender, EventArgs e)
{
if (muteButton.Text == "Mute")
{
muteButton.Text = "Unmute";
_soundPlayer.Stop();
}
else
{
muteButton.Text = "Mute";
_soundPlayer.PlayLooping();
}
}
private void playButton_Click(object sender, EventArgs e)
{
ans1.Visible = true;
ans2.Visible = true;
ans3.Visible = true;
ans4.Visible = true;
playButton.Visible = false;
}
public void question()
{
if (questionNr == 1)
{
questionLabel.Text = "What is Chuck's full name?";
ans1.Text = "Charles Irving Bartowski";
ans2.Text = "Charles Richard Bartowski";
ans3.Text = "Charles Luke Bartowski";
ans4.Text = "Zachary Strahovski";
}
}
private void ans1_Click(object sender, EventArgs e)
{
}
private void ans2_Click(object sender, EventArgs e)
{
}
private void ans3_Click(object sender, EventArgs e)
{
}
private void ans4_Click(object sender, EventArgs e)
{
}
}
}
Form where you invoke the question() method. First call that method from where you need.
eg: FormLoad/Button click etc..Then try
public Form1()
{
InitializeComponent();
_soundPlayer = new SoundPlayer("song.wav");
question();
}
It's good if you put a break point in your Form Load event and see how your code executed.Then you'll get an idea about the flow of your code.

Add item to ListView from another class

I've looked up many question on how to do this, but none of them seem to work. I have a ListView on my main view, and I'm populating it from adding data to it from another form. On the other form, I have a couple strings from text fields I'd like to pass to it. Here's my code:
// Add new booking form:
namespace Booker
{
public partial class newBookingForm : Form
{
public newBookingForm()
{
InitializeComponent();
}
private void NewBookingForm_Load(object sender, EventArgs e)
{
roomField.Focus();
}
private void newBookingButton_Click(object sender, EventArgs e)
{
// Add to ListView
// Add to Parse
string room = this.roomField.Text;
string date = this.datePicker.Value.ToShortDateString();
string time = this.timeField.Text;
string person = "<username>"; // Get current Parse user
Booker booker = new Booker();
string[] array = new string[4] { room, date, time, person };
booker.UpdatingListView(array);
this.Hide();
}
}
}
// ListView form:
namespace Booker
{
public partial class Booker : Form
{
delegate void MyDelegate(string[] array);
public Booker()
{
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
Environment.Exit(0);
}
private void Booker_Load(object sender, EventArgs e)
{
listView.View = View.Details;
listView.AllowColumnReorder = false;
}
/* Methods for the Booker form */
private void newBookingButton_Click(object sender, EventArgs e)
{
newBookingForm nbf = new newBookingForm();
nbf.Show();
}
public void UpdatingListView(string[] array)
{
if (this.listView.InvokeRequired)
this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
else
{
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(array[1]);
this.listView.Items.Add(lvi);
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Error - no action being sent
}
private void helpButton_Click(object sender, EventArgs e)
{
// Display help panel
}
private void contactButton_Click(object sender, EventArgs e)
{
// Open panel/email
}
private void newBooking_Click(object sender, EventArgs e)
{
newBookingButton_Click(sender, e);
//string[] row = { "Meeting room", "April 11, 2013", "12:00PM-1:00PM", "Ryan" };
//var listViewItem = new ListViewItem(row);
//listView.Items.Add(listViewItem);
}
Try this in UpdatingListView:
ListViewItem lvi = new ListViewItem(array[0],0);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
lvi.SubItems.Add(array[3]);
this.listView.Items.Add(lvi);
This will create a column in the first position of your ListView with the 4 items of your array as rows in that column.
To add items from another class, just access the other class' method to add from Program. Like this:
// Class we're adding from:
string name = "Ryan";
string site = "imryan.net";
string[] array = new string[2] = { name, site };
Program.classWithListView.UpdatingListView(array);
// Class we're adding to:
public void UpdatingListView(string[] array)
{
if (this.listView.InvokeRequired)
{
this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
} else {
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
this.listView.Items.Add(lvi);
}
}
The string array will be added to the ListView as SubItems.

Categories