| |

VerySource

 Forgot password?
 Register
Search
View: 717|Reply: 2

Public methods are not available in tabPage1

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-29 08:20:01
| Show all posts |Read mode
I defined a method to automatically add controls
public void AddMyControls (int pos_width, int pos_height, string text, string name)
        {
            PictureBox UserControl = new PictureBox ();
            UserControl.Name = "PictureBox" + name;
            UserControl.Location = new Point (40 + pos_width, 30 + pos_height);
            UserControl.Size = new Size (124, 50);
            this.Controls.Add (UserControl);
            UserControl.Click + = new System.EventHandler (this.pictureBox_Click);
            //MessageBox.Show (text.ToString ());
        }

But the scope of this method is only for this.AddMyControls

this.tabPage1.AddMyControls is invalid
Is there any way to solve it
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-5 12:00:02
| Show all posts
AddMyControls is a method of your form class, not a method of TabPage class
this.tabPage1.AddMyControls of course cannot be compiled unless you overload the TabPage class

The simpler solution is to add the Control parameter and pass in the parent control.
public void AddMyControls (Control ctrl, int pos_width, int pos_height, string text, string name)
{
  ....
  ctrl.Add (UserControl);
  ....
}
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-8 21:15:01
| Show all posts
Speaking upstairs is good.
The method written in this way is more versatile.
If you really want to achieve your needs, you can inherit TabPage such as MyTabPage, add a protected method called AddMyControls (), and then add an instance of MyTabPage to your TabControl.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list