Display Position of an Item in a List - c#

I am working with a link list. The list is brought up on the form load (size, height, stock, price) displayed in a multiline textbox called textBoxResults. I have three button called First, Next and Last. Naming convention is straightforward, button First click displays first tree, Next, displays each item after first through each button click and Last button shows the last item on list. First works fine but Second only shows the following item after first and gives up, Last displays weird result WindowsFormsApplication1.Form1+Fruit Trees. How can I display the proper tree name according to its position through the button clicks?
Code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class ListOfTrees
{
private int size;
public ListOfTrees()
{
size = 0;
}
public int Count
{
get { return size; }
}
public FruitTrees First;
public FruitTrees Last;
}
public void ShowTrees()
{
}
public void Current_Tree()
{
labelSpecificTree.Text = Trees.First.Type.ToString();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void buttonFirst_Click(object sender, EventArgs e)
{
Current_Tree();
}
private void buttonNext_Click(object sender, EventArgs e)
{
Current = Trees.First;
labelSpecificTree.Text = Current.Next.Type.ToString();
}
private void buttonLast_Click(object sender, EventArgs e)
{
Current = Trees.Last;
labelSpecificTree.Text = Trees.Last.ToString();
}
}
}

Several problems in your code (see the comments for corrections):
public int Add(FruitTrees NewItem)
{
FruitTrees Sample = new FruitTrees();
Sample = NewItem;
Sample.Next = First;
First = Sample;
//Last = First.Next;
// Since Add is an operation that prepends to the list - only update
// Last for the first time:
if (Last == null){
Last = First;
}
return size++;
}
Secondly in your Next method:
private void buttonNext_Click(object sender, EventArgs e)
{
// In order to advance you need to take into account the current position
// and not set it to first...
//Current = Trees.First;
Current = Current.Next != null ? Current.Next : Current;
labelSpecificTree.Text = Current.Type.ToString();
}
And in the "last" method:
private void buttonLast_Click(object sender, EventArgs e)
{
Current = Trees.Last;
// show the data, not the name of the Type
labelSpecificTree.Text = Current.Type.ToString();
}
And of course the current method is broken, also...
public void Current_Tree()
{
Current = Trees.First;
labelSpecificTree.Text = Current.Type.ToString();
}

Related

Manipulating similar objects in a form using another form

I am new to C# and I want to utilize the forms with one another.
I have 2 forms. (1)MMCMLibrary_home and (2)MMCMLibrary_reserve.
In this project, I'm in the stage of changing the label background colors in Form 1 but can't seem to utilize Form 2 to process it.
These are my necessary codes so far:
FORM 1
namespace MMCM_Library
{
public partial class MMCMLibrary_home : Form
{
public static MMCMLibrary_home instance;
//DCR1 Labels
public Label lbl1_1;
public Label lbl1_2;
public Label lbl1_3;
public Label lbl1_4;
public MMCMLibrary_home()
{
InitializeComponent();
instance = this;
lbl1_1 = lblDCR1_9;
lbl1_2 = lblDCR2_11;
lbl1_3 = lblDCR1_1;
lbl1_4 = lblDCR1_3;
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve();
reserveDCR1.Show();
}
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve();
reserveDCR2.Show();
}
public void btnDCR3_Click(object sender, EventArgs e)
{
var reserveDCR3 = new MMCMLibrary_reserve();
reserveDCR3.Show();
}
public void btnDCR4_Click(object sender, EventArgs e)
{
var reserveDCR4 = new MMCMLibrary_reserve();
reserveDCR4.Show();
}
}
}
FORM 2
when I click any reserve now button in form 1 it will open form 2. However, if I pick a radio button, the background change will always be applied to Discussion Room 1 even I reserved for discussion room 2
namespace MMCM_Library
{
public partial class MMCMLibrary_reserve : Form
{
public static MMCMLibrary_reserve instance;
public MMCMLibrary_reserve()
{
InitializeComponent();
instance = this;
}
private void MMCMLibrary_reserve_Load(object sender, EventArgs e)
{
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
}
private void btnDCR1_Click(object sender, EventArgs e)
{
}
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
}
}
}
Can you help me to device an efficient way to solve this. Can you also suggest a database method suitable for my beginner project.
You've said that:
the background change will always be applied to Discussion Room 1
Well, yes. It seems you don't pass specific object into Form2. There's strightforward:
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
So you'll be always changing backcolor of lbl1_1 of your Form1. What needs to be done is to indicate which room has been selected. You can assign room after you click the button by passing the int parameter:
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve(1);
reserveDCR1.Show();
}
or:
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve(2);
reserveDCR2.Show();
}
Then, in Form2, at the very top, add something like:
int room; to be able to assign the room number in Form2:
public MMCMLibrary_reserve(int roomNumber)
{
InitializeComponent();
room = roomNumber;
instance = this;
}
And then you could just select room you clicked, by:
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
if(room == 1)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
else if(room == 2)
{
MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
}
else if(room == 3)
{
MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
}
else if(room == 4)
{
MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
}
}
else if(radioButton2.Checked)
{
//etc.
}
else if(radioButton3.Checked)
{
//etc.
}
else if(radioButton4.Checked)
{
//etc.
}
}
I think that was the problem. Try it and let us know.

Accessing a list from a second form and adding user input to the list

I have a list called "studentInfo" and I am trying to allow the user to go to a form I created called "addRecord" and type in a student code and a student mark and once they hit submit the data is then added to the list in form1 and then also the data gets added to the listbox in form1.
I don't know how to go about this since I am new to C# so any guidance will be appreciated.
My list is created like this
public static List<string> studentInfo = new List<string>();
Then once my form1 loads I read in from a CSV file to populate the listbox
private void Form1_Load(object sender, EventArgs e)
{
string lines, studentNumber, studentMark;
StreamReader inputFile;
inputFile = File.OpenText("COM122.csv");
while (!inputFile.EndOfStream)
{
lines = inputFile.ReadLine();
studentInfo = lines.Split(',').ToList();
studentNumber = studentInfo[0];
studentMark = studentInfo[1];
lstMarks.Items.Add(studentNumber + " : " + studentMark);
}
inputFile.Close();
}
I want the user to go to the addRecord form, enter in a student code and student mark, then hit submit and then the program asks the user to confirm that the data they have entered is correct and then the user is directed back to form1 and the data will be in the listbox.
Alrighty, in Form addRecord, we create two properties to hold the data to be retrieved from the main Form. This should only be done if addRecord returns DialogResult.OK. I've added in two buttons, one for cancel and one for OK. These simply set DialogResult to their respective results. The property values for the two values to be returned are also set in the OK button handler:
public partial class addRecord : Form
{
public addRecord()
{
InitializeComponent();
}
private string _Student;
public string Student
{
get
{
return this._Student;
}
}
private string _Score;
public string Score
{
get
{
return this._Score;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
this._Student = this.tbStudent.Text;
this._Score = this.tbScore.Text;
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
Now, back in Form1, here is an example of showing addRecord with ShowDialog(), then retrieving the stored values when OK is returned:
public partial class Form1 : Form
{
public static List<string> studentInfo = new List<string>();
// ... other code ...
private void btnAddRecord_Click(object sender, EventArgs e)
{
addRecord frmAddRecord = new addRecord();
if (frmAddRecord.ShowDialog() == DialogResult.OK)
{
studentInfo.Add(frmAddRecord.Student);
studentInfo.Add(frmAddRecord.Score);
lstMarks.Items.Add(frmAddRecord.Student + " : " + frmAddRecord.Score);
}
}
}
--- EDIT ---
Here's a different version of addRecord that doesn't allow you to hit OK until a valid name and a valid score have been entered. Note that the property has been changed to int, and the TextChanged() events have been wired up to ValidateFields(). The OK button is also initially disabled:
public partial class addRecord : Form
{
public addRecord()
{
InitializeComponent();
this.tbStudent.TextChanged += ValidateFields;
this.tbScore.TextChanged += ValidateFields;
btnOK.Enabled = false;
}
private string _Student;
public string Student
{
get
{
return this._Student;
}
}
private int _Score;
public int Score
{
get
{
return this._Score;
}
}
private void ValidateFields(object sender, EventArgs e)
{
bool valid = false; // assume invalid until proven otherwise
// Make sure we have a non-blank name, and a valid mark between 0 and 100:
if (this.tbStudent.Text.Trim().Length > 0)
{
int mark;
if (int.TryParse(this.tbScore.Text, out mark))
{
if (mark >= 0 && mark <= 100)
{
this._Student = this.tbStudent.Text.Trim();
this._Score = mark;
valid = true; // it's all good!
}
}
}
this.btnOK.Enabled = valid;
}
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}

Selecting multiple items from listbox to add to 2D array and pass to another page UWP C#

I am trying to select multiple (2) items from a listbox and add these values to a 2D array, this array gets passed to the next page to output order info from a menu. When I go to the second page the array is passed but only displays the first selected item from the listbox on the first page? Is it because I am not loading the array correctly in my code?
First page code:
class order
{
public string[,] orderItems = new string[3,3];
}
public sealed partial class foodMenu : Page
{
int x = 0;
order newOrder = new order();
public foodMenu()
{
this.InitializeComponent();
}
private void ReturnHomeButton_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(MainPage));
}
private void breakfastButton_Click(object sender, RoutedEventArgs e)
{
menuList.Items.Clear();
menuList.Items.Add("Eggs");
menuList.Items.Add("Bacon");
menuList.Items.Add("Cereal");
menuList.Items.Add("Orange Juice");
menuList.Items.Add("Coffee");
}
private void lunchButton_Click(object sender, RoutedEventArgs e)
{
menuList.Items.Clear();
menuList.Items.Add("Ham Sandwich");
menuList.Items.Add("Turkey Sandwich");
menuList.Items.Add("Salad");
menuList.Items.Add("Soup");
menuList.Items.Add("Soda");
menuList.Items.Add("Juice");
}
private void dinnerButton_Click(object sender, RoutedEventArgs e)
{
menuList.Items.Clear();
menuList.Items.Add("Pasta");
menuList.Items.Add("Lobster");
menuList.Items.Add("Ham");
menuList.Items.Add("Pot Roast");
menuList.Items.Add("Wine");
menuList.Items.Add("Juice");
}
private void addOrderButton_Click(object sender, RoutedEventArgs e)
{
newOrder.orderItems[x,x] = Convert.ToString(menuList.SelectedItem);
x++;
}
private void orderButton_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(orderConfirmation), newOrder);
}
}
}
Second page code:
public sealed partial class orderConfirmation : Page
{
public orderConfirmation()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
order orderOutputInfo = (order)e.Parameter;
string[,] orderItems = new string[3, 3];
orderItems[0,0] = orderOutputInfo.orderItems[0,0];
orderOutput.Items.Add(orderItems[0,0]);
orderItems[1, 1] = orderOutputInfo.orderItems[1, 1];
orderOutput.Items.Add(orderItems[1, 1]);
orderItems[2, 2] = orderOutputInfo.orderItems[2, 2];
orderOutput.Items.Add(orderItems[2, 2]);
}
}
}
Assuming that your "menuList" is the ListBox.
menuList.SelectedItem
Return is singular.
menuList.SelectedItems
Return is multiple which I think might be what you're looking for.

How to send int value from form to usercontrol

I'm Working on a game that uses multiple layouts. To make those layouts, I use user controls by bringing them forward and sending them behind. Now I'm in a phase where I have to send all gathered data (hardness, what type etc) to User control where all rest of the work is done. I'm stuck with sending int values to my user control that does all the work. I thought about sending them with eventhandler, but EventHandler didn't seem to recognize my int values either
In short, I want to pass Int values that I have in a form, to User control
public void liitmine1(object sender, EventArgs e)
{
/*event*/
uCraskus1.BringToFront();
int What = 1;
}
public void kumme(object sender, EventArgs e)
{
uCmehanism1.BringToFront();
int Between = 1;
}
public event EventHandler KummeClick; /*Sends info to mechanism*/
private void KummeNupp(object sender, EventArgs e)
{
if(What == 1) /*The name'what' does not exist in current context error*/
if (this.KummeClick != null)
this.KummeClick(this, e);
}
if (this.KummeClick != null) is just to test, don't mind if anything is wrong with that
You can make your own EventArgs type, like this:
public class KummeClickEventArgs : EventArgs
{
public int MyProperty { get; set; }
}
And then use it (assumption that kumme method is one of the subscribers):
public void kumme(object sender, KummeClickEventArgs e)
{
//here is your logic like:
//int test = e.MyProperty;
uCmehanism1.BringToFront();
int Between = 1;
}
public event EventHandler<KummeClickEventArgs> KummeClick; /*Sends info to mechanism*/
private void KummeNupp(object sender, EventArgs e)
{
if(What == 1) /*The name'what' does not exist in current context error*/
if (this.KummeClick != null)
{
var eventArgs = new KummeClickEventArgs
{
MyProperty = 3
};
this.KummeClick(this, eventArgs);
}
}

how to pass a variable from one form to a textBoxChanged event handler on another form?

On Form one i need to send the listbox.SelectedIndex to second Form:
private void btnEditWord_Click(object sender, EventArgs e)
{
Form editWord = new editWord(listBox.SelectedIndex);
editWord.ShowDialog();
}
Second Form: the selected index variable does not exist in current context.
public editWord(int value)
{
InitializeComponent();
int selectedIndex = value;
}
private void wordTextBox_TextChanged(object sender, EventArgs e)
{
string word = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Word"].ToString());
wordTextBox.Text = word;
}
I have commented your question,but just to make it clear, your second form code should look like this:
int selectedIndex=-1;
public editWord(int value)
{
InitializeComponent();
selectedIndex = value;
}
private void wordTextBox_TextChanged(object sender, EventArgs e)
{
string word = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Word"].ToString());
wordTextBox.Text = word;
}
The problem with your provided code is that the scope of selectedIndex is only the constructor.
Just move the int selectedIndex; outside of the constructor, to make it global to the second form and then in the constructor selectedIndex = value;

Categories