Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I have a little problem. Let me share with you.

Let's say we have a bunch of buttons, checkBoxes, menuitems etc. [What I mean here is we have different kinds of controls.] When I presses a button something happens[like zooming a picture].

And then I'm clicking another button; and in 2nd buttons control, I want to check that if the 1st button is pressed.

Sample pseudocode for what I mean:

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory = @"C:\";
    saveFileDialog1.DefaultExt = ".bmp";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        ImOrtamCizdir.genelBmpNesnesi.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    }
}

private void button2_Click(object sender, EventArgs e)
{

    // What I ask is here.
    // if button1 is pressed?!
    if(button1.IsPressed())
    {
        // do something...
    }


     KontrolleriAc();
     bmp = new Bitmap(p_box_map.Width, p_box_map.Height);
     ImOrtamCizdir ortamcizdir = new ImOrtamCizdir(p_box_map, bmp, ZoomKontrolCarpan);

     bmp.RotateFlip(RotateFlipType.Rotate180FlipX);

     lbxKucultmeOranlari.Items.Add("scale_factor_x :" + ortamcizdir.ScaleFactorY);
     lbxKucultmeOranlari.Items.Add("scale_factor_y :" + ortamcizdir.ScaleFactorX);
     lbxKucultmeOranlari.Items.Add(ortamcizdir.ScaleFactor);
}




is it possible?

Can we check a control's situation in another control?

Sorry for my bad English...
My Best Regards...
Posted
Updated 11-Jul-11 1:36am
v4

Don't do it.

Your controls should reflect the state of business objects (whether through formal data binding or, for a simple case, through setting properties in event handlers). In a really simple application (one form) the 'business objects' can be local fields of the form. Then, in other places (such as the event handler for the button you are talking about), you can look up into those same objects.

The simplest modification of your provided code towards this ideal is:

private bool isSaved = false;

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory = @"C:\";
    saveFileDialog1.DefaultExt = ".bmp";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;
 
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        ImOrtamCizdir.genelBmpNesnesi.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    }

    isSaved = true;
}
 
private void button2_Click(object sender, EventArgs e)
{
 
    // What I ask is here.
    // if button1 is pressed?!
    if(isSaved)
    {
        // do something...
    }
 
     KontrolleriAc();
     bmp = new Bitmap(p_box_map.Width, p_box_map.Height);
     ImOrtamCizdir ortamcizdir = new ImOrtamCizdir(p_box_map, bmp, ZoomKontrolCarpan);
 
     bmp.RotateFlip(RotateFlipType.Rotate180FlipX);
 
     lbxKucultmeOranlari.Items.Add("scale_factor_x :" + ortamcizdir.ScaleFactorY);
     lbxKucultmeOranlari.Items.Add("scale_factor_y :" + ortamcizdir.ScaleFactorX);
     lbxKucultmeOranlari.Items.Add(ortamcizdir.ScaleFactor);
}


In a real application you'd want to store the file name, bind that to the titlebar of the form and so on – and you'd probably want that information to be somewhere other than in your main form. Sometimes I create an 'App', 'DataModel' or 'GlobalState' class to make it really explicit what is state information; other times I will have a 'Project' class or similar which manages the data and logic relating to a single file, and that can store the state information.

In general, having direct dependencies on UI controls for any kind of non-UI logic is a really bad idea. You should be able to redesign the UI, or reuse the core code as a back end library with no UI, and creating dependencies like this stops you from doing that and results in 'spaghetti code' problems.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 16:43pm    
Very good answer, my 5. I agree with you only... look having two buttons pressed is impossible anyway; please see my answer.
--SA
RaviRanjanKr 11-Jul-11 16:51pm    
Nice Answer, My 5
Un_NaMeD 12-Jul-11 4:59am    
Hi sir.
This is a nice suggestion as hoernchenmeister mentioned before.
I guess it's the way that I have to use.
Thanks anyway...
Sergey Alexandrovich Kryukov 12-Jul-11 10:28am    
No, there is good about this solution. It won't work. This is wonderful. You got all the explanations from Box and myself and actually a solution, but you want to pick something which won't even compile. IsClicked does not exist and cannot exist because the idea makes no sense. There is not a concept of "clicked", this is not a state at all.
--SA
Un_NaMeD 13-Jul-11 2:13am    
Hi sir.
I already know it.
I just want to ask if there is something like "clicked" to know if the control's activated.
My best regards...
The answer by Bob is very good. I only want to add: you can not click a button pressed while a click of other button is being handled. Only one button can be pressed at the moment.

There are 3-state buttons when this is not so, for example WPF System.Windows.Controls.Primitives.ToggleButton, so it has a property IsPressed. See http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.aspx[^].

I think you're talking about regular buttons. For them, your inquiry makes no sense.

—SA
 
Share this answer
 
Comments
Espen Harlinn 12-Jul-11 10:03am    
That was a fair reply, my 5
Sergey Alexandrovich Kryukov 12-Jul-11 10:26am    
Thank you, Espen.
--SA
If the container has both controls then it can see the state of them.

C#
// not real code
class MyForm {
  Button button1;
  Button button2;

  // more stuff

  void button2_click(ButtonClickEvent ev) {
    if (button1.someProperty) {
      // do this
    } else {
      // do that    }
  }
}
 
Share this answer
 
Comments
Un_NaMeD 11-Jul-11 6:11am    
Hi sir.

What do you mean by "If the container has both controls"?
Which container?
Can you explain a little more?
Thank you..
If the "container" lets say a panel (pnlMain) owns both buttons (btn1, btn2) you could do the following:

private void button2_Click(object sender, EventArgs e)
{
     if(pnlMain.Parent.Controls["btn1"].IsClicked()
     {
          // do something
     }
}

That of course only works if you have a button that supports IsClicked().
You might set btn1.Enabled = false if btn1 is clicked or set a private variable to controol the buttons state.

Cheers
Andy
 
Share this answer
 
v4
Comments
RaviRanjanKr 11-Jul-11 8:18am    
Always use "Pre" tag to wrap your code.
Un_NaMeD 11-Jul-11 8:21am    
Hi sir.
I didn't get what you mean by saying ...."if that button supports IsClicked()".

All buttons does'nt support for IsClicked()?

"set a private variable to controol the buttons state." is a nice suggestion but not in my situation :/

Thank you...
hoernchenmeister 11-Jul-11 8:26am    
You are totally right, buttons do not have support for IsClicked().
If you need that functionallity more often you could also create a UserControl that inherits from Button and add a property.

I had similar problems and solved them using the buttons Enabled property. This worked because btn1 could not be clicked 2 times.

On the btn1 click-event I disabled btn1 so I could check for btn1.Enabled.

cheers
Andy

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