Getting name for a WPF shape from variable - c#

I want to initialize and show a ellipse (WPF shape) in a function.The name of the ellipse should be given to the function as a parameter.
Is there a possibility to do something like that ?
Edit:
The following is given:
private void A1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Feld A1 gedrückt";
//Spielstein setzen
//Rêgeln überprüfen
myEllipse = new Ellipse();
myEllipse.Fill = System.Windows.Media.Brushes.Black;
myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
myEllipse.VerticalAlignment = VerticalAlignment.Top;
myEllipse.Margin = new Thickness(2, 2, 0, 0);
myEllipse.Width = 45;
myEllipse.Height = 45;
grid3.Children.Add(myEllipse);
A1.IsHitTestVisible = false;
}
What I want to do is to get the name("myEllipse") from a string variable. For example:
string name = 'myEllipse';
name = newEllipse();
myEllipse.Fill = System.Windows.Media.Brushes.Black;

I take it from your code you are you trying to create multiple instances of Ellipse which you can then recall and modify when needed? If so, one way to do this is to create a List or Dictionary and add the ellipses you create to them, which you can then recall when needed through the index (if it's a list) or key (if it's a dictionary).
List<Ellipse> myEllipses = new List<Ellipse>();
Private void A1_Click(object sender, RoutedEventArgs e)
{
var myEllipse = new Ellipse();
myEllipses.Add(myEllipse);
...
}

Related

create multiple rectangle dynamically in windows phone

I have created a canvas and I want to let user create rectangle/s on the screen and then user should be able to manipulate it.
I have written the following code -
private TranslateTransform move = new TranslateTransform();
private ScaleTransform resize = new ScaleTransform();
private TransformGroup rectangleTransforms = new TransformGroup();
private Brush stationaryBrush;
private Brush transformingBrush = new SolidColorBrush(Colors.Orange);
private void Button_Click(object sender, RoutedEventArgs e)
{
rect = new Rectangle();
rect.Height = 100;
rect.Width = 100;
SolidColorBrush myBrush = new SolidColorBrush(Colors.Orange);
rect.Fill = myBrush;
LayoutRoot.Children.Add(rect);
rectangleTransforms.Children.Add(move);
rectangleTransforms.Children.Add(resize);
rect.RenderTransform = rectangleTransforms;
// Handle manipulation events.
rect.ManipulationStarted +=
new EventHandler<ManipulationStartedEventArgs>(Rectangle_ManipulationStarted);
rect.ManipulationDelta +=
new EventHandler<ManipulationDeltaEventArgs>(Rectangle_ManipulationDelta);
rect.ManipulationCompleted +=
new EventHandler<ManipulationCompletedEventArgs>(Rectangle_ManipulationCompleted);
}
void Rectangle_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
rect.Fill = stationaryBrush;
}
void Rectangle_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
stationaryBrush = rect.Fill;
rect.Fill = transformingBrush;
}
void Rectangle_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
move.X += e.DeltaManipulation.Translation.X;
move.Y += e.DeltaManipulation.Translation.Y;
if (e.DeltaManipulation.Scale.X > 0 && e.DeltaManipulation.Scale.Y > 0)
{
resize.ScaleX *= e.DeltaManipulation.Scale.X;
resize.ScaleY *= e.DeltaManipulation.Scale.Y;
}
}
I copied this code from MSDN Library.
I have declared a rectangle object(rect) above.
Now my problem is this code is working fine for one rectangle but I want to give use an option to add another or multiple rectangles.
1.is it possible to create multiple rectangle with same button click event and let the user manipulate each rectangle create or something where he can adjust the rectangle atleast one time after creating it and not afterwards.
Any Help is appreciated.
You'd just have to get the Rectangle instance from the "sender" in the event handler, rather than storing a local copy:
void Rectangle_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
var rect = (Rectangle)sender;
rect.Fill = stationaryBrush;
}
The same applies to "move" -- don't use a local variable, but create a new instance in each button click event:
var rectangleTransform = new TransformGroup();
rectangleTransforms.Children.Add(new TranslateTransform());
rectangleTransforms.Children.Add(new ScaleTransform());
rect.RenderTransform = rectangleTransforms;
You can retrieve it in the "ManipulationDelta" handler by casting the RenderTransform property back to the TransformGroup type:
void Rectangle_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var rect = (Rectangle)sender;
var move = (TransformGroup)rect.RenderTransform;
// etc

C# Get text from a textbox that is made at runtime

Hello I am making a program that has 2 textboxes and 2 buttons
When I press the add button then it will make 2 new textboxes using this code :
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX_1 font
NEW_TEXTBOX_1.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_1.Font = new Font(NEW_TEXTBOX_1.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_1.Location = new System.Drawing.Point(16, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_1.Size = new System.Drawing.Size(178, 29);
this.Controls.Add(NEW_TEXTBOX_1);
//Make the PRICE_TEXTBOX_
TextBox NEW_TEXTBOX_2 = new TextBox();
NEW_TEXTBOX_2.Name = "PRICE_TEXTBOX_" + HOW_FAR.ToString();
//Set NEW_TEXTBOX font
NEW_TEXTBOX_2.Font = new Font("Segoe Print", 9);
NEW_TEXTBOX_2.Font = new Font(NEW_TEXTBOX_2.Font, FontStyle.Bold);
//Set pos and size and then create it.
NEW_TEXTBOX_2.Location = new System.Drawing.Point(200, 71 + (35 * HOW_FAR));
NEW_TEXTBOX_2.Size = new System.Drawing.Size(89, 29);
this.Controls.Add(NEW_TEXTBOX_2);
//Change pos of the add button
ADD_ROW.Location = new System.Drawing.Point(295, 71 + (35 * HOW_FAR));
this.Height = 349 + (35 * HOW_FAR);
this.Width = 352;
}
This works very well but now I want to get the text from a newly made textbox back how do I do this?
This doesn't work because it says : NAME_TEXTBOX_1 Does not exist in the current context.
private void button2_Click(object sender, EventArgs e)
{
string tmpStr = NAME_TEXTBOX_1.Text;
}
You need to move the variable declaration outside of the ADD_ROW_Click event handler so that it's accessible outside that block;
TextBox NEW_TEXTBOX_1;
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
NEW_TEXTBOX_1 = new TextBox(); //remove "TextBox" since we declared it above
NEW_TEXTBOX_1.Name = "NAME_TEXTBOX_" + HOW_FAR.ToString();
//...
The alternative, and possibly better depending on the number of textboxes, is to add each TextBox you create into a List. You can then iterate that List from and find the TextBox you want. For example
List<TextBox> allTextBoxes = new List<TextBox>();
private void ADD_ROW_Click(object sender, EventArgs e)
{
//Make the NEW_TEXTBOX_1
HOW_FAR += 1;
TextBox NEW_TEXTBOX_1 = new TextBox();
//...fill out the properties
//add an identifier
NEW_TEXTBOX_1.Tag = 1;
allTextBoxes.Add(NEW_TEXTBOX_1);
}
Then when you want a particular TextBox
private void button2_Click(object sender, EventArgs e)
{
TextBox textBox1 = allTextBoxes.Where(x => x.Tag == 1).FirstOrDefault();
string tmpStr = "";
if(textBox1 != null)
tmpStr = textBox1.Text;
}
Alternatively, and especially if you're going to have a lot of TextBoxes, you could store them in a Dictionary as Corak suggested in the comments.
you're declaring NAME_TEXTBOX_1 within the ADD_ROW_Click method, which is why it isn't available within the button2_Cick method.
You can declare the textbox at the class level to access it in both places.
(You should work on renaming your variables too - e.g. TextBoxPrice)
One simple solution:
Make a private field called "NEW_TB" for example.
In your button2_Click(..) { string tmpStr = NEW_TB.Text; }
Add in your ADD_ROW_Click(..) method NEW_TB = NAME_TEXTBOX_1;
If I understood your question right, this should work.
Make global your textboxes:
TextBox NEW_TEXTBOX_1;
then initiate them in your method:
NEW_TEXTBOX_1 = new TextBox();
OMG Never mind sorry guys I found a good way :D
var text = (TextBox)this.Controls.Find("PRICE_TEXTBOX_1", true)[0];
text.Text = "PRO!";
This works pretty well :)

PictureBoxSizeMode is error?

Getting error when its casting in Listview:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int a = 1;
string theimage = textBox1.Text + #"\allimages\";
foreach (ListViewItem item in listView1.SelectedItems)
{
// 39 zero's + "1"
string initValue = new String('0', 3) + "0";
// convert to int and add 1
int newValue = Int32.Parse(initValue) + a;
// convert back to string with leading zero's
string newValueString = newValue.ToString().PadLeft(4, '0');
string imageslist = "product" + newValueString + "img";
string[] images = Directory.GetFiles(theimage, imageslist + "*.jpg");
// Cast the Picturebox
PictureBox myPicBox = new PictureBox();
myPicBox.Location = new Point(7, 240);
myPicBox.Size = new System.Drawing.Size(140, 140);
myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
myPicBox.Margin = new Padding(3,3,3,3);
myPicBox.Visible = true;
myPicBox.Image = new Bitmap(images[1]);
Controls.Add(myPicBox);
System.Diagnostics.Debugger.Break();
//List<PictureBox> pictureBoxList = new List<PictureBox>();
}
}
Its my error:
Error 1 'test.Form1.PictureBoxSizeMode()' is a 'method', which is not valid in the given context C:\Users\radiaku\Documents\Visual Studio 2008\Projects\test\test\Form1.cs 428 37 test
the code above working fine when I using button_click handler..
It sounds like your form has a method called PictureBoxSizeMode. You could either change the method name, or change the property setter to:
myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
Changing the method name would be cleaner though.
That error can only occur if you have a method named PictureBoxSizeMode in your code. You should rename that method to something else. A scenario like:
private void UserInput_KeyPress(object sender, KeyPressEventArgs e)
{
PictureBox myPicBox = new PictureBox();
myPicBox.Location = new Point(7, 240);
myPicBox.Size = new System.Drawing.Size(140, 140);
myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
myPicBox.Margin = new Padding(3, 3, 3, 3);
myPicBox.Visible = true;
}
private void PictureBoxSizeMode()
{
}
Or qualify it with the Namespace like.
myPicBox.SizeMode = myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

How to set values in x axis MSChart using C#

I have these XY values:
Series S1 = new Series()
S1.Points.AddXY(9, 25);
S1.Points.AddXY(10, 35);
S1.Points.AddXY(11, 15);
chart1.Series.Add(S1);
but I need to show the X values in the graph like this:
X="9-10"
X="10-11"
X="11-12"
How can I achieve that?
So far this is what I've found:
and here is the code:
private void Form1_Shown(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Minimum = 7;
chart1.ChartAreas[0].AxisX.Maximum = 15;
Series S1 = new Series();
S1.Points.AddXY(9, 25);
S1.Points.AddXY(10, 35);
S1.Points.AddXY(11, 15);
chart1.Series.Add(S1);
chart1.Series[0].Points[0].AxisLabel = "9-10";
chart1.Series[0].Points[1].AxisLabel = "10-11";
chart1.Series[0].Points[2].AxisLabel = "11-12";
as you can see I work with numbers, and set texts for the X axis labels, but I can do that just for the DataPoints values, I need it for the whole range of values.
Any ideas please?
Here is the answer thanks to sipla:
working with Custom labels and the Customize event:
string[] range = new string[10];
private void Form1_Shown(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Minimum = 7;
chart1.ChartAreas[0].AxisX.Maximum = 16;
range[0] = "";
range[1] = "7-8";
range[2] = "8-9";
range[3] = "9-10";
range[4] = "10-11";
range[5] = "11-12";
range[6] = "12-1";
range[7] = "1-2";
range[8] = "2-3";
range[9] = "";
Series S1 = new Series();
S1.Points.AddXY(9, 25);
S1.Points.AddXY(10, 35);
S1.Points.AddXY(11, 15);
chart1.Series.Add(S1);
}
int count;
private void chart1_Customize(object sender, EventArgs e)
{
count = 0;
foreach (CustomLabel lbl in chart1.ChartAreas[0].AxisX.CustomLabels)
{
lbl.Text = range[count];
count++;
}
}
Curious as to why your range array was sprawled out like that. It would have been cleaner to put your array in brackets as it was defined and also initialized. e.g.
string[] range = new string[10] {"","7-8","8-9","9-10","10-11","11-12","12-1","1-2","2-3",""};
/*
The tenth element is also likely unnecessary
as it simply repeats the first
element of the array
*/

Using wpf to animate ellipses consequently

I have to implement a road junction simple program. The junction's image is set as the Background Property of WPF Grid and I have ArrayLists inside a Queue to represent the color of each car, origin street and destination street.
Now, I need to animate the cars as moving ellipses and I need each car to start its movement after the privious car gets out of the screen.
I am using the following code but it only animates first car.
What is the solution?
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < queue.Count; i++)
{
ArrayList car = (ArrayList)queue[i];
int id = Convert.ToInt32(car[0]);
int color = Convert.ToInt32(car[1]);
int from= Convert.ToInt32(car[2]);
int to = Convert.ToInt32(car[3]);
Ellipse myEllipse = new Ellipse();
if (color == 0)
{
myEllipse.Stroke = System.Windows.Media.Brushes.Green;
myEllipse.Fill = System.Windows.Media.Brushes.Green;
}
else {
myEllipse.Stroke = System.Windows.Media.Brushes.Blue;
myEllipse.Fill = System.Windows.Media.Brushes.Blue;
}
myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
myEllipse.VerticalAlignment = VerticalAlignment.Center;
myEllipse.Width = 45;
myEllipse.Height = 45;
myGrid.Children.Add(myEllipse);
DoubleAnimation da = new DoubleAnimation();
da.From = from;
da.To = to;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
TranslateTransform tt = new TranslateTransform();
myEllipse.RenderTransform = tt;
tt.BeginAnimation(TranslateTransform.XProperty, da);
}
}
In WPF animation is is organized in other way.
I would suggest you to look at Storyboard. Hope this will help you.

Categories