Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi to all!

here is my code to set my menuitems visibility true
default all menu items are visible false

but the visibility remain false

C#
public static void function_name(int a2)
{
    try
    {
        string sql_string_sfunction_name = ("Data Source=32423424;Initial Catalog=sdgsdgasdg;User Id=gdggdfg;Password=sdertet;");
        SqlConnection conn1 = new SqlConnection(sql_string_sfunction_name);
        SqlCommand command1 = new SqlCommand("proc_function_name2", conn1);
        conn1.Open();
        command1.CommandType = CommandType.StoredProcedure;
        command1.Parameters.AddWithValue("@fnid", a2);
        SqlDataReader rdr1 = command1.ExecuteReader();
        if (rdr1.HasRows)
        {
            StringBuilder sb1 = new StringBuilder();
            while (rdr1.Read())
            {
                sb1.Append(((string)rdr1["systemfname_vc"]).ToString());
                function_name_string = sb1.ToString();
                function_list_display.Add(function_name_string);
                SecurityModule sm = new SecurityModule();
                sm.formm_display_function(function_name_string);
            }
        }
        else
        {
            MessageBox.Show("not found");
        }
        rdr1.Close();
        command1.ExecuteNonQuery();
        conn1.Dispose();
        conn1.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        MessageBox.Show("Error in database connection");
    }
}

public void formm_display_function(string display_form_str)
{
    foreach (ToolStripMenuItem menuItem in menuStrip1.Items)
    {
        foreach (ToolStripMenuItem subitem in menuItem.DropDownItems)
        {
            if (subitem.Name.Trim() == display_form_str)
            {
                subitem.Visible = true;
                break;
            }
        }
    }
}
Posted
Updated 8-Aug-11 2:15am
v2

Since you append the name of your menustrip item to the Stringbuilder each time round, and it is not a local variable but a class level one, the first time you will look for "name1", the second time you will look for "name1name2", and so forth - could this be your problem?
 
Share this answer
 
Comments
kami124 8-Aug-11 7:23am    
no, what i think problem is some wher here
subitem.Visible = true;

before all the code is working perfectally
Manfred Rudolf Bihy 8-Aug-11 7:33am    
Your other problem is you're creating a new form instance because you make function_name static and your other function is a non static. See my answer please!
Manfred Rudolf Bihy 8-Aug-11 7:30am    
Agreed! 5+
And that isn't the only problem lurking in OP's code.
Espen Harlinn 8-Aug-11 18:44pm    
My 5
kami124 9-Aug-11 1:28am    
yes this was my mistake
at the end i got it thanks
Aside from what OriginalGriff already mentioned you have another problem. If SecurityModule is the name of your form class you're in for trouble. public static void function_name(int a2) is static and in order to be able to call public void formm_display_function(string display_form_str) you're creating an instance of SecurityModule. When you then call sm.formm_display_function all the operations regarding the visiblity are made on the new instance of your form which isn't even being shown. Your function function_name should either be not static or you should make function formm_display_function also static and pass in this as a parameter.

—MRB
 
Share this answer
 
Comments
kami124 8-Aug-11 7:32am    
ya might be u r right
i will check it
Manfred Rudolf Bihy 8-Aug-11 7:35am    
What do you mean "might be". If your form class is SecurityModule then what I described will explain why you can't see any changes in your displayed form.
Espen Harlinn 8-Aug-11 18:44pm    
Good reply, my 5
kami124 9-Aug-11 1:27am    
sorry yestarday i was away so i couldnt reply you were absolutaley right
thanks a lot
Manfred Rudolf Bihy 9-Aug-11 5:51am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900