I need your help to figure out how in the program to know which textbox named "txtHandi" goes with what panel.
My code falls apart here: void btnPlayerName_Click(object sender, EventArgs e)
It does not recognize the btnPlayerName I click on or the txtHandi. I am sure it is something I am overlooking!
1. Please help me understand how I can associate each of the "btnPlayerName" to the panel it shows up on.
2. and how I can associate each of the txtHandi on that same panel so the line of code "int playerHandicap = Convert.ToInt32(txtHandi.Text);"
in the void btnPlayerName_Click(object sender, EventArgs e) will work.
I suspect I will need help with:
int playerGross = Convert.ToInt32(myGross[i].Text);
int holeHandicap = (handicapofhole[i]);
in "void btnPlayerName_Click(object sender, EventArgs e)" also.
If you can jump start me with a few of these items I most likely will be able to connect the dots.
I am providing portions of code for your review.
Thanks in advance for your help.
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kevin AungPosted Jan 9, 2011, 1:20 PM
Since the player's objects are all contained in the panel, you could search that specific panel to get the txtHandi value;
void btnPlayerName_Click(object sender, EventArgs e)
{
Panel curPanel;
TextBox curHandi;
Button curPlayerBtn;
string HandiVal = "";
curPlayerBtn = (Button)sender;
curPanel = (Panel)curPlayerBtn.Parent;
foreach (Control c in curPanel.Controls)
{
if (c is TextBox)
{
curHandi = (TextBox)c;
HandiVal = curHandi.Text;
}
}
}
Secondly, you should make a user control for these Player panels so that you won't have to use too much logic in the main game.
Tim CamperPosted Jan 7, 2011, 3:35 PM
So do I just copy / paste from notepad right into the post? This is kind of embarrasing.
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Player myPlayer;
TextBox[] myGross = new TextBox[18];
Label[] myNet = new Label[18];
int[] handicapofhole = new int[] { 10, 18, 8, 4, 12, 14, 16, 2, 6, 15, 17, 1, 9, 13, 11, 7, 3, 5 };
Label lblHandicap = new Label();
TextBox txtHandi = new TextBox();
Label lblGrossScores = new Label();
Label lblNet = new Label();
public List
public List
public List
Tim CamperPosted Jan 7, 2011, 3:29 PM
Kevin ask: So when do the textbox and panel get created dynamically?
Answer: When the user clicks on "btnConfirmPlayers". The quotes here indicate the name of the button. "btnConfirmPlayers" is already on the form and does not get created at runtime.
Go to the block of code where TextBox txtHandi = new TextBox(); is dynamcially generated.
This is where I get stuck. What programming techniques do I use, so when I click on the button
"btnPlayerName", the code knows what "txtHandi" textbox is associated with that run of the for loop?
The same applies to myGross[j] and so on.
Just imagine a form with a panel of controls. The number of panels is dependent on the number of players.
So that varies based on user input. The key here is you may have 10 panels and controls that look exactly alike with the exception of the btnPlayerName which knows who the player is.
So when I click on btnPlayerName (for a specific player) I want to enter an integer number that is the player handicap. Following that I want to enter the players gross scores in their respective textboxes.
Then send to method in the Player class to figure the net scores and return in the respective net score textboxt.
Another post suggest I should be making one component then make however many instances of that component.
Again, thanks in advance for your input!
Tim
Kevin AungPosted Jan 5, 2011, 5:45 PM
What I would do is create the objects and give them some sort of an index, either in their Name or Tag.
Create objects:
private void CreateObjects()
{
int index;
TextBox curTextBox = new TextBox();
curTextBox.Name = "txtSomeTextBox" + index.ToString();
// or
// curTextBox.Tag = index.ToString();
Panel curPanel= new Panel();
curPanel.Name = "panelSomePanel" + index.ToString();
// or
// curPanel.Tag = index.ToString();
curPanel.Controls.Add(curTextBox);
this.Controls.Add(curPanel);
}
Same for buttons and what not. This way you have that index to check to see how the objects need to group or where it belongs to. Keep in mind if you create a button dynamically and point to the same event handler (ex: btnPlayerName_Click) they're all gonna do the same thing unless you determine in your code where the button came from and what it needs to do.
Gomathi PalaniswamyPosted Jan 3, 2011, 12:22 AM
get like
for (i = 0; i < sourcefrm.Controls.Count; i++)
{
if (sourcefrm.Controls[i] is Panel)
{
Panel rg = (Panel)sourcefrm.Controls[i];
foreach (Control c in rg.Controls)
{
if (c is TextBox)
{
c.Enabled = true;
}
if (c is ComboBox)
{
c.Enabled = true;
}
}
}
}