none
C# Change Page RRS feed

  • Question

  • Hi all 

    I try to use tabControl to create pages by my buttonClick.

    When I Click tab to change page it doesn't work.

    How can I fix it?


    The image is still there.





    • Edited by CY_Chen Tuesday, June 18, 2019 7:04 AM
    Monday, June 17, 2019 3:31 AM

All replies

  • Hi CY_Chen,

    In your code, the background image of the tabPage is always bound to "Properties.Resources._800x480", so "The image is still there".

    Maybe you need to set different images for the pages.

        public static TabControl pageTabControl = new TabControl();
        public static List<TabPage> tabPage = new List<TabPage>();
        List<Image> images = new List<Image>();
        public Form1()
        {
            pageTabControl.Location = new System.Drawing.Point(30, 20);
            pageTabControl.Size = new Size(500,200);
            this.Controls.Add(pageTabControl);
            images.Add(Image.FromFile(@"D:\pic\a.jpg"));
            images.Add(Image.FromFile(@"D:\pic\b.jpg"));
            images.Add(Image.FromFile(@"D:\pic\c.jpg"));
            InitializeComponent();
        }
        string PName = "Page";
        public static int PCounts = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            tabPage.Add(new TabPage());
            tabPage[PCounts].Name = PName + PCounts;
            tabPage[PCounts].Text = PName + PCounts;
            tabPage[PCounts].Size = new Size(820, 500);
            tabPage[PCounts].BackgroundImage = images[PCounts];
            pageTabControl.Controls.Add(tabPage[PCounts]);
            PCounts += 1;
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 5:25 AM
    Moderator
  • Hi CY_Chen,

    Well, the code about "btCreate_Click" just create several new panels. So, where do you store the panels?

    I don't quite understand how there is a relation between "btCreate_Click" and "Button_Click". There is no code for Panel in Button_Click".

    The code "this.Controls.Add(p); " only implements adding a panel on the Form.

    Any further info please?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 5:51 AM
    Moderator
  • Hi Cy_Chen,

    Yes, you are right. Why not combine the two code fragment? Just like,

        tabPage.Add(new TabPage());
    
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "Image File(*.png;)|*.png;*";
        openFileDialog.InitialDirectory = "D:\\pic\\";
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            Img = Image.FromFile(openFileDialog.FileName);
        }
        Assembly assembly = Assembly.GetExecutingAssembly();        // namespace.classname                
        object obj = assembly.CreateInstance("Assembly_create_panel_and_image_together.BroculosDrawing");
        Panel p = (Panel)obj;
        p.Name = name + (count++);
        p.BackgroundImage = Img;
        p.Location = new Point(x, y);
        p.Size = new Size(100, 100);
    
        tabPage[PCounts].Name = PName + PCounts;
        tabPage[PCounts].Text = PName + PCounts;
        tabPage[PCounts].Size = new Size(820, 500);
        //tabPage[PCounts].BackgroundImage = images[PCounts];
        tabPage[PCounts].Controls.Add(p);
        pageTabControl.Controls.Add(tabPage[PCounts]);
        PCounts += 1;

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 6:30 AM
    Moderator
  • Hi CY_Chen,

    I see. If your issue is solved, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they face similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 6:54 AM
    Moderator
  • Hi CY_Chen,

    One thread only discusses one issue. If you have another new one, please create a new thread to post it and close this one.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 17, 2019 7:15 AM
    Moderator