Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
GeneralRe: Playing Sounds through the Speakers Pin
max2929711-Sep-07 12:17
max2929711-Sep-07 12:17 
Questionone object's private variable affecting another object's, but it shouldn't [modified] Pin
dfn10-Sep-07 16:23
dfn10-Sep-07 16:23 
Questioncreating a mp3 scratching program [modified] Pin
dman1000110-Sep-07 12:50
dman1000110-Sep-07 12:50 
AnswerRe: creating a mp3 scratching program Pin
VirtualVoid.NET10-Sep-07 21:46
VirtualVoid.NET10-Sep-07 21:46 
QuestionArray of Regions. Elements seem to disappear. [modified] Pin
hain10-Sep-07 11:18
hain10-Sep-07 11:18 
AnswerRe: Array of Regions. Elements seem to disappear. Pin
Giorgi Dalakishvili10-Sep-07 11:24
mentorGiorgi Dalakishvili10-Sep-07 11:24 
GeneralRe: Simple code example [modified] Pin
hain10-Sep-07 12:28
hain10-Sep-07 12:28 
GeneralRe: Simple code example Pin
Patrick Etc.10-Sep-07 12:49
Patrick Etc.10-Sep-07 12:49 
A little smart debugging and a glance in Reflector exposes the problem.

If you place a breakpoint at this line:

myControl.Region = region[currRegionIndex];

and examine the region array, each Region object has a Non-Public member called "nativeRegion". This is an IntPtr handle to a Win32 Rgn object. The first time you use the region, that IntPtr value is not zero - that is, item != IntPtr.Zero. Once you assign the region to the control, that same value becomes 0.

Looking at the code for Control.Region in Reflector exposes why:

IntPtr zero = IntPtr.Zero;
try
{
    if (value != null)
    {
        zero = this.GetHRgn(value);
    }
    if (this.IsActiveX)
    {
        zero = this.ActiveXMergeRegion(zero);
    }
    if (UnsafeNativeMethods.SetWindowRgn(new HandleRef(this, this.Handle), new HandleRef(this, zero), SafeNativeMethods.IsWindowVisible(new HandleRef(this, this.Handle))) != 0)
    {
        zero = IntPtr.Zero;
    }
}
finally
{
    if (zero != IntPtr.Zero)
    {
        SafeNativeMethods.DeleteObject(new HandleRef(null, zero));
    }
}


You see what's going on. The region is being deleted once it is used successfully.

The solution, then, is to assign a copy of each region instead, using the Region.Clone method:

<code>        public Form1()
        {
            InitializeComponent();

            GraphicsPath gp0 = new GraphicsPath();
            gp0.AddEllipse(myControl.ClientRectangle);
            region[0] = new Region(gp0);

            GraphicsPath gp1 = new GraphicsPath();
            gp1.AddRectangle(myControl.ClientRectangle);
            region[1] = new Region(gp1);

            myControl.Region = region[1].Clone();
            Controls.Add(myControl);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            currRegionIndex = (currRegionIndex + 1) % 2;

            myControl.Region = region[currRegionIndex].Clone();

            myControl.Refresh();
        }</code> 

GeneralRe: Simple code example Pin
hain10-Sep-07 16:12
hain10-Sep-07 16:12 
QuestionModal Dialog [modified] Pin
kamalesh8210-Sep-07 9:34
kamalesh8210-Sep-07 9:34 
AnswerRe: Modal Dialog Pin
Christian Graus10-Sep-07 9:52
protectorChristian Graus10-Sep-07 9:52 
GeneralRe: Modal Dialog Pin
kamalesh8210-Sep-07 9:58
kamalesh8210-Sep-07 9:58 
GeneralRe: Modal Dialog Pin
Christian Graus10-Sep-07 10:00
protectorChristian Graus10-Sep-07 10:00 
QuestionPicture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:09
Jordanwb10-Sep-07 9:09 
AnswerRe: Picture Box created by code not showing up Pin
Ermak8610-Sep-07 9:38
Ermak8610-Sep-07 9:38 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:53
Jordanwb10-Sep-07 9:53 
GeneralRe: Picture Box created by code not showing up Pin
Christian Graus10-Sep-07 9:55
protectorChristian Graus10-Sep-07 9:55 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 9:58
Jordanwb10-Sep-07 9:58 
GeneralRe: Picture Box created by code not showing up Pin
Ermak8610-Sep-07 10:02
Ermak8610-Sep-07 10:02 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 10:04
Jordanwb10-Sep-07 10:04 
GeneralRe: Picture Box created by code not showing up Pin
Ermak8610-Sep-07 10:13
Ermak8610-Sep-07 10:13 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 10:18
Jordanwb10-Sep-07 10:18 
GeneralRe: Picture Box created by code not showing up Pin
Skippums10-Sep-07 10:34
Skippums10-Sep-07 10:34 
GeneralRe: Picture Box created by code not showing up Pin
Jordanwb10-Sep-07 10:42
Jordanwb10-Sep-07 10:42 
GeneralRe: Picture Box created by code not showing up Pin
Skippums10-Sep-07 10:48
Skippums10-Sep-07 10:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.