I display dynamically some images and when i hover with mouse i want to display a text box which, if case, will be displayed over all images or over some of them.
This is how i dispay images dynamically:
var picture = new PictureBox
{
Name = "pictureBox" + contor,
Size = new Size(72, 72),
Location = new Point(x, y),
Image = Image.FromFile("C://Users//radul//OneDrive//Desktop//Pictures Lic//rf 72x72.png"),
};
x += 100;
y = (y + 100) % this.Size.Height;
picture.MouseDown += new MouseEventHandler(picture_MouseDown);
picture.MouseMove += new MouseEventHandler(picture_MouseMove);
picture.MouseUp += new MouseEventHandler(picture_MouseUp);
picture.MouseHover += new EventHandler(picture_MouseHover);
picture.MouseLeave += new EventHandler(picture_MouseLeave);
this.Controls.Add(picture);
and to show textbox:
private void picture_MouseHover(object sender, EventArgs e){
tBox.Height = 100;
tBox.Width = 400;
tBox.Multiline = true;
tBox.BringToFront();
tBox.Text = detalii;
tBox.Top = picBox.Top + 20;
tBox.Left = picBox.Right + 20;
this.Controls.Add(tBox);
}
Output here is what my program displays and what i want is that textbox to be over that images.
Related
I am trying to dynamically create an array of images on a form. THe images are rendering okay, but I cannot work out how to create the eventHandlers.
pictureBox.Click += new System.EventHandler(this.pictureBox);
This line is causing be problems.
private void frmBorderlessMain_Load(object sender, EventArgs e)
{
int top = 10;
int left = 10;
int width = 200;
int height = 150;
var uutNames = MyObject.GetNames().ToArray();
var uutImages = MyObject.GetImages().ToArray();
for (int i = 0; i < uutNames.Length; i++)
{
System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
Image newImage = Image.FromFile(uutImages[i]+".png");
pictureBox.Image = ((System.Drawing.Image)(newImage));
pictureBox.Location = new System.Drawing.Point(left, top);
pictureBox.Name = "pictureBox"+i;
pictureBox.Size = new System.Drawing.Size(200, 150);
pictureBox.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.Zoom;
pictureBox.TabIndex = i;
pictureBox.TabStop = false;
pictureBox.Click += new System.EventHandler(this.pictureBox);
this.tabPage1.Controls.Add(pictureBox);
left += width + 10;
if ( (i > 0 ) && (i % 3) == 0)
{
top += height + 10;
left = 10;
}
}
}
Additional Info:
Upon clicking the image I would like a new form to open displaying details about the entity that was clicked. It will be the same form, but will be loaded with data depending on which image was clicked on.
pictureBox.Click += new System.EventHandler(this.pictureBox);
That is not how event handlers are used.
Lambda:
pictureBox.Click += (sender, eventArgs) => Console.WriteLine("HELP I WAS CLICKED OMG");
Or with a function:
pictureBox.Click += PictureBox_Click;
....
private void PictureBox_Click(object sender, EventArgs e)
{
Console.WriteLine("Stop clicking me >:(");
}
I have a MetroTabControl named mtcNewExpTabControl with four pages(mtpSettings, mtpNewExp, mtpTempImages, mtpCompareImages). I am starting New Experiment on mtpNewExp page and get image after clicking "StartExperiment" button. User can get many images. After completion of experiment, I add images to imageList1.Images.
When I click on the mtpTempImages tabPage mtcNewExpTabControl_SelectedIndexChanged triggered and images are displayed with combobox named with imageList1.Images.Keys[i]. I can show many combobox and picturebox pairs on mtpTempImages.
On mtpTempImages there is also a mtbCompareImages button. User can check checkboxes next to the picturebox named same with checboxName. After checking all the checkboxes needed to compared, user will click on the "Compare Images" button. I want to change active tabPage to mtpCompareImages without clicking on the tab. mtbCompareImages button should be enough for that. I can show images on mtpTempImages but I could not succeded that on mtpCompareImages tabpage. Active tabPage also do not change to mtpCompareImages tabPage. What should I do?
List<MetroCheckBox> cbxTempImages = new List<MetroCheckBox>();
List<MetroCheckBox> cbxCompareImages = new List<MetroCheckBox>();
List<PictureBox> pbxTempImages = new List<PictureBox>();
List<PictureBox> pbxCompareImages = new List<PictureBox>();
private void mtbCompareImages_Click(object sender, EventArgs e)
{
for (int i = 0; i < cbxTempImages.Count(); i++)
{
if (cbxTempImages[i].Checked == true)
{
imageListChecked.Images.Add(pbxTempImages[i].Name, pbxTempImages[i].Image);
}
}
this.mtcNewExpTabControl.SelectedTab = mtpCompareImages;
}
private void mtcNewExpTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if((MetroTabPage)this.mtcNewExpTabControl.SelectedTab == mtpTempImages)
{
for (int i = 0; i < imageList1.Images.Count; i++)
{
cbxTempImages.Add(new MetroCheckBox());
cbxTempImages[i].Name = imageList1.Images.Keys[i].ToString();
cbxTempImages[i].Size = new Size(15, 15);
cbxTempImages[i].BackColor = Color.Transparent;
cbxTempImages[i].Location = new Point(x, y);
//PictureBox pic = new PictureBox();
pbxTempImages.Add(new PictureBox());
pbxTempImages[i].Name = imageList1.Images.Keys[i].ToString();
pbxTempImages[i].Image = imageList1.Images[i];
pbxTempImages[i].SizeMode = PictureBoxSizeMode.StretchImage;
pbxTempImages[i].Location = new Point(x + 15, y);
x += 120;
if (x > this.pnlTempImages.Width - 120)
{
x = 10; y += 100;
}
this.pnlTempImages.Controls.Add(cbxTempImages[i]);
this.pnlTempImages.Controls.Add(pbxTempImages[i]);
}
}
else if((MetroTabPage)this.mtcNewExpTabControl.SelectedTab == mtpCompareImages)
{
x = 10; y = 10;
for (int i = 0; i < imageListChecked.Images.Count; i++)
{
cbxCompareImages.Add(new MetroCheckBox());
//MetroCheckBox cbxCI = new MetroCheckBox();
cbxCompareImages[i].Name = imageListChecked.Images.Keys[i].ToString();
cbxCompareImages[i].Size = new Size(15, 15);
cbxCompareImages[i].BackColor = Color.Transparent;
cbxCompareImages[i].Location = new Point(x, y);
//PictureBox picCI = new PictureBox();
pbxCompareImages.Add(new PictureBox());
pbxCompareImages[i].Name = imageListChecked.Images.Keys[i].ToString();
pbxCompareImages[i].Image = imageListChecked.Images[i];
pbxCompareImages[i].SizeMode = PictureBoxSizeMode.StretchImage;
pbxCompareImages[i].Location = new Point(x + 15, y);
x += 120;
if (x > this.pnlCompareImages.Width - 120)
{
x = 10; y += 100;
}
this.pnlCompareImages.Controls.Add(cbxCompareImages[i]);
this.pnlCompareImages.Controls.Add(pbxCompareImages[i]);
}
}
InitializeComponent();
}
on startup I'm generating a lot of controls 90 to be exact and everything is working ok EXCEPT for the labels they are not being drawn or something? they are there because I can click them and they show proper ID (click event) here's the genereation code
private static bool ClientsLoaded = false;
private static WebBrowser[] Clients = new WebBrowser[45];
private static Label[] ClientLabel = new Label[45];
private static int MaximizedClient = -1;
public Form1()
{
InitializeComponent();
int WBoffsetX = 0;
int WBoffsetY = 0;
int lbloffsetX = 0;
int lbloffsetY = 0;
for (int i = 0; i < 45; i++)
{
var wb = new WebBrowser();
Clients[i] = wb;
wb.ScrollBarsEnabled = false;
wb.Height = 12;
wb.Width = 12;
wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
WBoffsetX += 13;
wb.ScriptErrorsSuppressed = true;
this.Controls.Add(wb);
ClientLabel[i] = new Label();
ClientLabel[i].Name = "lbl_" + i;
ClientLabel[i].Font = new Font("Arial", 12);
ClientLabel[i].ForeColor = System.Drawing.Color.White;
ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
lbloffsetX += 22;
ClientLabel[i].Click += new EventHandler(lbl_click);
ClientLabel[i].Text = "C" + i + ": o";
this.Controls.Add(ClientLabel[i]);
}
}
I've tried adding a button with for(45) clientlabel[i].Refresh() and it did nothing I tried changing the visibilty of them all to false and then back to true and nothing however I did find 1 thing interesting if I hide lbl_1 label 2 text will appear if I had label 2 label 3 text will appear but if I change the previous label back to visible they stay invisible textwise
I can click in a line on the form and
private void lbl_click(object sender, EventArgs e)
{
int id = -1;
var s = sender.ToString();
for(int i = 0; i<=45; i++)
{
if (s.Contains("C" + i + ":"))
{
id = i;
}
}
MessageBox.Show("Hello label, " + id);
}
will pop up the proper ids etc
does anyone know what's causing this maybe? or how to fix it
Well, I don't know what is the problem. This code works well enough and it has only marginal differences with the original(AutoSize property, explicit statement of Height and Width, and minor Location adjustment):
for (int i = 0; i < ClientLabel.Length; i++)
{
// Web browsers
WebBrowser wb = new WebBrowser()
{
ScrollBarsEnabled = false,
Height = 12,
Width = 12,
Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
ScriptErrorsSuppressed = true
};
WBoffsetX += 13;
Clients[i] = wb;
// Labels
Label label = new Label()
{
Name = "label_" + i,
Text = "Data",
AutoSize = true,
Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
Width = 100,
Height = 20,
Font = new Font("Arial", 12),
ForeColor = System.Drawing.Color.White,
};
label.Click += new EventHandler(lbl_click);
ClientLabel[i] = label;
lbloffsetX += 30;
}
this.Controls.AddRange(Clients);
this.Controls.AddRange(ClientLabel);
So I have a form, and I want to add some Panels with some controls(labels, and radiobuttons) when the form loads.
And I want to do it from the code, of course(it's for making an application with tests, and the questions will be random)
This is what I have done till now:
List<Panel>ls=new List<Panel>();
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Controls.Add(l);
pan.Show();
}
}
But it doesn't show anything on the form.
Add the panel just created to the Form.Controls collection
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Location = new Point(10, i * 100);
pan.Size = new Size(200, 90); // just an example
pan.Controls.Add(l);
this.Controls.Add(pan);
}
}
enter image description here
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
why not display label2 & label3?
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