How to remove all views in region in CompositeWPF/Silverlight - wpf

I want remove all views from my region in Composite. I'm using Silverlight 4.0.
And unfortunately, this code crashes with OutOfRangeException inside Composite.
List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);
foreach (object view in views)
{
_regionManager.Regions["NavigationRegion"].Remove(view);
}
This is my callstack:
[External Code]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs
e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs})
Line 102 + 0x24 bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.RemoveAndNotify(System.Collections.IList
items = Count = 1) Line 45 + 0x2b bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.UnderlyingCollection_CollectionChanged(object
sender = Count = 0,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e =
{System.Collections.Specialized.NotifyCollectionChangedEventArgs})
Line 153 + 0xb bytes C# [External Code]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(object
view = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView}) Line
230 + 0x1b bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Components.ViewController.linksRegion_LanguageChanged(object
sender = {Kite.MyApp.SlUI.Controls.LinksUserControl},
Kite.MyApp.SlUI.Common.EventArgs e =
{Kite.MyApp.SlUI.Common.EventArgs}) Line 77 + 0x2d bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.OnLanguageChanged(string
newLanguage = "en-GB") Line 37 + 0x32 bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.ComboBox_SelectionChanged(object
sender = {System.Windows.Controls.ComboBox},
System.Windows.Controls.SelectionChangedEventArgs e =
{System.Windows.Controls.SelectionChangedEventArgs}) Line 31 + 0xb
bytes C# [External Code]
What did I wrong?
P.S. In my region I have 3 views. So, composite crashes on removing the third.
Hoping for you help :). Thank you.
UPD: I downloaded debug symbols and found that problem is only in Accordion control as region.
This is full call stack:
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument, System.ExceptionResource resource) + 0x41 bytes
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x10 bytes
mscorlib.dll!System.Collections.Generic.List<object>.this[int].get(int index = 0) + 0x13 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.GetItemImpl(int index) + 0x15 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.GetItemImplSkipMethodPack(int index) + 0xb bytes
System.Windows.dll!System.Windows.PresentationFrameworkCollection<System.__Canon>.this[int].get(int index) + 0xb bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.UnselectItem(int index = 0, object item = null) + 0x1aa bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.ChangeSelectedIndex(int oldIndex = 0, int newIndex = -1) + 0x14c bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.OnSelectedIndexPropertyChanged(System.Windows.DependencyObject d = {System.Windows.Controls.Accordion}, System.Windows.DependencyPropertyChangedEventArgs e = {System.Windows.DependencyPropertyChangedEventArgs}) + 0x1d9 bytes
System.Windows.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp = {System.Windows.CustomDependencyProperty}, object oldValue, object newValue) + 0x51 bytes
System.Windows.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) + 0xf5 bytes
System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet) + 0x2ad bytes
System.Windows.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) + 0xb bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.SelectedIndex.set(int value = -1) + 0x4d bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) + 0x737 bytes
System.Windows.dll!System.Windows.Controls.ItemsControl.OnItemCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x52 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.NotifyCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.NotifyItemsCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x1f bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.EnumerableCollectionView.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x4f bytes
System.Windows.dll!System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x3d bytes
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) Line 102 + 0x24 bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.RemoveAndNotify(System.Collections.IList items = Count = 1) Line 45 + 0x2b bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.UnderlyingCollection_CollectionChanged(object sender = Count = 0, System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) Line 153 + 0xb bytes C#
System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes
System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<System.__Canon>.RemoveItem(int index) + 0xa1 bytes
mscorlib.dll!System.Collections.ObjectModel.Collection<Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata>.Remove(Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata item) + 0x75 bytes
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(object view = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView}) Line 230 + 0x1b bytes C#

Check first that _regionManager.Regions["NavigationRegion"] actually exists and is non null like so
if (_regionManager.Regions["NavigationRegion"] != null)
{
List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);
foreach (object view in views)
{
_regionManager.Regions["NavigationRegion"].Remove(view);
}
}
It may be that NavigationRegion isnt set up properly, or there is a typo or something like that

use a for loop not foreach when modifying a collection

if you want to remove just one, you could do:
_regionManager.Regions["NavigationRegion"].Remove(
_regionManager.Regions["NavigationRegion"].GetView("viewName"));

Related

how to create multi line chart with single datatable in vb.net

I am working on vb.net application where i have to create a multi line chart. The table coming from the database using storing procedure is:-
TagName On Off Trip
P1 0 0 1
P2 0 1 1
P3 0 1 0
Q1 0 1 0
Q2 1 0 1
Q3 2 2 2
W1 4 2 1
W2 2 0 1
W3 1 1 0
W4 0 1 1
W5 2 1 1
And the code in vb.net i used to bind the chart named "chTrend" is:-
ds = ObjTags.GetTrendData()
If (ds.Tables(0).Rows.Count > 0) Then
dt = ds.Tables(0)
chTrend.DataSource = dt
chTrend.Series(0).XValueMember = "TagName"
chTrend.Series(0).YValueMembers = "On"
chTrend.Series(1).XValueMember = "TagName"
chTrend.Series(1).YValueMembers = "Off"
chTrend.Series(1).XValueMember = "TagName"
chTrend.Series(1).YValueMembers = "Trip"
For i = 0 To 2
chTrend.Series(i).ChartType = SeriesChartType.Line
chTrend.Series(i).IsVisibleInLegend = True
chTrend.Series(i).IsValueShownAsLabel = True
chTrend.Series(i).ToolTip = "Data Point Y Value #VALY{G}"
chTrend.Series(i).BorderWidth = 3
Next
End If
When i run the program a Error is coming as following
"Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index"
At the position
chTrend.Series(0).XValueMember = "TagName"
how to solve this?
Assuming that your DataTable only has the four DataColumns {TagName, On. Off. Trip}, then replace everything in your shown code below the line:
chTrend.DataSource = dt
with this.
chTrend.Series.Clear() ' clears any existing series
Dim s As Series
For columnIndex As Int32 = 1 To dt.Columns.Count - 1
Dim name As String = dt.Columns(columnIndex).ColumnName
s = chTrend.Series.Add(name)
s.XValueMember = dt.Columns(0).ColumnName
s.YValueMembers = name
s.ChartType = SeriesChartType.Line
s.IsVisibleInLegend = True
s.IsValueShownAsLabel = True
s.ToolTip = "Data Point Y Value #VALY{G}"
s.BorderWidth = 3
Next
chTrend.DataBind() ' loads the data from dt to the chart
The Chart databinding is a not a binding in the normal sense of binding. You need to tell the chart to copy the data from the source so that it has something to plot. That is what the DataBind method does.

How to define byte array in vb6 and send it with Winsock

Well in c# it would look like:
Byte[] ulaznipodaci = new Byte[] { 0x08, 0x3F, 0x20, 0x03, 0x00, 0x00, 0x05, 0x00 };
sck.Send(ulaznipodaci);
I need this kind of code in vb6. I tried:
Dim ulaznipodaci() As Byte
ulaznipodaci = Array(&H8, &H3F, &H20, &H3, &H0, &H0, &H5, &H0)
Winsock2.SendData ulaznipodaci
But this code gives me an error on the line ulaznipodaci = Array(&H8, &H3F, &H20, &H3, &H0, &H0, &H5, &H0)
saying: Run time error '13':
Type mismatch.
I searched google but couldn't find anything. Please help, I'm kinda new in vb6.
Thanks in advance.
In VB6, you have to define your array's bounds and then populate each item separately:
Dim ulaznipodaci(7) As Byte
'// VB6 array is zero based unless you specify lower to upper bounds
ulaznipodaci(0) = &H8
ulaznipodaci(1) = &H3F
ulaznipodaci(2) = &H20
ulaznipodaci(3) = &H3
ulaznipodaci(4) = &H0
ulaznipodaci(5) = &H0
ulaznipodaci(6) = &H5
ulaznipodaci(7) = &H0
If, later you need to add more items:
ReDim Preserve ulaznipodaci(8)
ulaznipodaci(8) = &H0
.. or for example store in a string and then read in cycle
St="&H8, &H3F, &H20, &H3, &H0, &H0, &H5, &H0"
for i = 1 to N
pos = (i-1)*6 ' poition &
ulaznipodaci(i)=mid(St,pos,4) ' we do not need "," and a space
next

creating a 2D array from String arrays

I have built a program which takes 13 comma separated values of user input and appends them into a text file, each to a new line. Not too difficult, but I am new.
Now I am trying to bring these single line arrays back from the file into a 2D array where I hope to work with the values. I think I am close, but there is an obvious mistake that my inexperience does not allow me to see. I also know that there are much better class strategies written to handle this task, but again . . . newbie.
What I think is happening is that the entire file is being written into one location in the 2D array(I can sysout the finalArray , and it looks right, but only in position [0][0]). So how do I get each "z" to fill in the next open slot as it processes through ?. Hope you can help, here is my code:
String[][] finalArray = new String[100][13];
int i=-1;
try
{
x = new BufferedReader(new FileReader(readFile));
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
try
{
while ((line = x.readLine()) != null)
{
String[] y = line.split(separator);
try
{
z ="["+ y[0] + "," + y[1] + "," + y[2] + "," + y[3]
+ "," + y[4] + "," + y[5] + "," + y[6] + "," + y[7]
+ "," + y[8] + "," + y[9] + "," + y[10] + y[11]
+ "," + y[12] + "," + y[13]+"]";
finalArray[i+1][0] = z;
z is not array it is a String but You have array of string array
therefore your loop must look like this
i=0; // array starts with 0 Item
while ((line = x.readLine()) != null)
{
finalArray[i++] = line.split(separator);
}

Winforms DataGridView Cell Selection Change gives error

I have a winforms screen and the below code in Form_Activated event.
if (genlGrid1.Rows.Count > 0 && genlGrid1.Rows.Count <= genlGridIndex + 1 && (genlGridIndex >= 0 && genlGridIndex < 2))
{
//Looks like below line is where exception occurs
genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;
}
I'm very sure that the grid has 2 rows and 9 cells when this line of code is executed.
When this line of code is executed, I get the error below. No idea what is going on with the grid. What is SetCurrentCellAddressCore and why are we getting this exception??
Can some one please help?
System.ArgumentOutOfRangeException:
Specified argument was out of the
range of valid values. Parameter name:
columnIndex at
System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean
setAnchorCellAddress, Boolean
validateCurrentCell, Boolean
throughMouseClick) at
System.Windows.Forms.DataGridView.SetSelectedCellCoreInternal(Int32
columnIndex, Int32 rowIndex, Boolean
selected) at
System.Windows.Forms.DataGridViewCell.set_Selected(Boolean
value)
Should it be
if (genlGrid1.Rows.Count > 0 &&
genlGrid1.Rows.Count >= genlGridIndex + 1 &&
(genlGridIndex >= 0 && genlGridIndex < 2))
{
genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;
}
?
Note genlGrid1.Rows.Count >= genlGridIndex + 1

Converting binary file to Base64 string

I need to convert uploaded binary files to base64 string format on the fly. I'm using ASP, Vbscript. Using Midori's component for base64 conversion. For small size files (<20K) the performance is okay. But when it exceeds 75 or 100K, its totally lost. Is there any efficient way to convert big binary files (2MB) to base64 string format?
Thanks in advance,
Kenney
I have solved this issue by implementing a .net component for converting to base64 string. The hard part is the binary data sent to the .net COM from ASP is received as a string. Convert.ToBase64() accepts only byte[]. So I tried converting string to byte[].
But the encoding available in .net (Unicode, ASCII, UTF) doesn't works fine. There are data loss, while these encodings are used. Finally I get it done by using StringReader object. Read char by char(16 bit) and converted them to (8 bit) byte[] array.
And the performance is best.
Regards,
Siva.
you should use the .NET methods Convert.ToBase64String and Convert.FromBase64String.
Use the Convert.FromBase64String( ) method. This will give you the binary
data back (as a byte array).
To convert binary data to a Base64 string see conversion functions from binary data to a string in vbscript
from http://www.motobit.com/tips/detpg_Base64Encode/
Function Base64EncodeBinary(inData)
Base64EncodeBinary = Base64Encode(BinaryToString(inData))
End Function
Function Base64Encode(inData)
'rfc1521
'2001 Antonin Foller, Motobit Software, http://Motobit.cz
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim cOut, sOut, I
'For each group of 3 bytes
For I = 1 To Len(inData) Step 3
Dim nGroup, pOut, sGroup
'Create one long from this 3 bytes.
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
&H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
'Oct splits the long To 8 groups with 3 bits
nGroup = Oct(nGroup)
'Add leading zeros
nGroup = String(8 - Len(nGroup), "0") & nGroup
'Convert To base64
pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
'Add the part To OutPut string
sOut = sOut + pOut
'Add a new line For Each 76 chars In dest (76*3/4 = 57)
'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
Next
Select Case Len(inData) Mod 3
Case 1: '8 bit final
sOut = Left(sOut, Len(sOut) - 2) + "=="
Case 2: '16 bit final
sOut = Left(sOut, Len(sOut) - 1) + "="
End Select
Base64Encode = sOut
End Function
Function MyASC(OneChar)
If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function
Use MSXML to do the encoding for you. Here is function encapsulating the procedure:-
Function ToBase64(rabyt)
Dim xml: Set xml = CreateObject("MSXML2.DOMDocument.3.0")
xml.LoadXml "<root />"
xml.documentElement.dataType = "bin.base64"
xml.documentElement.nodeTypedValue = rabyt
ToBase64 = xml.documentElement.Text
End Function
Note this will include linebreaks in the base64 encoding but most base64 decoders are tolerant of linebreaks. If not you could simpy use Replace(base64, vbLF, "") to remove them, this will still be quicker than a pure VBScript solution.
Edit Example usage:-
Dim sBase64: sBase64 = ToBase64(Request.BinaryRead(Request.TotalBytes))
I use next code for c#:
public static string ImageToBase64(Image image, ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public static Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
for vbscript see http://www.freevbcode.com/ShowCode.asp?ID=5248 maybe help you.
There is a good discussion of this in base64-encode-string-in-vbscript.
In addition, I have found this site useful for trying to eek speed out of vb code. There are several variants of base 64 there for vb6 that are quite fast.
This is what worked for me
Function Base64DataURI(url)
'Create an Http object, use any of the four objects
Dim Http
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
'Send request To URL
Http.Open "GET", url, False
Http.Send
'Get response data As a string and encode as base64
Base64DataURI = Encrypt(Http.ResponseText)
End Function
In my case the URL is a script that generates a barcode on the fly and needed to encode to include that in emails.
Encrypt is a pretty standard function we use to encode as Base64, but the main concept we needed was to get the file via URL not file system.

Resources