Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to create a array of images based on user inputs for no. of rows and no. of colums
For example if user gives 2x3, is should show,

ImageAt1x1 ImageAt1x2 ImageAt1x3
ImageAt2x1 ImageAt2x2 ImageAt23


Ref Image: http://s7.postimage.org/oypg8660b/stov.png[^]

Now i am creating buttons programatically and setting the image property.
But it is pain as it has bunch of C# code to write.
Is there any better way to achieve this??
Note: On mouse hover on each item in image array it has to show its number in array like 2x1.
Posted
Comments
Sergey Alexandrovich Kryukov 24-Dec-12 3:54am    
Any better? Compared to what? Maybe, show some of your code. What's the problem?
—SA
meetarun007 24-Dec-12 5:06am    
This is what i ve used...

int numberOdRows = Int32.Parse(txtRows.Text);
int numberOdColumns = Int32.Parse(txtColums.Text);


G2.Children.Clear();


for (int i = 1; i <= numberOdRows; i++)
{
StackPanel _stack1 = new StackPanel { Orientation = Orientation.Horizontal };
for (int j = 1; j <= numberOdColumns; j++)
{
Image finalImage = new Image();
finalImage.Width = 25;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(@"c:\users\Projects\Images\Untitled.png");
logo.EndInit();
finalImage.Source = logo;
finalImage.ToolTip = "(" + i.ToString() + "," + j.ToString() + ")";
_stack1.Children.Add(finalImage);
}

G2.Children.Add(_stack1);
}
saephoed 28-Jan-13 7:45am    
you're code looks good already. Nevertheless, in case you still need an answer, you might have a look at some new alternatives i added
meetarun007 30-Jan-13 1:17am    
Thanks for your feedback.

1 solution

But it is pain as it has bunch of C# code to write. Is there any better way to achieve this??

Hello meetarun007
your code looks nice already. I don't fully understand what exactly you mean with better way, so here are three different approaches:

I need to create a array of images

interpreting that as 1 dimensional array and as targeting less code to write, it is possible to use a flow view which reduces the original 20 lines of code to 14:

C#
void flowSample()
{
    const double fixedImageWidth = 25;
    int numberOfRows = Int32.Parse(txtRows.Text);
    int numberOfColumns = Int32.Parse(txtColums.Text);

    wrapPanel1.Children.Clear();
    wrapPanel1.Width = fixedImageWidth * numberOfColumns;

    BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
    for (int i = 0; i < numberOfRows*numberOfColumns; i++)
    {
        Image finalImage = new Image();
        finalImage.Width = fixedImageWidth;
        finalImage.Source = logo;
        finalImage.ToolTip = string.Format("({0},{1})", i % numberOfColumns + 1, i / numberOfColumns + 1);

        wrapPanel1.Children.Add(finalImage);
    }
}


a grid is also possible. Although there's more code to write for that case, it reflects the two dimensioness more clearly:

C#
void gridSample()
{
    const double fixedImageWidth = 25;
    int numberOfRows = Int32.Parse(txtRows.Text);
    int numberOfColumns = Int32.Parse(txtColums.Text);

    grid1.RowDefinitions.Clear();
    grid1.ColumnDefinitions.Clear();

    for (int r = 1; r <= numberOfRows; r++)
    {
        RowDefinition row = new RowDefinition();
        row.Height = GridLength.Auto;
        grid1.RowDefinitions.Add(row);
    }
    for (int c = 1; c <= numberOfColumns; c++)
    {
        ColumnDefinition col = new ColumnDefinition();
        col.Width = GridLength.Auto;
        grid1.ColumnDefinitions.Add(col);
    }

    BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
    for (int r = 1; r <= numberOfRows; r++)
        for (int c = 1; c <= numberOfColumns; c++)
        {
            Image finalImage = new Image();
            finalImage.Width = fixedImageWidth;
            finalImage.Source = logo;
            finalImage.ToolTip = string.Format("({0},{1})", r, c);

            Grid.SetRow(finalImage, r - 1);
            Grid.SetColumn(finalImage, c - 1);
            grid1.Children.Add(finalImage);
        }
}


it can also be approached a little more generic so it can be reused more easily

C#
abstract class dim
{
    public int nof;
    public abstract void Init();
    public Action<UIElement, int, int> setPosition;
}
class dim<T> : dim where T : DefinitionBase, new()
{
    public IList<T> defs;
    public Action<T, GridLength> setSpacing;
    public override void Init()
    {
        defs.Clear();
        for (int i = 1; i <= nof; i++)
        {
            T d = new T();
            setSpacing(d, GridLength.Auto);
            defs.Add(d);
        }
    }
}
void gridSample2()
{
    const double fixedImageWidth = 25;
    List<dim> dims = new List<dim> {
        new dim<RowDefinition>    { nof = Int32.Parse(txtRows.Text),   defs = grid2.RowDefinitions,    setSpacing = new Action<RowDefinition,    GridLength>((d, s) => d.Height = s), setPosition = new Action<UIElement, int, int>((e, d0, d1) => Grid.SetRow(e, d0)) }, 
        new dim<ColumnDefinition> { nof = Int32.Parse(txtColums.Text), defs = grid2.ColumnDefinitions, setSpacing = new Action<ColumnDefinition, GridLength>((d, s) => d.Width = s),  setPosition = new Action<UIElement, int, int>((e, d0, d1) => Grid.SetColumn(e, d1)) } };

    dims.ForEach(d => d.Init());

    BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
    Enumerable.Range(0, dims[0].nof).ToList().ForEach(d0 => Enumerable.Range(0, dims[1].nof).ToList().ForEach(d1 =>
        {
            Image finalImage = new Image();
            finalImage.Width = fixedImageWidth;
            finalImage.Source = logo;
            finalImage.ToolTip = string.Format("({0},{1})", d0+1, d1+1);

            dims.ForEach(d => d.setPosition(finalImage, d0, d1));

            grid2.Children.Add(finalImage);
        }));
}
 
Share this answer
 
v3

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