What's the deal, i have an imagePanel (u can look at it as a picturePanel)
called imagePanel1 (i imported it so i don't have to make scroll Bars :))
and i have a treeView on the left side, from witch i can drag a node, and drop it over the imagePanel, where i get a Location of the drop, and on that location i create a normal panel called panel1
And so i do 100 times, so at the end i ll have an imagePanel full of small panels...
Now is the problem, when i click on the imagePanel (where a panel is located)
I want that panel to be selected on MousePress, and moved on mouseMove, and eventualLy deleted on a btnDelete...
Here is the code for the imagePanel:
//***********************************************************************
private void imagePanel1_DragDrop_1(object sender, DragEventArgs e)
{
Type testTip = new TreeNode().GetType();
YLScsImage.ImagePanel dropPicturePanel = (YLScsImage.ImagePanel)sender;
TreeNode movedNode;
_mouseDownSelectedWindow = Rectangle.Empty;
if (e.Data.GetDataPresent(testtype))
{
movedNode= (TreeNode)e.Data.GetData(testType);
dropPicturePanel.Tag = movedNode.Tag;
movedNode.ImageIndex = 1;
movedNode.SelectedImageIndex = 1;
movedNode.ForeColor = Color.Gray;
//**************************************
//HERE IS THE CODE FOR THE CREATED PANEL
Panel panel1 = new Panel();
Point point1 = this.PointToClient(new Point(e.X - 278, e.Y - 19)); //the imagePanel1 is on the form at the point 278,19
panel1.AllowDrop = true;
panel1.Location = point1;
panel1.BackgroundImage = iltest.Images[0]; //nvm
panel1.Height = 16;
panel1.Width = 16;
imagePanel1.Controls.Add(panel1); //am adding it to the imagePanel1
//saving the locations of each panel
string path = #"C:\Users\Cosic\Desktop\ICR\TABELA3_Paneli.txt"; // path to file
if (!File.Exists(path))
File.Create(path);
if (panelBr == 0)
System.IO.File.WriteAllBytes(path, new byte[0]); //brise ceo text iz fajla
TextWriter sw = new StreamWriter(path, true);
sw.WriteLine(e.X + "; " + e.Y + "; " + panel1.Width + "; " + panel1.Height + ";");
sw.Close();
//am done with saving
panelBr++;//nvm
}
}
tell me if u need some more code...i got a lot of it ;)
and sorry for bad english, am not that good as I would like to be...
I solved the problem, like this:
panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
just write panel1.anyEventUWant += and 2 times tab button....
it generate automaticly a new function with a single code line in it
here is an example
void panel1_MouseUp (object sender, EventArgs e)
{
//throw new NotImplementedException();
}
U can access the panel like this: ((object)sender).
Related
My problem is in the picture:
How can i position automatically the next control (textbox in this sample), without a TableLayoutPanel?
Do you mean you want the TextBox move left/right based on the width of Label?
private void button2_Click(object sender, EventArgs e) {
int gap1 = textBox1.Left - label1.Right;
label1.AutoSize = true;
label1.Text = "long long long long long long long long";
textBox1.Left = label1.Right + gap1;
int gap2 = textBox1.Left - label1.Right;
label2.AutoSize = true;
label2.Text = "s";
textBox2.Left = label2.Right + gap2;
}
You firstly record the gap between the TextBox and Label, then set the AutoSize to true, followed by setting the new content of Label, finally you can move the TextBox accordingly.
Before:
After:
If you need to align multiple TextBox, or the width of TextBox as well, it would be more complicated, but you can follow the similar logic.
However, you have to write your own code but not doable in the Design view, as the Anchor of control is to the parent container but not the sibling control. Well, in Xcode on Mac you could do this, but AFAIK Visual Studio does not have this feature out of the box.
Here's a simple example of using a counter to track the number of controls created and compute the proper Y position:
private int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
int y = counter * 25;
Label lbl = new Label();
lbl.Text = "Label " + counter.ToString();
lbl.Location = new Point(5, y);
TextBox tb = new TextBox();
tb.Location = new Point(lbl.Bounds.Right + 5, y);
this.Controls.Add(lbl);
this.Controls.Add(tb);
}
I have form that programmatically add a panel in it;
For each task coming in to my program, I add a label and
progress bar to panel which have 30 pixel Y added to previous Y position.
But when panel get scrolls sometimes when want to scroll down,
positions multiplied and not in their exact position.
Remember, I checked and written Y positions to console and saw that Y position is ok, but panel does not show it correctly
I think problem is for panel draw method,
but don't know how to fix it.
Problem is that task 26 should come just after 25 but not in correct position, despite of console I've seen position is correct.
code to add controls:
private static void AddTaskControls(int taskId)
{
Label lbl = new Label();
lbl = new Label();
lbl.Name = "lbl" + taskId.ToString();
lbl.Text = "Task " + taskId.ToString() + ":";
lbl.Width = 57;
lbl.Height = 13;
lbl.Location = new Point(0, 25 + (taskId) * 30);
ProgressBar pr = new ProgressBar();
pr = new ProgressBar();
pr.Name = "pr" + taskId.ToString();
pr.Width = 180;
pr.Height = 23;
pr.Location = new Point(50, 20 + (taskId) * 30);
Label lbl2 = new Label();
lbl2.Name = "lbl" + taskId.ToString() + "List";
lbl2.Text = "Starting " + taskId.ToString() + "";
lbl2.Width = 200;
lbl2.Height = 13;
lbl2.Location = new Point(230, 25 + (taskId) * 30);
//Console.WriteLine(lbl2.Location.Y + "taskid:"+taskId);
if (panel1.InvokeRequired)
{
AddTaskControllsCallback t = new AddTaskControllsCallback(AddTaskControls);
panel1.BeginInvoke(t, new object[] { taskId });
}
else
{
panel1.Controls.Add(lbl);
panel1.Controls.Add(pr);
panel1.Controls.Add(lbl2);
pr.BringToFront();
}
}
This has nothing to do with threading. When adding controls the current scroll position is used to re-calculate the effective Location. Weird? Yes..
So the reason probably is that your Panel scrolls down while you are adding new controls. I don't see where it happens but here is a tiny test to demonstrate the problem:
Looks familiar, right?
I enforce the scrolling in the code but in your case the reason should be similar, as should be the fix..
private void button20_Click(object sender, EventArgs e)
{
// lets add a few buttons..
for (int i = 0; i < 20; i++)
{
Button btn = new Button();
btn.Top = i * 25;
btn.Parent = panel2;
}
// ..now let's enforce a scoll amount of 111 pixels:
panel2.AutoScrollPosition = new Point(panel2.AutoScrollPosition.X,
-panel2.AutoScrollPosition.Y + 111);
// ..and add a few more buttons..
for (int i = 20; i < 40; i++)
{
Button btn = new Button();
btn.Top = i * 25; // not good enough!
// btn.Top = i * 25 + panel2.AutoScrollPosition.Y; // this will fix the problem!
btn.Parent = panel2;
}
}
So your code needs to set the Locations like this:
lbl.Location = new Point(0, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y);
..
pr.Location = new Point(50, 20 + (taskId) * 30 + panel1.AutoScrollPosition.Y));
..
lbl2.Location = new Point(230, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y));
(Or you find out how the scrolling happens and prevent it.)
I've found that it is a lot easier to add a second panel inside the scrollable panel, add the controls to that second panel, and increase the size of the second panel. Any problems with scrolling position do not apply then.
So create a form, add a button, add a panel panel1 with AutoScroll = true;, and add another panel panel2 inside the first panel (with location 0, 0).
private void button1_Click(object sender, EventArgs e)
{
// lets add a few buttons..
for (int i = 0; i < 25; i++)
{
Button btn = new Button();
btn.Top = i * 25;
panel2.Controls.Add(btn); // Add to panel2 instead of panel1
}
// Recalculate size of the panel in the scrollable panel
panel2.Height = panel2.Controls.Cast<Control>().Max(x => x.Top + x.Size.Height);
panel2.Width = panel2.Controls.Cast<Control>().Max(x => x.Left + x.Size.Width);
}
I'm using WinForms. In My form i have a button and a panel. When i click on that button i want to slide the panel to the right. I'm having issues with the code. Currently I'm getting red error lines under
= panel2.Location.X + 1;
Error Message: Cannot implicitly convert type int to System.Drawing.Point
I was trying to move the panel with the similar approach i did by growing the panel. I provided that in my code. How can i move the panel?
private void btn_Right_Click(object sender, EventArgs e)
{
// Make Panel Grow
//while (panel1.Width < 690)
//{
// panel1.Width = panel1.Width + 1;
//}
while (panel2.Location.X < 690)
{
panel2.Location = panel2.Location.X + 1;
}
}
You get an error because you try to set the location with an integer. You will need a new point instance:
panel2.Location = new Point(panel2.Location.X, panel2.Location.Y + 1);
Try using .Left instead of .Location.X
This works in VB...
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If sender.text = ">" Then
Do Until Panel1.Left > Me.Width - 50
Panel1.Left += 1
Loop
sender.text = "<"
Else
Panel1.Left = 511
sender.text = ">"
End If
End Sub
I'm surprised it is a smooth as it is - panel is empty however.
I am adding two controls dynamically during runtime, however only the control that is made first is displayed.
Here is the code:
Label tempLab = new Label();
tempLab.text = "Test Label";
MyControl.Controls.Add(tempLab);
tempLab.Location = new Point(5,5);
Button tempBut = newButton()
tempBut.text = "Test Button";
MyControl.Controls.Add(tempBut);
tempBut.Location = new Point(20,20);
Isn't copypasta so ignore syntax errors with caps.
Any ideas ?
They are being added to a groupbox. I have tried adding them to a panel or just the form and the same issue occurs. I don't need event handlers, so please don't cite that requirement.
I quickly tried your code pasting it in a windows form constructor. It runs ok, but the label is slightly overlapping the button because of its size. You may want to autosize it:
Label tempLab = new Label();
tempLab.Text = "Test Label";
tempLab.AutoSize = true;
Controls.Add(tempLab);
tempLab.Location = new Point(5,5);
Button tempBut = new Button();
tempBut.Text = "Test Button";
Controls.Add(tempBut);
tempBut.Location = new Point(20,20);
Oh, by the way. You mentioned you are using MyControl as a Panel or a GroupBox. Please ensure that you are also adding MyControl to your Controls collection.
it appears that the location does not have a Size which becomes a flat line so to speak which is not visible.. this tempBut.Location = new Point(20,20); try changing to this
this.tempBut.Location = new System.Drawing.Point(20,20);
this.tempBut.Size = new System.Drawing.Size(30, 15);
hope this helps. I am adding a array of MyTextBox into panel.
Point prevlocation = new Point(0,0);
foreach (object key in keys) //List of Objects or which make new controls
{
MyTextBoxControlArray[i] = new MyTextBoxUserControl(key); //User control but could be any control like textbox etc
MyTextBoxControlArray[i].Width = this.panel1.Width - 50;
MyTextBoxControlArray[i].AutoSize = true;
MyTextBoxControlArray[i].InfoLoad += new MyTextBoxUserControl.InfoLoadEventHandler(Form1_InfoLoad);
if (i == 0)
{
//first control
prevlocation.Y += 3;
prevlocation.X += 3;
MyTextBoxControlArray[i].Location = prevlocation;
}
else
{
//adjsuting height and width
MyTextBoxControlArray[i].Location = new System.Drawing.Point(
prevlocation.X,
prevlocation.Y + MyTextBoxControlArray[i].Height+3);
}
prevlocation = MyTextBoxControlArray[i].Location;
i++;
}
this.panel1.Controls.AddRange(MyTextBoxControlArray); //in panel i can add a array of controls , but this could be done one by one
string sql3 = "SELECT COUNT(*) from systeminfo";//counting no of element
n = dm.countelement(sql3);
int i, c = 1;
int m = 100;
for (i = 0; i < n; i++, c++)
{
sql3 = " SELECT Company_name FROM systeminfo LIMIT " + (i + 1) + " OFFSET " + i + "";
string cname = dm.getlang(sql3);
PictureBox pb = new PictureBox();
Label lb = new Label();
pb.Location = new System.Drawing.Point(m, 30 + (30 * i));
lb.Location = new System.Drawing.Point(m-30, 30 + ((30 * i)-30));
pb.Name = "p" + c;
lb.Name = "l" + c;
lb.Size = new System.Drawing.Size(100, 20);
pb.Size = new System.Drawing.Size(30, 30);
lb.Text = cname;
lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
lb.BackColor = Color.Transparent;
pb.ImageLocation = #"..\image\image.jpg";
pb.MouseDown += new System.Windows.Forms.MouseEventHandler(this.picmap1_MouseDown_1);
pb.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picmap1_MouseMove_1);
pb.MouseUp += new System.Windows.Forms.MouseEventHandler(this.picmap1_MouseUp_1);
picmap1.Controls.Add(pb);
picmap1.Controls.Add(lb);
c++;
}
private void picmap1_MouseMove_1(object sender, MouseEventArgs e)
{
var c = sender as PictureBox;
if (!_dragging || null == c) return;
c.Top = e.Y + c.Top - _yPos;
c.Left = e.X + c.Left - _xPos;
foreach (Control d in picmap1.Controls)
if (d is Label)
{
d.Top = e.Y + d.Top - _yPos;
d.Left = e.X + d.Left - _xPos;
}
}
private void picmap1_MouseUp_1(object sender, MouseEventArgs e)
{
var c = sender as PictureBox;
if (null == c) return;
_dragging = false;
}
private void picmap1_MouseDown_1(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
_dragging = true;
_xPos = e.X;
_yPos = e.Y;
foreach (Control d in picmap1.Controls)
if (d is Label)
{
_xPos = e.X;
_yPos = e.Y;
}
}
this is example of dynamic add control with move on mouse drag
I am working with windows form application in c#. I have installed licensed version of TeeChart for.net v3. I am trying to remove some unwanted portion of TeeChart.
Thanks to stackoverflow website users, they helped me to create a mouse click popup on TeeChart. Now i want to implement some functionality for that popup clicks.
using the below code i am able to create mouse right click popup.
double m_dblTempVolFromTo = 0;
double dtFromTo = 0;
private void mainTChart_MouseUp(object sender, MouseEventArgs e)
{
if (!checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
{
m_dblTempVolFromTo = mainTChart.Series[0].XScreenToValue(e.X);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
else if (checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
{
DateTime dt;
dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
DateTime.TryParse(dtFromTo.ToString(), out dt);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
}
The above code is creating popup like as shown below.
I am trying to implement functionality for "For 7.6 cc" and "To 7.6 cc".
when i click "To 7.6 cc" then the chart should remove from "0 to 7.6" scale and remaining part should be there. As well the same apply for "From 145 cc" click, it has to remove the chart from "145 to 150(end of scale)".
click on "To" is using to remove starting portion of chart and click on "From" is using to remove end portion of chart.
I have tried like this but i am not able to get what i want.
void From_Click(object sender, EventArgs e)
{
if (!checkBoxIsTime.Checked)
{
var destBitmap = mainTChart.Export.Image.Bitmap.Clone(new Rect(0, 0, 100, 200), sourceBitmap.PixelFormat);
}
}
void To_Click(object sender, EventArgs e)
{
}
even i have tried with this code also
void mainTChart_GetLegendRect(object sender, mainTChart.GetLegendRectEventArgs e)
{
Rectangle cropRect = e.Rectangle;
Bitmap legendImg = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(legendImg))
{
g.DrawImage(chartBmp, new Rectangle(0, 0, mainTChart.Width, mainTChart.Height),
cropRect,
GraphicsUnit.Pixel);
}
}
nothing is working for me. Can any one help me with this task.
Thanks in advance.
Edited
when i have X-axis as time then i am not able to get display time on mouse click. The code I have tried as shown below
DateTime dt;
dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
DateTime.TryParse(dtFromTo.ToString(), out dt);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));
I am getting like this
I am getting as shown in the above image but want to display the equivalent time on mouse right click. I am getting some value to this variable "dtFromTo" like 41322.9876587965" but i am not able to convert that value into time. please help me.
Known the values you want to "cut" From/To, you can just set the Bottom axis Minimum and Maximum properties.
when i click "To 7.6 cc" then the chart should remove from "0 to 7.6"
scale and remaining part should be there
This would be:
mainTChart.Axes.Bottom.AutomaticMinimum = false;
mainTChart.Axes.Bottom.Minimum = 7.6;
As well the same apply for "From 145 cc" click, it has to remove the
chart from "145 to 150(end of scale)".
This would be done setting the Maximum:
mainTChart.Axes.Bottom.AutomaticMaximum = false;
mainTChart.Axes.Bottom.Maximum = 145;
So I think this should to the trick:
void From_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMaximum = false;
mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
}
void To_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMinimum = false;
mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
}
I'd also suggest you to create the ContextMenu in mainTChart_MouseDown event insted of mainTChart_MouseUp because creating it at the mainTChart_MouseUp event is too late, the ContextMenu shown won't show the updated version.
Edit:
For DateTime XValues, the wrong line is this one:
DateTime.TryParse(dtFromTo.ToString(), out dt);
Here it is the full code that seems to work fine for me here:
private void InitializeChart()
{
mainTChart.Aspect.View3D = false;
Line line1 = new Line(mainTChart.Chart);
line1.XValues.DateTime = true;
line1.FillSampleValues();
mainTChart.Axes.Bottom.Labels.DateTimeFormat = "hh:mm";
mainTChart.MouseDown += new MouseEventHandler(mainTChart_MouseDown);
}
double m_dblTempVolFromTo = 0;
double dtFromTo = 0;
void mainTChart_MouseDown(object sender, MouseEventArgs e)
{
if (!mainTChart.Axes.Bottom.IsDateTime && e.Button == MouseButtons.Right)
{
m_dblTempVolFromTo = mainTChart[0].XScreenToValue(e.X);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
else if (e.Button == MouseButtons.Right)
{
dtFromTo = mainTChart[0].XScreenToValue(e.X);
String stFromTo = mainTChart.Axes.Bottom.Labels.LabelValue(dtFromTo);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + stFromTo));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + stFromTo));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
}
void From_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMaximum = false;
if (!mainTChart.Axes.Bottom.IsDateTime)
mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
else
mainTChart.Axes.Bottom.Maximum = dtFromTo;
}
void To_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMinimum = false;
if (!mainTChart.Axes.Bottom.IsDateTime)
mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
else
mainTChart.Axes.Bottom.Minimum = dtFromTo;
}