I have generated 8*16 ovalshape's in a form. The code is:
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 8; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Transparent;
ovl.Name = "oval" + j + "" + i;
ovl.Location = new Point((ovl.Width * i) * 2, (ovl.Height * j) * 2);
ovalShape.Add(ovl);
}
}
foreach (OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer =
new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
os.Parent = shapeContainer;
this.Controls.Add(shapeContainer);
}
Now I want access to each ovalshape differently. How could I do this?
Since ovalShape is a List<OvalShape>, you can use the indexer to access any one item:
var anOval = ovalShape[0];
You are already accessing each ovalshape in ovalShape differently in your foreach loop
foreach (OvalShape os in ovalShape)
{
//...
}
Otherwise you can also access each ovalshape by it's index, as
var newOvalShape = ovalShape[0];
You have already named your controle like ovl.Name = "oval" + j + "" + i;
So , I think you can create dictrionary like Dictionary<string , OvalShape> dic
Then you can set it like
//...
ovl.Name = "oval" + j + "" + i;
dic.add(ovl.Name , ovl);
//...
Then , you can access this dictionary in other methods , and access it by its name.
Related
Suppose a have a MxNx3 matrix
byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];
How do I access a single band of it (for read and write purposes)? Something like:
singleBand = myMatrix[:allRows: , :allCols: , :desiredBand:];
On the left is what I have, of the right is what I want to access (for example).
int M=10;
int N=20;
var test = new byte[3][,] { new byte[N,M],new byte[N,M],new byte[N,M]};
var band1 = test[1]; //its green
band1[2, 2] = 99;
If you can't change the type of myMatrix, then you can use the following code:
byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];
var singleBand = new byte[sizeRow, sizeCol];
var band = 1;
for (var i = 0; i < sizeRow; i++) {
for (var j = 0; j < sizeCol; j++) {
singleBand[i, j] = myMatrix[i, j, band];
}
}
But if you can change it, then probably Zeromus' solution is better, since you can more easily manipulate with what you call the band.
You simply need to loop through your required array elements and extract the "band" that you need.
// Create a three-dimensional array.
int[, ,] threeDimensional = new int[3, 3, 3];
// Set the first "Band" to 9
threeDimensional[0,0,1] = 9;
threeDimensional[1,0,1] = 9;
threeDimensional[2,0,1] = 9;
threeDimensional[0,1,1] = 9;
threeDimensional[1,1,1] = 9;
threeDimensional[2,1,1] = 9;
threeDimensional[0,2,1] = 9;
threeDimensional[1,2,1] = 9;
threeDimensional[2,2,1] = 9;
// Loop over each dimension's length.
for (int i = 0; i < threeDimensional.GetLength(2); i++)
{
for (int y = 0; y < threeDimensional.GetLength(1); y++)
{
for (int x = 0; x < threeDimensional.GetLength(0); x++)
{
Console.Write(threeDimensional[x, y, i]);
}
Console.WriteLine();
}
Console.WriteLine();
}
Your only option is to access each "cell" in the band one at a time and extract it to some other location. In my case i just put them out to the display.
The user basically enters a number of hex values into a textbox separated by commas eg. AA,1B,FF. These are then displayed in a listbox box. if the number of hex values in the textbox exceeds the size to transfer defined by the user, the listbox only displays the this number of values or if the size to transfer is bigger that adds zero values to the listbox.
this works fine until you enter a value with a zero in front of it such as AA,BB,CC,DD,EE,0F, if sizeToTransfer = 2, the listbox should display 0xAA and 0xBB. but instead it only removes the 0F value?
I'm pretty new to programming so it may be something obvious I'm missing any help would be appreciated.
private void WriteSPI1_Click(object sender, EventArgs e)
{
string hexstring = textbox1.Text;
HexValues.Items.Clear();
string[] hexarray = hexstring.Split((",\r\n".ToCharArray()), StringSplitOptions.RemoveEmptyEntries);
byte[] hexbytes = new byte[hexarray.Length];
uint num = Convert.ToUInt32(hexarray.Length);
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
if (hexarray.Length > sizeToTransfer)
{
diff = num - sizeToTransfer;
for (i = 0; i < diff+1; i++)
{
HexValues.Items.Remove("0x" + x);
}
}
else
{
diff = sizeToTransfer - num;
for (i = 0; i < diff; i++)
{
HexValues.Items.Add("0x00");
}
}
}
CHANGE
for (int j = 0; j < sizeToTransfer; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
WITH
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
and remove the if stantment that follow
I have a problem. I'm making a web browser in a C# Windows Form, and I've added bookmarks. My problem? Removing them. I can't seem to get good code that will remove the bookmarks! Here's my code:
private void button2_Click_1(object sender, EventArgs e)
{
int removeBookmarkI = 0;
string removeBookmarkName = Microsoft.VisualBasic.Interaction.InputBox("Which bookmark would you like to delete?", "Delete Bookmark", "Google");
for (int i = 0; i < bookmark_names.Length; i++)
{
if (bookmark_names[i] == removeBookmarkName)
{
removeBookmarkI = i;
string[] holdBookmark_names_temp = new string[bookmark_names.Length];
string[] holdBookmark_url_temp = new string[bookmark_names.Length];
for (int j = i; j < bookmark_names.Length - 1; j++)
{
bookmark_names[j] = bookmark_names[j + 1];
bookmark_url[j] = bookmark_url[j + 1];
}
int skipNum = 0;
for (int j = 0; j < bookmark_names.Length - 1; j++)
{
if (bookmark_names[j] == removeBookmarkName)
{
skipNum = 1;
}
holdBookmark_names_temp[j] = bookmark_names[j + skipNum];
holdBookmark_url_temp[j] = bookmark_url[j + skipNum];
}
bookmark_names = new string[bookmark_names.Length - 1];
bookmark_url = new string[bookmark_url.Length - 1];
for (int j = 0; j < bookmark_names.Length; j++)
{
bookmark_names[j] = holdBookmark_names_temp[j];
bookmark_url[j] = holdBookmark_url_temp[j];
}
this.Controls.Remove(buttonsAdded[removeBookmarkI]);
buttonsAdded.Remove(buttonsAdded[bookmark_names.Length - 1]);
this.Controls.Remove(buttonsAdded[bookmark_names.Length - 1]);
System.IO.File.WriteAllLines("C:\\Users\\Public\\IAmAPerson_Web_Explorer\\bookmarks\\bookmark_names.txt", bookmark_names);
System.IO.File.WriteAllLines("C:\\Users\\Public\\IAmAPerson_Web_Explorer\\bookmarks\\bookmarks.txt", bookmark_url);
bookmark_names = new string[bookmark_names.Length - 1];
bookmark_url = new string[bookmark_url.Length - 1];
bookmark_names = System.IO.File.ReadAllLines(#"C:\Users\Public\IAmAPerson_Web_Explorer\bookmarks\bookmark_names.txt");
bookmark_url = System.IO.File.ReadAllLines(#"C:\Users\Public\IAmAPerson_Web_Explorer\bookmarks\bookmarks.txt");
bookmarkButtonSpacing -= 100;
break;
}
if (i == bookmark_names.Length - 1)
{
System.Windows.Forms.MessageBox.Show("You do not have a bookmark named " + removeBookmarkName + "!");
}
}
}
It should prompt for what bookmark you want to delete, remove it, shift all other bookmarks over, and remove it from the web explorer directory files. What's going on? How can I fix this? I'm using a list for my buttons called buttonsAdded. Also, I am aware that this code is probably the worst looking code in C# EVER, but I'm new, and I have yet to learn the best ways to code.
I was wondering how could I select objects which were created during programs runtime.
Each object has its unique name. How could I select that object by its name?
Example names:
"mapPart_0_0"
"mapPart_0_1"
"mapPart_0_2"
etc.
It's a windows form project. In c#.
Creation of those objects:
private void addBoxes()
{
for (int a = 0; a < 25; a++)
{
for (int b = 0; b < 10; b++)
{
MyCustomPictureBox box = new MyCustomPictureBox();
box.Location = new Point(b * 23 + 5, a * 23 + 5);
box.Image = new System.Drawing.Bitmap("tiles/0.png");
box.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
box.Size = new Size(24, 24);
box.Name = "mapPart_" + a + "_" + b;
box.Click += new EventHandler(boxClickAdd);
box.oFile = "0";
panel1.Controls.Add(box);
}
}
}
I would suggest to simply put the objects in a System.Collections.Generic.Dictionary<string, your object type> list. It provides the exact functionality you are seeking if I understand the question correctly.
im tunning this loop and what to populate the array with the output of my methods, im not sure about that last part "array2DB[i,i] =" how shold i do this.
updated loop based on replyes
private void BackGroundLoop()
{
for (int i = 1; i < 31; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}
I'm not 100% sure what you want to do, but you want probably this instead of your last line:
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
-1 in index is needed, because arrays are indexed from 0 in .NET.
This is the most you can do, without running into exception:
int[,] array2DB = new int[2, 30];
for (int i = 0; i < 30; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0,i] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1,i] = int.Parse(UpdateFormClass. nyeSaker.ToString());
}
Note that you can't have array2DB[2, *] or above because it will generate an arrayoutofbound exception.
You have to use two for loops. One for each the x and y axis of the array.
for (int i = 0; i < 2; i++){
for (int j = 0; j < 30; j++)
{
....
array2DB[i,j] = int.Parse(UpdateFormClass.gamleSaker.ToString())
, int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}