OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-04-2009, 14:23
L_G_D L_G_D is offline
Member
 
Join Date: Apr 2009
Posts: 31
Question enable check box in form only if object is selected

I have a macro that's otherwise working great, but I have a couple check boxes in the form that only apply if an object is selected, so I'd like to only have them enabled if there is an object selected. Otherwise, no object selected, the check box is greyed out and can't be checked by accident. I tried a couple things, but they didn't work, any pointers?

Thanks
LD
Reply With Quote
  #2  
Old 01-04-2009, 17:19
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

Add Initialize event handler for your form (if you don't have one already) and add something like this:

Code:
Dim bSomeObjectsSelected As Boolean
bSomeObjectsSelected = Not ActiveShape Is Nothing
cbYourCheckBox1.Enabled = bSomeObjectsSelected
cbYourCheckBox2.Enabled = bSomeObjectsSelected
Reply With Quote
  #3  
Old 02-04-2009, 08:38
L_G_D L_G_D is offline
Member
 
Join Date: Apr 2009
Posts: 31
Default

Thanks for that, but I don't have an Initialize Event Handler that I know of, and I'm not quite sure were/how to add one? I've googled around a bit and found some references, but no exact examples. Do I add it to the main module or somewhere in the form code?

Sorry for being so dense, lol.

LD
Reply With Quote
  #4  
Old 02-04-2009, 08:47
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

In the form code. You'll see two comboboxes at the top of the code editor. The left one allows you to select and object and the right one is to add an event handler for that object.

Here is how you do it:

1. Select the form item in the Project Explorer window on the left in VBA (or press Ctrl-R to go to Project Explorer, then select your form)
2. Right-click the form item and select "View Code" from the pop-up menu
3. In the left combobox on top of the code window (which displays "(General)" by default), select "UserForm" item.
4. In the right combobox select "Initialize"

This would add the following event handler:

Code:
Private Sub UserForm_Initialize()

End Sub
Add your code to the body of the function and it will be executed as soon as the form is shown. In that function you can enable/disable your controls, initialize text and comboboxes and do other initializations you want before the user sees the form...

I hope it makes sense...
Reply With Quote
  #5  
Old 02-04-2009, 09:32
L_G_D L_G_D is offline
Member
 
Join Date: Apr 2009
Posts: 31
Default

Ah, excellent, that works perfectly, now if I could trouble you a bit more....

I have this code:

Code:
checkbox1.Visible = (checkbox2.Value = vbchecked)
what I'd like to do is reverse the effect it has. Right now, it makes checkbox1 invisible if checkbox2 is checked. I'd like it to make checkbox1 visible only if checkbox2 is checked, since cb1 is dependent on having cb2 available. At all other times, cb1 should not be visible. I've tried a few things, but can't get that one working either. I know I could just use the enabled instead of visible function, but I'm running out of room on the dialog (poor planning) and might want to "stack" a couple of options in one place that only appear when their parent option is checked.

Make any sense?

thanks again
LD
Reply With Quote
  #6  
Old 02-04-2009, 16:51
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

Are you in VB or VBA? In VBA form editor the checkbox don't have the value of vbChecked/vbUnchecked, etc... It's VB-only thing.

In VBA, a checkbox has a value of True (-1) if it is checked, False (0) if it is unchecked or Null if it is in indeterminate state (for 3-state checkboxes).

Here is a sample code for you. I created a form called "MyForm" and put two checkboxes on it - CheckBox1 and CheckBox2.

In the Code module for the form paste this code:

Code:
Option Explicit

Private Sub CheckBox1_Click()
    CheckBox2.Visible = (CheckBox1.Value <> False)
End Sub

Private Sub UserForm_Initialize()
    CheckBox1.Value = True ' Initial setting for the checkbox
End Sub
UserForm_Initialize will be called the first thing before the form is shown. This gives us the opportunity to set the initial values for the control. In this case, I chose to set checkbox1's state to checked

CheckBox1_Click event will be called each time you click checkbox1. This allows us to show/hide checkbox1 depending on the current check state of checkbox1.
Reply With Quote
  #7  
Old 03-04-2009, 14:31
L_G_D L_G_D is offline
Member
 
Join Date: Apr 2009
Posts: 31
Default

Yes, I'm in Corel Draw VBA, which is probably why it didn't make a difference when I changed vbchecked to vbunchecked.

I tried the new changes, and it works, but it makes the one check box always checked when the script is run. I want it to start off unchecked, and if the user checks it, the other check box appears. It can't default to a checked state, it's not always going to be used. If I tell it to start as false, then the other check box appears initally, but once you check then uncheck the first checkbox, the second one appears and disappears like it should. It's just that initial state that's throwing it off.

Here's what I wanted to get it to do:

Checkbox 1 only appears if something is selected, and is initally unchecked.
Checkbox 2 only appears if checkbox 1 is checked.

I don't know if that's even possible at this point.

BTW, what's the "Option Explicit" command, where should I put it, and what does it do?

Thanks again
LD


ps - I figured it out, at least it works the way I want, it might not be the "correct" way to do things, but it gets the job done. In the UserForm_Initialize section I have this:

Code:
ChkInclOut.Visible = (ChkSameSize.Value = True)
ChkSameSize.Value = False
That last line probably isn't needed I guess.

In the ChkSameSize_Click section, I have this:

Code:
ChkInclOut.Visible = (ChkSameSize.Value = True)
Which seems to be working. Might not be perfect, but it works.

I found that when I used the Option Explicit command, some of my code that included the VBChecked variable no longer worked. So I changed all those to True in commands like:

Code:
FraDesiredSize.Enabled = (chkAddBleed.Value = False)
Which is probably the right way to do it anyway.

So, on to the next issue. This form has a couple of text boxes for user input. The check boxes can modify those text boxes to automatically fill in certain values. But, if the user checks a box by mistake, then unchecks the box, the value stays there. How do I get value back to what it was before the box was checked accidentally?

Have a nice weekend, and thanks again.

LD

Last edited by L_G_D; 03-04-2009 at 15:23. Reason: Figured it out
Reply With Quote
  #8  
Old 03-04-2009, 15:45
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

Option Explicit tells the compiler to require explicit variable declaration. That is, you have to define a variable using Dim before you use it.

I prefer to always have this set as it helps find spelling errors and simple problems like yours with vbChecked where it is not defined in VBA. Consider this:

Code:
Dim myVariable1 As Long
Dim myVariable2 As Long
myVariable1 = 50
myVariable2 = myVarieble1
After this code, what do you think the value of myVariable2 be? 50? No, it'll be 0. The reason is I misspelled myVariable1 in the last line and typed it as myVarieble1. VBA tries to be polite and creates a new variable for you and initializes it with "Empty" which will result in 0 when set to a Long.

If you put "Option Explicit" at the top of the code module, then VBA will throw an error at compile time warning you that "myVarieble1" is not declared...

This allows you to fix those hard to find problems.

For your question about text boxes, there is no magic wand for this.. You need to do it yourself. When you put a check mark into a checkbox, you would add some code to set some text string to your text box, something along these lines:

Code:
Option Explicit

Sub CheckBox1_Click()
    If CheckBox1.Value Then
        TextBox1.Text = "Some string"
    End If
End Sub
What you need to do is to remember the previous value of the checkbox before you change it to your string. Then when the checkbox is unchecked, restore the old value:

Code:
Option Explicit

Private OldTextBox1String As String

Sub CheckBox1_Click()
    If CheckBox1.Value Then
        OldTextBox1String = TextBox1.Text
        TextBox1.Text = "Some string"
    Else
        TextBox1.Text = OldTextBox1String 
    End If
End Sub

Last edited by Alex; 03-04-2009 at 15:48.
Reply With Quote
  #9  
Old 07-04-2009, 09:37
L_G_D L_G_D is offline
Member
 
Join Date: Apr 2009
Posts: 31
Default

Great, thanks, I kinda thought that's what option explicit did when it started complaining about my variables. Good thinking to use it for error checking, btu I always type prefectly, so i dont have to wrory about that, LOL!

LD
Reply With Quote
  #10  
Old 08-04-2009, 07:17
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

Quote:
Originally Posted by L_G_D View Post
btu I always type prefectly, so i dont have to wrory about that, LOL!
If you made the typos in "btu", "i", "dont" "wrory" intentionally, then it was really funny
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to hide all but the selected object cooloox Macros/Add-ons 2 28-09-2008 21:19
Remove Mirror form All Selected Text vindaa CorelDRAW/Corel DESIGNER VBA 2 12-07-2008 19:47
VB Form from Object Click bprice CorelDRAW/Corel DESIGNER VBA 3 13-05-2008 11:30
Selected object to other page Duplicate method ? ljesus7 CorelDRAW/Corel DESIGNER VBA 5 12-02-2007 16:37
Retrieve Color Values from Selected Object Jon Lorber CorelDRAW/Corel DESIGNER VBA 2 19-08-2004 07:24


All times are GMT -5. The time now is 08:56.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com