12 Replies - 2992 Views - Last Post: 07 January 2011 - 08:08 PM Rate Topic: -----

#1 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Associate button and text box with panel

Posted 02 January 2011 - 09:34 PM

My application creates panels and controls, ie. labels, textboxes, etc., based on user input. The controls on the panel are located in the same place each time.

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.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Associate button and text box with panel

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Associate button and text box with panel

Posted 03 January 2011 - 05:57 AM

Unless there is some reason you are neglecting to tell us, the panel doesn't matter. What you need to do is wire up the Click event for the dynamically generated buttons.

Button btnPlayerName = new Button();
btnPlayerName.Location = new Point(12, 25);
btnPlayerName.Size = new Size(146, 57);
btnPlayerName.Name = PlayerName[i].ToString();
btnPlayerName.Text = String.Format("Player {0} \n {1}", (i + 1), PlayerName[i]);
btnPlayerName.BackColor = Color.Azure;
btnPlayerName.TabStop = false;
btnPlayerName.Enabled = true;

btnPlayerName.Click += (s, es) =>
{
     // your code for this button
     // when the button is clicked, this code will be executed.
};

numButton.Add(btnPlayerName);


Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Associate button and text box with panel

Posted 03 January 2011 - 07:20 AM

Quote

My application creates panels and controls, ie. labels, textboxes, etc., based on user input. The controls on the panel are located in the same place each time.

Rather than the program trying to track 20 instances of a lot of look-a-like panels you should be making one component then make however many instances of that component.

If your custom component includes 1 button, 1 label, a Combobox and so on then your code can also know that for whatever instance of that component it can be reached by

myComponent5.FirstNameTextBox.Text = "Fred";


and so on.

This tutorial does a lot with inheritance.

Bulding an application - Part 1

Quote

I need your help to figure out how in the program to know which textbox named "txtHandi" goes with what panel.
Just check the control's .Parent property.
Was This Post Helpful? 1
  • +
  • -

#4 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Associate button and text box with panel

Posted 06 January 2011 - 03:53 PM

View PosttlhIn, on 03 January 2011 - 06:20 AM, said:

Quote

My application creates panels and controls, ie. labels, textboxes, etc., based on user input. The controls on the panel are located in the same place each time.

Rather than the program trying to track 20 instances of a lot of look-a-like panels you should be making one component then make however many instances of that component.

If your custom component includes 1 button, 1 label, a Combobox and so on then your code can also know that for whatever instance of that component it can be reached by

myComponent5.FirstNameTextBox.Text = "Fred";


and so on.

This tutorial does a lot with inheritance.

Bulding an application - Part 1

Quote

I need your help to figure out how in the program to know which textbox named "txtHandi" goes with what panel.
Just check the control's .Parent property.

This post has been edited by timcamper: 06 January 2011 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Associate button and text box with panel

Posted 07 January 2011 - 12:47 PM

Quote

What you need to do is wire up the Click event for the dynamically generated buttons.


I am wiring up the Click event for the dynamically generated button "btnPlayerName".
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
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 01:03 PM

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?


Re-read post number 3 where it was suggested you make a control for this, then have 10 instances of hte control.

As you are seeing it is a nightmare to do it the way you are trying with 10 panels, and track 10 textboxes individually at program.cs level and so on.

This is the very heart of Object Oriented Programming. You make a single object that is your player panel and all of its sub controls like textboxes. You should only have to track 10 PlayerPanels. Each PlayerPanel has its btnPlayerName wired to its TextBoxHindi. You don't track any of that.

If you aren't event going to try the OOP advice that is given to you then stop asking for the advice.
Was This Post Helpful? 0
  • +
  • -

#7 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Associate button and text box with panel

Posted 07 January 2011 - 02:51 PM

Yes, I agree with you on the OOP suggestion. That is the whole goal for me. When I tried the tutorial "Building an application - POS/Cash Register - Part one", I could not create the Windows Control Library as suggested. I believe because I am using Visual C# 2010 Express. Is it possible to just create a class as a work around? Or should the Windows Control Library be available and I am not creating it correctly?

Your suggestion is exactly what I would like to do. Thanks for being on top of post.
Tim
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 03:13 PM

View Posttimcamper, on 07 January 2011 - 02:51 PM, said:

When I tried the tutorial "Building an application - POS/Cash Register - Part one", I could not create the Windows Control Library as suggested. I believe because I am using Visual C# 2010 Express....


That project template is available in all versions of VS. You're sure you don't have a template like this:

Posted Image

Make sure you have clicked "Windows" on the left.

This post has been edited by insertAlias: 07 January 2011 - 03:13 PM

Was This Post Helpful? 1
  • +
  • -

#9 timcamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 22-July 09

Re: Associate button and text box with panel

Posted 07 January 2011 - 06:30 PM

No I don't have that template. I am not using VS. I am using Visual C# 2010 Express. I can't find the link right now but at one of Microsoft's tutorials about this same topic, they were quick to point out that Window Form Control Library is not supported on the Express Editions. My templates only go as far as Empty Project. I will install VS and go from there.
Thanks - Tim

Side note question: What are you doing to get the nice image of the project template. I tried print screen, then into word...
need I say more. I am dating myself. So I could post what I see.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 06:46 PM

Alt+PrintScreen copies the active window into the clipboard, and then I paste it into paint.net, but you can paste it into any image editor. Then I save it and upload it to imgur.com.
Was This Post Helpful? 1
  • +
  • -

#11 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 07:14 PM

Screenshots: SnagIt. WONDERFUL screen capture utility. www.techsmith.com
Does video capture, screen capture by drag or click on an individual object, menu capture.
With a built in editor for adding captions, arrows, icons... It's not photoshop but it is perfectly tailored to I.T. needs.

Since Visual Studio 2010 Express is free I suggest you get it so you can do this and future projects the right way. Didn't we already cover this in PM conversation? Or was that someone else? I get lost.
Was This Post Helpful? 1
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 08:00 PM

Visual C# 2010 is Visual Studio Express. They provide them language by language instead of packaged together. And apparently, the Express editions do not have the template for Control Libraries. That sucks.

Really, though, you don't need to define your controls in a control library. Just define them in your Forms project.
Was This Post Helpful? 1
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Associate button and text box with panel

Posted 07 January 2011 - 08:08 PM

insertAlias is right that you don't have to do it in a DLL.
Can you put more than one project in a solution?
If you can I'd make one Project that is all your custom controls so you can add that project to any other future solution. This will help you approximate a DLL because you can reuse the same code and controls in several solutions.

Plan B:

Attached Image
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1