Not Calling Correctly
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Not Calling Correctly

  1. #1
    Join Date
    Jul 2008
    Posts
    18

    Not Calling Correctly

    I have the code laid out, but can't seem to find my error. I have all the buttons, text boxes, etc. set to public on a Form (frmMain) and am trying to call them from a class (Assign.cs). I get no errors, but it doesn't seem to "post" in the text box. Code is below:

    Code calling the contextMenuStrip
    Code:
     private void btnRoll1_Click(object sender, EventArgs e)
            {
                if (btnRoll1.Text != "")
                {
                    contextAssignValues.Show(new Point(Cursor.Position.X, Cursor.Position.Y));
                    buttonToMenu = "btnRoll1";
                }
            }
    Code for the element "assignStrength," a menu option in the contextMenuStrip

    Code:
            private void assignStrength_Click(object sender, EventArgs e)
            {
                //assign is defined under Public Partial Class Form : frmMain as
               // Assign assign = new Assign();
                assign.assignStrength(buttonToMenu);
            }
    Finally, the code in the Assign.cs assignStrength method. buttonToMenu is a passed variable that holds a string of "btnRoll1" or "btnRoll2" etc so I know which button was clicked to get the contextMenu to show.

    Code:
    class Assign
        {
            private static frmMain frmMainControl = new frmMain();
    
            public void assignStrength(string x)
            {
                string buttonToMenu = x;
                if (frmMainControl.txtStrScore.Text == "")
                {
                    switch (buttonToMenu)
                    {
                        case "btnRoll1":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll1.Text;
                            frmMainControl.btnRoll1.Text = "";
                            break;
                        case "btnRoll2":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll2.Text;
                            frmMainControl.btnRoll2.Text = "";
                            break;
                        case "btnRoll3":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll3.Text;
                            frmMainControl.btnRoll3.Text = "";
                            break;
                        case "btnRoll4":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll4.Text;
                            frmMainControl.btnRoll4.Text = "";
                            break;
                        case "btnRoll5":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll5.Text;
                            frmMainControl.btnRoll5.Text = "";
                            break;
                        case "btnRoll6":
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll6.Text;
                            frmMainControl.btnRoll6.Text = "";
                            break;
                        default:
                            MessageBox.Show("Unexpected Error Returned from Case buttonToMenu.", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                    }
                }
                else
                {
                    string tempValue = "";
                    switch (buttonToMenu)
                    {
                        case "btnRoll1":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll1.Text;
                            frmMainControl.btnRoll1.Text = tempValue;
                            break;
                        case "btnRoll2":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll2.Text;
                            frmMainControl.btnRoll2.Text = tempValue;
                            break;
                        case "btnRoll3":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll3.Text;
                            frmMainControl.btnRoll3.Text = tempValue;
                            break;
                        case "btnRoll4":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll4.Text;
                            frmMainControl.btnRoll4.Text = tempValue;
                            break;
                        case "btnRoll5":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll5.Text;
                            frmMainControl.btnRoll5.Text = tempValue;
                            break;
                        case "btnRoll6":
                            tempValue = frmMainControl.txtStrScore.Text;
                            frmMainControl.txtStrScore.Text = frmMainControl.btnRoll6.Text;
                            frmMainControl.btnRoll6.Text = tempValue;
                            break;
                        default:
                            MessageBox.Show("Unexpected Error Returned from Case buttonToMenu OR tempValue", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                    }
                }
            }
        }
    I can tell by commenting out all the code in the public void assignStrength(string x) function that the values ARE getting passed by setting messagebox.show(buttonToMenu), however nothing every actually shows up in txtStrengthScore.Text or anywhere else for that matter.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Not Calling Correctly

    Code:
    private static frmMain frmMainControl = new frmMain();
    "new frmMain()" returns a frmMain object, but not the one that you are looking at when the code is run. It is created but not yet visible, it is the same as creating two integer variables, x and y, setting x equal to 10 and also expecting y to be set to 10.

    Some advice; do not create classes that depend on the implementation of your form, you have it backward. Your data classes should hold data and your form class will use these data classes to store and manipulate the data, not to display it.

  3. #3
    Join Date
    Jul 2008
    Posts
    18

    Re: Not Calling Correctly

    I see.. so how do I call my already created frmMain?

    And thanks.. I'll try to work through that..

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Not Calling Correctly

    I would reiterate that holding a reference to your main form in another class is a terrible design and is often what most beginners will jump to because it is easier (I did ). I suggest that you read into using events for passing information between classes.

  5. #5
    Join Date
    Jul 2008
    Posts
    18

    Re: Not Calling Correctly

    Alright.. well as far as buttonToMenu variable is concerned, is there a way to constantly pass it? For example.. I've got 6 different buttons that all call the conextMenuStrip when clicked. Is there a way to tell the contextMenuStrip WHICH button caused it to show up, so that when an option on the menu is clicked, I know which button it came from?

    A "roll" button is pressed. When the roll button is pressed, it generates 6 random numbers. The 6 random numbers are dispersed to each button (1 number to each button). What is supposed to happen is when button1 is pressed, a context menu pops up with several options. Each option assigns it to a different text box. Once it is clicked, the corresponding text box gets the number and the button.text is cleared.

    However, if button 2 is pressed and sent to a text box that already has a value in it, then the value in that text box and the value on the button need to switch places.

    In play:
    Button1 Clicked -> Context Menu shown -> "Strength" option selected. txtStrength.Text obtains Button1.Text value.

    Button2 Clicked -> Context Menu shown -> "Strength" option selected. txtStrength.Text obtains Button2.Text value, while Button2.Text obtains txtStrength.Text value.

    The only way I see being able to do this is having a holding variable that is filled when a button is clicked (so that when the values from text box to button is switched, I know where its going) or to create 6 different context menus with the same items. Either seems redundant. Do you know a better way to do this? I'm always open for suggestions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center