Dynamically displaying controls

[b][red]This message was edited by shinsnake at 2006-9-28 5:56:7[/red][/b][hr]
I have 8 panels, pnlBegin1 - pnlBegin8. I have an integer that increments and decrements based on two buttons. It starts at 1 and stops at 8. What I want to do is use the number to change a certain panel to visible. For instance, if the user has clicked the button two times, pnlBegin3 should be visible.

[code]protected void Page_Load(object sender, EventArgs e)
{
btnBeginCounter = 1;

if (Page.IsPostBack)
{
btnBeginCounter = (int)ViewState["btnBeginCounter"];
if (btnBeginCounter == 1)
{
btnBeginPrev.Enabled = false;
}
if (btnBeginCounter == 8)
{
btnBeginNext.Enabled = false;
}
}
}

protected void btnBeginPrev_Click(object sender, EventArgs e)
{
btnBeginCounter--;
/*Insert code here for pnlBegin[btnCounter].Visible = true;*/
}

protected void btnBeginNext_Click(object sender, EventArgs e)
{
btnBeginCounter++;
/*Insert code here for pnlBegin[btnCounter].Visible = true;*/
}[/code]

How would I accomplish that?


Comments

  • U need ViewState to preserve variables across post backs ... here is the typical pattern ...

    [code]
    protected void Page_Load(object sender, EventArgs e)
    {
    [b]
    if(!Page.IsPostBack)
    btnBeginCounter = 1;
    else
    btnBeginCounter = (int)ViewState["BeginCounter"];
    [/b]

    if (Page.IsPostBack)
    {
    btnBeginCounter = (int)ViewState["btnBeginCounter"];
    if (btnBeginCounter == 1)
    {
    btnBeginPrev.Enabled = false;
    }
    if (btnBeginCounter == 8)
    {
    btnBeginNext.Enabled = false;
    }
    }
    }

    protected void btnBeginPrev_Click(object sender, EventArgs e)
    {
    btnBeginCounter--;
    /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    }

    protected void btnBeginNext_Click(object sender, EventArgs e)
    {
    btnBeginCounter++;
    /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    }

    [b]
    protected void Page_PreRender(object sender, EventArgs e)
    {
    ViewState["BeginCounter"] = btnBeginCounter;
    }
    [/code]
    [/b]

    : [b][red]This message was edited by shinsnake at 2006-9-28 5:56:7[/red][/b][hr]
    : I have 8 panels, pnlBegin1 - pnlBegin8. I have an integer that increments and decrements based on two buttons. It starts at 1 and stops at 8. What I want to do is use the number to change a certain panel to visible. For instance, if the user has clicked the button two times, pnlBegin3 should be visible.
    :
    : [code]protected void Page_Load(object sender, EventArgs e)
    : {
    : btnBeginCounter = 1;
    :
    : if (Page.IsPostBack)
    : {
    : btnBeginCounter = (int)ViewState["btnBeginCounter"];
    : if (btnBeginCounter == 1)
    : {
    : btnBeginPrev.Enabled = false;
    : }
    : if (btnBeginCounter == 8)
    : {
    : btnBeginNext.Enabled = false;
    : }
    : }
    : }
    :
    : protected void btnBeginPrev_Click(object sender, EventArgs e)
    : {
    : btnBeginCounter--;
    : /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    : }
    :
    : protected void btnBeginNext_Click(object sender, EventArgs e)
    : {
    : btnBeginCounter++;
    : /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    : }[/code]
    :
    : How would I accomplish that?
    :
    :
    :

  • Sorry, I messed up my code when I typed it in. The declaration of btnBeginCounter is actually outside of the Page_Load function. So it only sets when the Page is not posted back.

    But my original question has more to do with how to call controls dynamically. For instance, when the button is pressed the 3rd time, I want to make panel pnlBegin4 visible and all other panels invisible, but when it's pressed a 4th time, I want to make panel pnlBegin5 visible and all other panels invisible. How would I do that? In other words, who do I use a variable to call an individual control by it's name?

    : U need ViewState to preserve variables across post backs ... here is the typical pattern ...
    :
    : [code]
    : protected void Page_Load(object sender, EventArgs e)
    : {
    : [b]
    : if(!Page.IsPostBack)
    : btnBeginCounter = 1;
    : else
    : btnBeginCounter = (int)ViewState["BeginCounter"];
    : [/b]
    :
    : if (Page.IsPostBack)
    : {
    : btnBeginCounter = (int)ViewState["btnBeginCounter"];
    : if (btnBeginCounter == 1)
    : {
    : btnBeginPrev.Enabled = false;
    : }
    : if (btnBeginCounter == 8)
    : {
    : btnBeginNext.Enabled = false;
    : }
    : }
    : }
    :
    : protected void btnBeginPrev_Click(object sender, EventArgs e)
    : {
    : btnBeginCounter--;
    : /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    : }
    :
    : protected void btnBeginNext_Click(object sender, EventArgs e)
    : {
    : btnBeginCounter++;
    : /*Insert code here for pnlBegin[btnCounter].Visible = true;*/
    : }
    :
    : [b]
    : protected void Page_PreRender(object sender, EventArgs e)
    : {
    : ViewState["BeginCounter"] = btnBeginCounter;
    : }
    : [/code]
    : [/b]
    :

  • [b][red]This message was edited by DrMarten at 2006-12-2 11:21:46[/red][/b][hr]

    : I have 8 panels, pnlBegin1 - pnlBegin8. I have an integer that increments and decrements based on two buttons. It starts at 1 and stops at 8. What I want to do is use the number to change a certain panel to visible. For instance, if the user has clicked the button two times, pnlBegin3 should be visible.

    _______________________________________________________________________

    Hi,

    If you are doing this in Visual Studio you could probably use the following statement in your C code?
    [code]
    Imports Microsoft.VisualBasic
    [/code]

    Put the previous code statement at the top of your code window or use the following code as a module?
    [code]
    [green][b]
    'The first two variables are GLOBAL.
    [/b][/green]

    Dim btnNumber As Integer = 0
    Dim pnlArray(7) As System.Windows.Forms.Panel

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    pnlArray(0) = Me.Panel1
    pnlArray(1) = Me.Panel2
    pnlArray(2) = Me.Panel3
    pnlArray(3) = Me.Panel4
    pnlArray(4) = Me.Panel5
    pnlArray(5) = Me.Panel6
    pnlArray(6) = Me.Panel7
    pnlArray(7) = Me.Panel8

    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
    If btnNumber = 0 Then
    btnNumber = 7
    Else
    btnNumber = btnNumber - 1
    End If
    selectPanel(btnNumber)
    [green][b]'Delete the next line of code if you want.[/b][/green]
    TextBox1.Text = "Panel " & CStr(btnNumber)
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    If btnNumber = 7 Then
    btnNumber = 0
    Else
    btnNumber = btnNumber + 1
    End If
    selectPanel(btnNumber)
    [green][b]'Delete the next line of code if you want.[/b][/green]
    TextBox1.Text = "Panel " & CStr(btnNumber)
    End Sub

    Private Sub selectPanel(ByVal pnlNumber As Integer)
    Dim myPanel As Windows.Forms.Panel
    Dim index, pnlNum As Integer
    pnlNum = pnlNumber

    For index = 0 To 7
    myPanel = pnlArray(index)
    myPanel.Visible = False
    Next

    myPanel = pnlArray(pnlNumber)
    myPanel.Visible = True
    End Sub
    [/code]

    I made every panel on top of each other ( with a different background colour )
    [b][red]but you don't have to [/red][/b]as the last Sub makes them ALL invisible first then only shows the selected panel. ;-)

    I hope you can use the above idea/code. :-)



    Regards,

    Dr M.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories