I have created a form that accepts user input for a couple different pieces of info (title, location , date(from a monthCalendar) etc..), when the Add button is clicked the information is stored in the current array element and the Title is displayed in a listBox. When the Title in the listBox is selected the rest of the information for that specific Title is re-populated in the appropriate textBox.
I have been trying to take this a step further with no success. When the Add button is clicked I would like to have the user input saved to the date selected on the monthCalendar. So if the user clicks on a date that has no information saved, the listBox remains empty. If there has been information saved on a date, the listBox would then display the Title(s).
Code snippets:
class MeetingManager
{
private Meeting[] meetings;
public int currentIndex;
public int maxIndex;
private string title;
private string location;
private string startTime;
private string endTime;
private string notes;
public MeetingManager()
{
meetings = new Meeting[10];
currentIndex = -1;
maxIndex = -1;
}
// excluded getter/setters + basic error checking
public void Add()
{
try
{
if (maxIndex >= meetings.Length - 1)
{
throw new ApplicationException("YOU CAN ONLY CREATE 10 MEETINGS");
}
else
{
maxIndex++;
currentIndex = maxIndex;
Meeting temp = new Meeting(Title, Location, StartTime, EndTime, Notes);
meetings[maxIndex] = temp;
Title = meetings[maxIndex].Title;
Location = meetings[maxIndex].Location;
StartTime = meetings[maxIndex].StartTime;
EndTime = meetings[maxIndex].EndTime;
Notes = meetings[maxIndex].Notes;
}
}
catch (ApplicationException ex)
{
throw ex; // toss it up to the presentation
}
}
public void Add(string title, string location, string startTime, string endTime, string notes)
{
try
{
Title = title;
Location = location;
StartTime = startTime;
EndTime = endTime;
Notes = notes;
Add();
}
catch (ApplicationException ex)
{
throw ex;
}
}
public override string ToString()
{
return Title;
}
}
public partial class CalendarForm : Form
{
private MeetingManager mManager; // reference to business layer object
private void calendarSaveChangesButton_Click(object sender, EventArgs e)
{
try
{
mManager.Title = textBoxTitle.Text;
mManager.Location = textBoxLocation.Text;
mManager.StartTime = maskedStartTimeTextBox.Text;
mManager.EndTime = maskedEndTimeTextBox.Text;
mManager.Notes = notesTextBox.Text;
mManager.Add();
meetingListBox.Enabled = true;
meetingListBox.Items.Add(mManager);
//clears the textBoxes after clickng saveChanges
textBoxTitle.Text = "";
textBoxLocation.Text = "";
maskedStartTimeTextBox.Text = "";
maskedEndTimeTextBox.Text = "";
notesTextBox.Text = "";
}
catch (ApplicationException ex)
{
MessageBox.Show(this, ex.Message);
}
}
/// <summary>
/// When a meeting is selected from the listBox, it re-populates
/// the empty fields with the information stored in the array element
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void meetingListBox_SelectedIndexChanged(object sender, EventArgs e)
{
MeetingManager m = meetingListBox.SelectedItem as MeetingManager;
if (m != null)
{
textBoxTitle.Text = m.Title;
textBoxLocation.Text = m.Location;
maskedStartTimeTextBox.Text = m.StartTime;
maskedEndTimeTextBox.Text = m.EndTime;
notesTextBox.Text = m.Notes;
}
}
}
Well, I think you could try something like this:
class MeetingManager
{
...
//add and implement a find method which returns a Meeting-object if there is a
//corresponding meeting date (in private Meeting[] meetings;)
public Meeting MeetingFinder(DateTime meetingTime)
{
//if there is a corresponding meeting-object for the date, return the meeting object
//if there isn't, return null
}
...
}
public partial class CalendarForm : Form
{
...
private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
{
//which date was selected?
var selectedDate = monthCalendar.SelectionRange.Start;
//do we have that date in the meetings?
var meetingOnTheSelectedDate = mManager.MeetingFinder(selectedDate);
if(meetingOnTheSelectedDate != null)
{
//populate your winform with the data from meetingOnTheSelectedDate
}
}
...
}
Related
I made an mp3 player in C#. If I select the songs at once, they automatically play one after the other, but when it comes to a song I added later, I get a "System.IndexOutOfRangeException" error. When I add music later, I want the song to play automatically one after the other, how can I do that?
string[] yol, dosya;
private void Btn_Muzik_Ekle_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
dosya = ofd.SafeFileNames;
yol = ofd.FileNames;
for (int x = 0; x < dosya.Length; x++)
{
Lb_Muzik_Listesi.Items.Add(dosya[x]);
}
}
}
private void Lb_Muzik_Listesi_SelectedIndexChanged(object sender, EventArgs e)
{
//This Is Where I Got The Error
OynatmaEkranı.URL = yol[Lb_Muzik_Listesi.SelectedIndex];
OynatmaEkranı.Ctlcontrols.play();
try
{
var file = TagLib.File.Create(yol[Lb_Muzik_Listesi.SelectedIndex]);
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
Pb_Muzik_Kapak.Image = Image.FromStream(new MemoryStream(bin));
}
catch
{
}
}
private void Zamanlayıcı_Tick(object sender, EventArgs e) //Timer
{
if (OynatmaEkranı.playState==WMPLib.WMPPlayState.wmppsPlaying)
{
Pb_Muzik.Maximum=(int)OynatmaEkranı.Ctlcontrols.currentItem.duration;
Pb_Muzik.Value = (int)OynatmaEkranı.Ctlcontrols.currentPosition;
try
{
Lbl_Muzik_Sure.Text = OynatmaEkranı.Ctlcontrols.currentPositionString;
Lbl_Muzik_Bitis.Text = OynatmaEkranı.Ctlcontrols.currentItem
.durationString.ToString();
}
catch
{
}
}
if (Pb_Muzik.Value==Pb_Muzik.Maximum)
{
if (Lb_Muzik_Listesi.SelectedIndex<Lb_Muzik_Listesi.Items.Count-1)
{
Lb_Muzik_Listesi.SelectedIndex = Lb_Muzik_Listesi.SelectedIndex + 1;
}
}
}
You can avoid this problem managing your data in the ListBox. Create a class with your required info (the file and the name):
public class MuzikItem
{
public MuzikItem(string file)
{
this.Text = System.IO.Path.GetFileNameWithoutExtension(file);
this.Url = file;
}
public string Text { get; set; }
public string Url { get; set; }
public override string ToString()
{
// This is the text to show in ListBox
return this.Text;
}
}
Add items to the ListBox using this class:
foreach (var file in ofd.FileNames)
{
var item = new MuzikItem(file);
Lb_Muzik_Listesi.Items.Add(item);
}
And use it:
var item = Lb_Muzik_Listesi.SelectedItem as MuzikItem;
if (item != null)
{
OynatmaEkranı.URL = item.Url;
OynatmaEkranı.Ctlcontrols.play();
try
{
var file = TagLib.File.Create(item.Url);
var bin = (byte[])file.Tag.Pictures[0].Data.Data;
Pb_Muzik_Kapak.Image = Image.FromStream(new MemoryStream(bin));
}
catch
{
}
}
You try to get the selected item as a MuzikItem (all your items are of this class so this return null only when no item is selected) and with this, you have the Url.
UPDATE
I like manage this things with events. In your Ticks methods:
if (Pb_Muzik.Value == Pb_Muzik.Maximum)
{
OnSongFinished();
}
And create a method to manage this event:
private void OnSongFinished()
{
if (Lb_Muzik_Listesi.SelectedIndex < Lb_Muzik_Listesi.Items.Count - 1)
{
Lb_Muzik_Listesi.SelectedIndex = Lb_Muzik_Listesi.SelectedIndex + 1;
}
else
{
// Stop the player
OynatmaEkrani.Ctlcontrols.stop();
}
}
Having some real trouble understanding where I've gone wrong here. I've marked in the code what and where. I am using an XAML interface and do have objects for everything here. The code compiles but the TextBlock will not update with the result from updateVTCShortCode Thanks for the help!
MAIN PROGRAM
namespace VTCPT
{
/// <summary>
///
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public void shortFormCodec_SelectionChanged(object sender, RoutedEventArgs e)
{
//UPDATE THE SHORTCODE TEXTBLOCK
updateVTCShortCode display = new updateVTCShortCode();
display.mergeShortCode(longFormCodec.SelectedItem.ToString());
if (String.IsNullOrEmpty(display.finalResult()))
{ shortFormCodec.Text = ".."; }
else { shortFormCodec.Text = display.finalResult();
shortFormCodec.Text = "test";
} /////THIS IS NOT ACTUALLY GETTING A RETURN
}
public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void updateShortForm(object sender, SelectionChangedEventArgs e)
{
}
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
}
private void fsSiteBuild_SelectionChanged(object sender, RoutedEventArgs e)
{
}
private void updateSiteBuild(object sender, TextChangedEventArgs e)
{
int index = fsRoomDesig.Text.IndexOf(".");
if (index > 0)
{ fsSiteBuild.Text = fsRoomDesig.Text.Substring(0, index); }
else { fsSiteBuild.Text = ".."; }
}
private void vtcSystemName_SelectionChanged(object sender, RoutedEventArgs e)
{
}
}
}
updateVTCShortCode CLASS
namespace VTCPT
{
class updateVTCShortCode
{
String result = "";
public void mergeShortCode(String longFormCodec)
{ if (longFormCodec.Equals("Cisco SX80"))
{
String sendShortForm = "SX80";
result = "V-T" + sendShortForm;
}
if (longFormCodec.Equals("Cisco Webex Codec Plus"))
{
String sendShortForm = "SRK";
result = "V-T" + sendShortForm;
}
if (longFormCodec.Equals("Cisco Webex Codec Pro"))
{
String sendShortForm = "SRK";
result = "V-T" + sendShortForm;
}
}
public String finalResult()
{ return result; } //////SHOULD BE GETTING SENT BACK TO MAIN PROGRAM
}
}
I think the problem is that in the following code taken from your shortFormCodec_SelectionChanged method. You set shortFormCodec.Text = display.finalResult(); immediately followed by shortFormCodec.Text = "test";. The final result will never be visible because it is being immediately overwritten with "test".
if (String.IsNullOrEmpty(display.finalResult()))
{
shortFormCodec.Text = "..";
}
else
{
shortFormCodec.Text = display.finalResult();
shortFormCodec.Text = "test";
}
As TheGeneral suggested in the comments, you should be able to identify this using breakpoints and stepping through the code (using the F8 key) while watching the values of your variables and text fields. If you hover your mouse over the variables and the .Text section of any shortFormCodec.Text line it will show you its value at that point in the program.
However, I think you may find it helpful if you adjust your code to use an if {} else if {} else {} structure. I would also change the finalResult() method to a property as it's doing nothing but return a string. For example:
class updateVTCShortCode
{
// You could set the default value to an empty string I.e. = ""
// but having it set to "Not set" may help you spot any problems for now.
// As long as you remember to call mergeShortCode() first, you would never
// see "Not set" returned anyway. But this would help you spot that mistake.
public string FinalResult { get; set; } = "Not set";
public void mergeShortCode(String longFormCodec)
{
if (longFormCodec.Equals("Cisco SX80"))
{
String sendShortForm = "SX80";
FinalResult = "V-T" + sendShortForm;
}
else if (longFormCodec.Equals("Cisco Webex Codec Plus"))
{
String sendShortForm = "SRK";
FinalResult = "V-T" + sendShortForm;
}
else if (longFormCodec.Equals("Cisco Webex Codec Pro"))
{
String sendShortForm = "SRK";
FinalResult = "V-T" + sendShortForm;
} else
{
// If longFormCodec is not matched, set the result to ".."
FinalResult = "..";
}
}
By setting the final result to ".." in the else block of the mergeShortCode() method and setting a default value for the FinalResult property (even if it is an empty string I.e. ""). You are preventing FinalResult ever being null and providing all possible outcomes from the one function. This means you can simplify the shortFormCodec_SelectionChanged() method to the following and easily reuse the mergeShortCode() method elsewhere:
public void shortFormCodec_SelectionChanged(object sender, RoutedEventArgs e)
{
//UPDATE THE SHORTCODE TEXTBLOCK
updateVTCShortCode display = new updateVTCShortCode();
display.mergeShortCode(longFormCodec.SelectedItem.ToString());
shortFormCodec.Text = display.FinalResult;
}
}
I would like to iterate through a list in a C# forms application where on the form the user will see the list and can click on a button or a link that is associated with each list item. For example: I know using ruby I could achieve this with the following code:
<% #pets.each do |pet| %>
<%= link_to 'Edit', edit_pet_path(pet) %>
<% end %>
Instead of opening a link I want to open a second pop up with the information of the individual item clicked.
Right now I currently loop through the list to display the items and also have a petsList_Click method that opens a second pop up when clicking on the list. The problem is it clicks on the list a whole rather then individual items in the list. If the user clicks on the first item of the list I want only that info getting passed to the pop up.
Here is my main form with my list and my click method:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Tabby newTabby1 = new Tabby("sunshine", 22222222222222222, new DateTime(2016, 2, 24), false);
Chiwawa newChi1 = new Chiwawa("tony", 33333333333333333, new DateTime(2016, 2, 24), false);
Siamese newsia1 = new Siamese("felix", 44444444444444444, new DateTime(2016, 3, 11), false);
Husky newHusk1 = new Husky("fluffs", 55555555555555555, new DateTime(2016, 2, 24), false);
List<Pet> list = new List<Pet>();
list.Add(newTabby1);
list.Add(newChi1);
list.Add(newsia1);
StringBuilder builder = new StringBuilder();
foreach (var item in list)
{
Console.WriteLine("list item " + item.Chip );
builder.Append(item.name + " " + item.Chip + " " + item.arrivalDate + " status" + item.adoptedStatus).Append("\n");
}
string result = builder.ToString(); // Get string from StringBuilder
petList.Text = result;
petCount.Text = "Pets Available : " + Pet.petCount;
}
private Image ImageUrl(object p)
{
throw new NotImplementedException();
}
private void petList_Click(object sender, EventArgs e)
{
PetInfoForm aPetInfoForm = new PetInfoForm();
aPetInfoForm.Closed += (s, args) => this.Close();
aPetInfoForm.Show();
}
}
Here is my PetInfoForm
public partial class PetInfoForm : Form
{
public PetInfoForm()
{
InitializeComponent();
}
private void PetInfoForm_Load(object sender, EventArgs e)
{
}
}
And just in case it is needed. Here is my pet class which is what the breed classes inherit from:
public abstract class Pet
{
#region Fields
protected long chip;
protected DateTime ArrivalDate;
public string name;
protected bool AdoptedStatus;
public static int petCount = 0;
#endregion End of Fields
#region Constructors
public Pet()
{
chip = 0;
AdoptedStatus = false;
petCount++;
}
public Pet(string name, long chip, DateTime arrivalDate, Boolean adoptedStatus)
{
this.chip = chip;
ArrivalDate = arrivalDate;
AdoptedStatus = adoptedStatus;
this.name = name;
petCount++;
}
#endregion End of Constructors
#region Properties
public int PetCount
{
get
{
return petCount;
}
}
public long Chip
{
get
{
return chip;
}
set
{
if (value > 0)
chip = value;
else
chip = 0;
}
}
public DateTime arrivalDate { get; set; }
public Boolean adoptedStatus { get; set; }
#endregion End Properties
#region Methods
public bool UpdateStatus() => adoptedStatus = true;
public int UpdateInventory() => petCount = petCount - 1;
public abstract void Noise();
#endregion End of Methods
}
I have a class called EventDB.cs that grabs data from a text file. I have every 5 lines input as a object list called events but am having trouble trying to populate the list into my combobox on my main form. Could someone point out what I'm doing wrong? Here is my code for my Event.cs, EventDB.cs and main ticketinfo.cs. Thanks in advance!
Event.cs
namespace TicketInformation
{
public class Event
{
public Event()
{
}
public Event(int day, string time, double price, string strEvent, string description)
{
this.Day = day;
this.Time = time;
this.Price = price;
this.StrEvent = strEvent;
this.Description = description;
}
public int Day { get; set; }
public string Time { get; set; }
public double Price { get; set; }
public string StrEvent { get; set; }
public string Description { get; set; }
public string GetDisplayText()
{
return StrEvent;
}
}
}
EventDB.cs
namespace TicketInformation
{
public static class EventDB
{
private static string dir = Directory.GetCurrentDirectory();
private static string path = dir + "\\calendar.txt";
public static List<Event> ExtractData() //(DateTime dtmDay)
{
//int intChosenDay = dtmDay.Day;
// create object for input stream for text file
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
//create the list
List<Event> events = new List<Event>();
string[] lines = File.ReadAllLines(path);
for (int index = 4; index < lines.Length; index += 5)
{
Event special = new Event();
special.Day = Convert.ToInt32(lines[index - 4]);
special.Time = (lines[index - 3]);
special.Price = Convert.ToDouble(lines[index - 2]);
special.StrEvent = lines[index - 1];
special.Description = lines[index];
events.Add(special);
}
//close stream for the text file
textIn.Close();
return events;
}
}
}
Ticketinfo.cs
static void Main()
{
Application.Run(new FrmEvents());
}
private List<Event> events = null;
private void FrmEvents_Load(
object sender, System.EventArgs e)
{
CreateEventList();
}
private void CreateEventList()
{
EventDB.ExtractData(events); //(mvwDate.SelectionStart);
cboEvent.Items.Clear();
foreach (Event e in events)
{
cboEvent.Items.Add(e.GetDisplayText());
}
} //end method CreateEventList
I think you make mistake in CreateEventList() method in Ticketinfo.cs. You dont update events in Ticketinfo.cs at all, you will have always empty list. You should change line
EventDB.ExtractData(events);
to
events = EventDB.ExtractData();
and then should work fine. now you method will return events from file.
You new methos should looks like :
private void CreateEventList()
{
events = EventDB.ExtractData(); //(mvwDate.SelectionStart);
cboEvent.Items.Clear();
foreach (Event e in events)
{
cboEvent.Items.Add(e.GetDisplayText());
}
} //end method CreateEventList
If you want to display events that are on or later than the selected date, there's a couple of things you can do:
First, you need an actual date in your Events class - you have a day and a time, but I don't see a date. So let's just use the Day property for purposes of this example.
You can bind your events list to your combo box based on the selected Day with LINQ very easily:
private void CreateEventList()
{
events = EventDB.ExtractData(); //(mvwDate.SelectionStart);
var e = (from ev in events
where ev.Day >= mvwDate.SelectionStart // mvwDate.SelectionStart needs to be an int
select ev).ToList();
cboEvent.Items.Clear();
cboEvent.DisplayMember = "StrDesc";
// You could also assign a ValueMember like this:
//cboEvent.ValueMember = "Day";
cboEvent.DataSource = e;
} //end method CreateEventList
There's a few assumptions in my example (mainly what mvwDate.SelectionStart is), and I haven't tested the code but this should give you another approach.
I'm creating a form to hold information from "meetings". The user will fill out info regarding title, location, startTime, endTime, notes, and a date. What I am currently working on is the "save changes" button which will:
clear all the TextBoxes.
store the input in an array.
display only the title in the ListBox.
when the title is clicked on in the ListBox, the info stored in that array element re-populates in the appropriate TextBoxes should the user wish to make changes.
I have completed #1, #2 and #3 I would appreciate any help for #4. I've pasted the coding below for your viewing.
public partial class CalendarForm : Form
{
int currentIndex;
int arraySize = 0;
Meeting[] meetingArray = new Meeting[100];
public CalendarForm()
{
InitializeComponent();
}
private void saveChangesButton_Click(object sender, EventArgs e)
{
meetingArray[arraySize] = new Meeting();
meetingArray[arraySize].title = textBoxTitle.Text;
meetingArray[arraySize].location = textBoxLocation.Text;
meetingArray[arraySize].startTime = textBoxStartTime.Text;
meetingArray[arraySize].endTime = textBoxEndTime.Text;
meetingArray[arraySize].notes = notesTextBox.Text;
currentIndex = arraySize;
arraySize++;
meetingListBox.Enabled = true;
textBoxTitle.Text = "";
textBoxLocation.Text = "";
textBoxStartTime.Text = "";
textBoxEndTime.Text = "";
notesTextBox.Text = "";
*edit* added these two lines which now add the title to the listBox
meetingListBox.Items.Add(meetingArray[currentIndex].title);
Controls.Add(meetingListBox);
}
}
public class Meeting
{
public string title;
public string location;
public string startTime;
public string endTime;
public string notes;
};
This is how I would refactor the class:
public partial class CalendarForm : Form
{
private List<Meeting> Meetings { get; set; }
public CalendarForm()
{
InitializeComponent();
Meetings = new List<Meeting>();
}
private void saveChangesButton_Click(object sender, EventArgs e)
{
try
{
Meeting meeting = CreateMeeting();
Meetings.Add(meeting);
meetingListBox.Add(meeting);
}
catch
{
//Add proper error handling here
}
}
private Meeting CreateMeeting()
{
return new Meeting()
{
Title = textBoxTitle.Text,
Location = textBoxLocation.Text
StartTime = DateTime.Parse(textBoxStartTime.Text),
EndTime = DateTime.Parse(textBoxEndTime.Text),
Notes = notesTextBox.Text,
};
}
}
//As Matt Burland answered already:
private void meetingListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Meeting meeting = meetingListBox.SelectedItem as Meeting;
if (meeting != null)
{
textBoxTitle.Text = meeting.Title;
//...etc for all your other text boxes.
}
}
public class Meeting
{
public string Title { get; set; }
public string Location { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Notes { get; set; }
public override string ToString()
{
return Title;
}
}
I've made a number of changes, more notably the switch from an Array to a List<>. Lists are more flexible and provide better functionality. Unless you really really need to use arrays, I would stay away from them just to better safeguard against logic errors index out of bounds type issues.
Also, I personally believe that dates should be stored in the DateTime struct format, but that is again a matter of preference. Note that it would be prudent to sanitize/validate the inputs (especially the dates) before assigning it into the Meeting object.
The Meeting object now has properties instead of public fields. Properties are preferred in case you ever want to change how something is Get/Set.
Hope this helps.
I really recommend you look up data binding and learn how to do this properly, but if you want a quick and dirty solution (although, in the end, you'll find it's a lot more work), I would do something like this:
private void saveChangesButton_Click(object sender, EventArgs e)
{
Meeting m = new Meeting();
m.title = textBoxTitle.Text;
m.location = textBoxLocation.Text;
m.startTime = textBoxStartTime.Text;
m.endTime = textBoxEndTime.Text;
m.notes = notesTextBox.Text;
meetingArray[arraySize] = m;
currentIndex = arraySize;
arraySize++;
meetingListBox.Enabled = true;
textBoxTitle.Text = "";
textBoxLocation.Text = "";
textBoxStartTime.Text = "";
textBoxEndTime.Text = "";
notesTextBox.Text = "";
meetingListBox.Items.Add(m);
//Controls.Add(meetingListBox); // You don't need to keep adding the control every time!
}
Now in your Meeting class, I'd override ToString() to just return the title. The ListBox will just use the ToString() method of whatever you add to it by default.
To help with #4, you want to bind the SelectedIndexChanged event and then use the SelectedItem property, cast it back to a Meeting object (because it'll return an Object) and then use it to repopulate your various text boxes.
Something like:
private void meetingListBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
Meeting m = meetingListBox.SelectedItem as Meeting;
if (m != null)
{
textBoxTitle.Text = m.title;
//...etc for all your other text boxes.
}
}