Поможите перевести контрол на VB

Язык C#: программирование на C#, портирование кода C# на VB и VB на C#.

Модератор: Ramzes

Roman Koff
Постоялец
Постоялец
Аватара пользователя
 
Сообщения: 495
Зарегистрирован: 17.09.2008 (Ср) 9:22
Откуда: От туда

Поможите перевести контрол на VB

Сообщение Roman Koff » 26.10.2008 (Вс) 13:49

Господа сишники-вэбэшники, помогите перевести контрол с C# на VB.Net. Никак не выходит камменный цветок. Спотыкаюсь на функции GetResolvedDataSource

Исходник от сюда http://www.aspnetmania.com/Code/Code/128.html
Код: Выделить всё
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace DN2Portal.WebControlsEx
{

       /// <summary>
     /// Summary description for GroupDropDownList.
     /// </summary>
     [ToolboxData("<{0}:GroupDropDownList runat=server></{0}:GroupDropDownList>")]
     public class GroupDropDownList : System.Web.UI.WebControls.DropDownList
     {
        /// <summary>
        /// The field in the datasource which provides values for groups
        /// </summary>
        [DefaultValue(""), Category("Data")]
        public virtual string DataGroupField
        {
           get
           {
              object obj = this.ViewState["DataGroupField"];
              if (obj != null)
              {
                 return (string) obj;
              }
              return string.Empty;
           }
           set
           {
              this.ViewState["DataGroupField"] = value;
           }
        }
   
 
        /// <summary>
        /// Render this control to the output parameter specified.
        /// Based on the source code of the original DropDownList method
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
           ListItemCollection items = this.Items;
           int itemCount = this.Items.Count;
           string curGroup = String.Empty;
           string itemGroup;
           bool bSelected = false;

         if (itemCount <= 0)
         {
            return;
         }

         for (int i = 0; i < itemCount; i++)
         {
            ListItem item = items[i];
            itemGroup = (string)item.Attributes["DataGroupField"];
            if (itemGroup != null && itemGroup != curGroup)
            {
               if (curGroup != String.Empty)
               {
                  writer.WriteEndTag("optgroup");
                  writer.WriteLine();
               }

               curGroup = itemGroup;
               writer.WriteBeginTag("optgroup");
               writer.WriteAttribute("label", curGroup, true);
               writer.Write('>');
               writer.WriteLine();
            }

            writer.WriteBeginTag("option");
            if (item.Selected)
            {
               if (bSelected)
               {
                  throw new HttpException("Cant_Multiselect_In_DropDownList");
               }
               bSelected = true;
               writer.WriteAttribute("selected", "selected", false);
            }
            writer.WriteAttribute("value", item.Value, true);
            writer.Write('>');
            HttpUtility.HtmlEncode(item.Text, writer);
            writer.WriteEndTag("option");
            writer.WriteLine();
         }
         if (curGroup != String.Empty)
         {
            writer.WriteEndTag("optgroup");
            writer.WriteLine();
         }
      }

      /// <summary>
      /// Perform data binding logic that is associated with the control
      /// </summary>
      /// <param name="e">An EventArgs object that contains the event data</param>
      protected override void OnDataBinding(EventArgs e)
      {
         // Call base method to bind data
         base.OnDataBinding(e);

         if (this.DataGroupField == String.Empty)
         {
            return;
         }

         // For each Item add the attribute "DataGroupField" with value from the datasource
         IEnumerable dataSource = GetResolvedDataSource(this.DataSource, this.DataMember);
         if (dataSource != null)
         {
            ListItemCollection items = this.Items;
            int i = 0;

            string groupField = this.DataGroupField;
            foreach (object obj in dataSource)
            {
               string groupFieldValue = DataBinder.GetPropertyValue(obj, groupField, null);
               ListItem item = items[i];
               item.Attributes.Add("DataGroupField", groupFieldValue);
               i++;
            }
         }

      }

      /// <summary>
      /// This is copy of the internal ListControl method
      /// </summary>
      /// <param name="dataSource"></param>
      /// <param name="dataMember"></param>
      /// <returns></returns>
      private IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
      {
         if (dataSource != null)
         {
            IListSource source1 = dataSource as IListSource;
            if (source1 != null)
            {
               IList list1 = source1.GetList();
               if (!source1.ContainsListCollection)
               {
                  return list1;
               }
               if ((list1 != null) && (list1 is ITypedList))
               {
                  ITypedList list2 = (ITypedList) list1;
                  PropertyDescriptorCollection collection1 = list2.GetItemProperties(new PropertyDescriptor[0]);
                  if ((collection1 == null) || (collection1.Count == 0))
                  {
                     throw new HttpException("ListSource_Without_DataMembers");
                  }
                  PropertyDescriptor descriptor1 = null;
                  if ((dataMember == null) || (dataMember.Length == 0))
                  {
                     descriptor1 = collection1[0];
                  }
                  else
                  {
                     descriptor1 = collection1.Find(dataMember, true);
                  }
                  if (descriptor1 != null)
                  {
                     object obj1 = list1[0];
                     object obj2 = descriptor1.GetValue(obj1);
                     if ((obj2 != null) && (obj2 is IEnumerable))
                     {
                        return (IEnumerable) obj2;
                     }
                  }
                  throw new HttpException("ListSource_Missing_DataMember");
               }
            }
            if (dataSource is IEnumerable)
            {
               return (IEnumerable) dataSource;
            }
         }
         return null;
      }

   }


}


То что перевел:
Код: Выделить всё
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel

<ToolboxData("<{0}:GroupDropDownList runat=server></{0}:GroupDropDownList>")> Public Class DropDownListEx : Inherits DropDownList

   <DefaultValue(""), Category("Data")> Property DataGroupField() As String
      Get
         Dim _Obj As Object = Me.ViewState("DataGroupField")
         If Not _Obj Is Nothing Then Return CType(_Obj, String)
         Return String.Empty
      End Get
      Set(ByVal value As String)
         Me.ViewState("DataGroupField") = value
      End Set
   End Property

   Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
      Dim _ItemsCount As Integer = Me.Items.Count
      Dim _CurGroup As String = String.Empty
      Dim _ItemGroup As String = ""
      Dim _Selected As Boolean = False
      If _ItemsCount < 1 Then Return
      For Each _Item As ListItem In Me.Items
         _ItemGroup = CType(_Item.Attributes("DataGroupField"), String)
         If Not _ItemGroup Is Nothing And _ItemGroup <> _CurGroup Then
            If _CurGroup <> String.Empty Then
               writer.WriteEndTag("optgroup")
               writer.WriteLine()
            End If
            _CurGroup = _ItemGroup
            writer.WriteBeginTag("optgroup")
            writer.WriteAttribute("label", _CurGroup, True)
            writer.Write(">")
            writer.WriteLine()
         End If
         writer.WriteBeginTag("option")
         If _Item.Selected Then
            If _Selected Then Throw New HttpException("Cant_Multiselect_In_DropDownList")
            _Selected = True
            writer.WriteAttribute("selected", "selected", False)
         End If
         writer.WriteAttribute("value", _Item.Value, True)
         writer.Write(">")
         HttpUtility.HtmlEncode(_Item.Text, writer)
         writer.WriteEndTag("option")
         writer.WriteLine()
      Next
      If _CurGroup <> String.Empty Then
         writer.WriteEndTag("optgroup")
         writer.WriteLine()
      End If
   End Sub

End Class
Слава роботам! Убить всех человеков! Bite my shiny metal ass!

Roman Koff
Постоялец
Постоялец
Аватара пользователя
 
Сообщения: 495
Зарегистрирован: 17.09.2008 (Ср) 9:22
Откуда: От туда

Re: Поможите перевести контрол на VB

Сообщение Roman Koff » 27.10.2008 (Пн) 15:19

Конкретно споткнулся на функции
Код: Выделить всё
private IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
      {
         if (dataSource != null)
         {
            IListSource source1 = dataSource as IListSource;
            if (source1 != null)
            {
               IList list1 = source1.GetList();
               if (!source1.ContainsListCollection)
               {
                  return list1;
               }
               if ((list1 != null) && (list1 is ITypedList))
               {
                  ITypedList list2 = (ITypedList) list1;
                  PropertyDescriptorCollection collection1 = list2.GetItemProperties(new PropertyDescriptor[0]);
                  if ((collection1 == null) || (collection1.Count == 0))
                  {
                     throw new HttpException("ListSource_Without_DataMembers");
                  }
                  PropertyDescriptor descriptor1 = null;
                  if ((dataMember == null) || (dataMember.Length == 0))
                  {
                     descriptor1 = collection1[0];
                  }
                  else
                  {
                     descriptor1 = collection1.Find(dataMember, true);
                  }
                  if (descriptor1 != null)
                  {
                     object obj1 = list1[0];
                     object obj2 = descriptor1.GetValue(obj1);
                     if ((obj2 != null) && (obj2 is IEnumerable))
                     {
                        return (IEnumerable) obj2;
                     }
                  }
                  throw new HttpException("ListSource_Missing_DataMember");
               }
            }
            if (dataSource is IEnumerable)
            {
               return (IEnumerable) dataSource;
            }
         }
         return null;
      }
Слава роботам! Убить всех человеков! Bite my shiny metal ass!

Nord777
Гуру
Гуру
Аватара пользователя
 
Сообщения: 1144
Зарегистрирован: 22.02.2004 (Вс) 13:15
Откуда: Подольск

Re: Поможите перевести контрол на VB

Сообщение Nord777 » 27.10.2008 (Пн) 17:31

Microsoft Visual Studio 2008
Microsoft .NET Framework 3.5


Вернуться в C#

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 1

    TopList