Hi Guys i want to know how to access one method from another. i have two buttons.one method is button1_click and second button2_click.What i want to do is to put a if condintion in button2, something like if(button1.click = true).Can anyone help me with this.Thanks

Recommended Answers

All 24 Replies

I didn't get it! can you please clarify!
If you want to handle two buttons click in one handler you set the event handler for each of which by its properties window to button_Click method.

void button_Click(object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
if(clickedButton.Name == "Button1")
{
MessageBox.Show("Button 1 Clicked");
/// some operations 
}
else if(clickedButton.Name == "Button2")
{
MessageBox.Show("Button 2 Clicked");
/// some operations 
}
}

I am sorry for the confusion the problem is that i have a button and a linklabel.I have a link label named 'a'.When i click on it it pulls all the words that starts with 'a' in general dictionary.What i want is that when i click on personal dictionary it should pull all the words that starts with 'a' from personal dictionary rather than general dictionary.

Buddy, It's a business scenario! what the technical problem you face?!

technical problem is.... when i click ona button the control of the link label should change.It has a query and i want the control to change to the other query

That's the code when you click on the button -when you assign button_Click method to its Click event handler-

void button_Click(object sender, EventArgs e)
{
//here change the properties of the LinkedButton...
}

I can't.When i am trying to do that it sasy cannot conver link type to button type.this is the exact error "Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.LinkLabel'".Thanks

Give me your code :) with just exceptions I can't assume what did you write.

Why dont use simply use two events handlers with a flag ; a simple boolean that would be called button1Clicked, buttonLabelClicked?

this is my code

private void button1_Click_1(object sender, EventArgs e)
        {
            LinkLabel link = (LinkLabel)sender;
            if (LinkLabel.Equals("linkLabel1"))
            {
                SqlDataAdapter dtadp = new SqlDataAdapter();
                DataTable dtbl = new DataTable();

                SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
                cn.Open();
                DataSet data = new DataSet();
                string myString = "";
                myString = @"Select distinct word from wordTbl where word like 'a%'";
                SqlCommand cmd = new SqlCommand(myString, cn);

                SqlDataReader rdr = cmd.ExecuteReader();
                cn.Close();
                dtadp.SelectCommand = cmd;
                dtadp.Fill(dtbl);
                dataGridView2.DataSource = dtbl;
            }
            else
            {


               dateTimePicker1.Visible = true;
               SqlDataAdapter dtadp = new SqlDataAdapter();
               DataTable dtbl = new DataTable();

               SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
               cn.Open();
               DataSet data = new DataSet();
               string myString = "";
               myString = @"Select distinct word from wordTbl";
               SqlCommand cmd = new SqlCommand(myString, cn);

               SqlDataReader rdr = cmd.ExecuteReader();
               cn.Close();
               dtadp.SelectCommand = cmd;
               dtadp.Fill(dtbl);
               dataGridView2.DataSource = dtbl;
           }
        }

Reasonable! You handle the Click event of Button then cast it as LinkLabel! Use another event handler for the Click of LinkLabel.

What you mean .Can you tell me how to do that.Thanks.

Simply... implement the event handler for the LinkButton, Double click on your LinkButton. VS will switch you to code view inside this method

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
//here to write your code 
LinkLabel link = (LinkLabel)sender;
            if (LinkLabel.Equals("linkLabel1"))
            {
                SqlDataAdapter dtadp = new SqlDataAdapter();
                DataTable dtbl = new DataTable();

                SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
                cn.Open();
                DataSet data = new DataSet();
                string myString = "";
                myString = @"Select distinct word from wordTbl where word like 'a%'";
                SqlCommand cmd = new SqlCommand(myString, cn);

                SqlDataReader rdr = cmd.ExecuteReader();
                cn.Close();
                dtadp.SelectCommand = cmd;
                dtadp.Fill(dtbl);
                dataGridView2.DataSource = dtbl;
            }
            else
            {


               dateTimePicker1.Visible = true;
               SqlDataAdapter dtadp = new SqlDataAdapter();
               DataTable dtbl = new DataTable();

               SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
               cn.Open();
               DataSet data = new DataSet();
               string myString = "";
               myString = @"Select distinct word from wordTbl";
               SqlCommand cmd = new SqlCommand(myString, cn);

               SqlDataReader rdr = cmd.ExecuteReader();
               cn.Close();
               dtadp.SelectCommand = cmd;
               dtadp.Fill(dtbl);
               dataGridView2.DataSource = dtbl;
           }
        }

getting error an object reference is required at the line if (LinkLabel.Equals("linkLabel1"))

replace if (LinkLabel.Equals("linkLabel1")) with

if (LinkLabel.Text("linkLabel1"))

cant do .text.Its not even the option

oh i seee,

private void button1_Click_1(object sender, EventArgs e)        
{
       if(sender is LinkLabel)
        {
               LinkLabel link = (LinkLabel)sender;
         }
       else
       {
              //do whatever else
       }
}

And then use the following

if(link.Name == "linklabel1")

it worked but issue is still the same.the even of linklabel should actually be button.but it doesnt let me do that

What :)

this is what i did now and nothing works

private void button1_Click_1(object sender, EventArgs e)
        {
            if (sender is LinkLabel)
            {
              LinkLabel link = (LinkLabel)sender;
              
                if (link.Name == "linkLabel1")
                {
                SqlDataAdapter dtadp = new SqlDataAdapter();
                DataTable dtbl = new DataTable();

                SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
                cn.Open();
                DataSet data = new DataSet();
                string myString = "";
                myString = @"Select distinct word from wordTbl where word like 'a%'";
                SqlCommand cmd = new SqlCommand(myString, cn);

                SqlDataReader rdr = cmd.ExecuteReader();
                cn.Close();
                dtadp.SelectCommand = cmd;
                dtadp.Fill(dtbl);
                dataGridView2.DataSource = dtbl;
            }
                
            else
            {


               dateTimePicker1.Visible = true;
               SqlDataAdapter dtadp = new SqlDataAdapter();
               DataTable dtbl = new DataTable();

               SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
               cn.Open();
               DataSet data = new DataSet();
               string myString = "";
               myString = @"Select distinct word from wordTbl";
               SqlCommand cmd = new SqlCommand(myString, cn);

               SqlDataReader rdr = cmd.ExecuteReader();
               cn.Close();
               dtadp.SelectCommand = cmd;
               dtadp.Fill(dtbl);
               dataGridView2.DataSource = dtbl;
           }
        }
            }

A closing tag is missing to your if. I might have misunderstood what you want to do, use .text Ramy seems to really insist. :P

ok i ll explain once again .sorry abt this.I have one for with label 'a' and 2 buttons one is general dictionary and another is personal dictionary.when i click on general dictionary and then clcik on label 'a' it shows all the words that starts with 'a' from general dictionary.now what i want to do is that when i select personal dictionary and clcik on 'a' it should display all the words that starts with 'a' from personal dictionary.

comeon guys ....is that it.

IMHO you should not use two buttons, but to radiobuttons in a groupbox control.
When you click on label 'a' show the contents of the dictionary according to the setting in the radiobox group.

i didnt get you.How will that make a difference.Cant i have 2 buttons on the same panel and do the same thing.Can you show me the syntax or an example.Thanks a lot for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.