Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 23-Dec-12 20:57pm
Comments
Sergey Alexandrovich Kryukov at 24-Dec-12 3:54am
   
Any better? Compared to what? Maybe, show some of your code. What's the problem?
—SA
meetarun007 at 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);
}
zaphoed at 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 at 30-Jan-13 1:17am
   
Thanks for your feedback.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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:
 
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
 
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);
        }));
}
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100