First, a great thank you to those who asked/responded to questions. You were able to get me this far.
I wanted to help a young Belgian entrepreneur by taking on a challenge, build a Media managing software to display various media types (Images, Videos, links, text) on huge LED screens.
I have limited coding experience as I work in EDI.
My issue is that I create playlists dynamically based on the number of playlists in the DB (see screenshot), but I cannot trigger the playing of the right playlist when pressing the play button.
Warning, my code is noob code.
PlayList ScreenShot
Label playListLbl = new Label();
GroupBox playListGrp = new GroupBox();
public GroupBox addplayListGrp(int i, int start, int end)
{
GroupBox playListGrp = new GroupBox();
playListGrp.Name = "playListGrp"+ Convert.ToString(1 + i);
playListGrp.Text = "Play list " + Convert.ToString(1 + i);
playListGrp.Font = new Font("Century Gothic", 12F,
FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
playListGrp.Width = 425;
playListGrp.Height = 525;
playListGrp.Margin = new Padding(1);
playListGrp.Location = new Point(start, end);
return playListGrp;
}
Button addPlayBtn(int i)
{
Button PlayBtn = new Button();
PlayBtn.Font = new Font("Century Gothic", 9.75F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
PlayBtn.ForeColor = Color.Black;
PlayBtn.Location = new Point(10, 467);
PlayBtn.Name = "playBtn" + Convert.ToString(1 + i);
PlayBtn.Size = new Size(100, 30);
PlayBtn.TabIndex = 6;
PlayBtn.Text = "Play";
PlayBtn.UseVisualStyleBackColor = true;
PlayBtn.Click += new EventHandler(playBtn1_Click);
return PlayBtn;
}
public BMS_main()
{
int startPos = 5;
int endPos = 5;
for (int i = 1; i <= playlistCountInc; i++)
{
playListGrp = addplayListGrp(i, startPos, endPos);
playListLbl = addLabel(i);
Label playListLblTime = addLabelTime(i);
Button PlayBtn = addPlayBtn(i);
}
playListGrp.Controls.Add(playListLbl);
playListGrp.Controls.Add(playListLblTime);
playListGrp.Controls.Add(playlistView);
playListGrp.Controls.Add(PlayBtn);
}
private void playBtn1_Click(object sender, EventArgs e)
{
if (ScreenStatus)
{
Playing = true;
DisplayTimer.Stop();
DisplayTimer.Enabled = false;
InitialScreenTimer.Stop();
InitialScreenTimer.Enabled = false;
PlayListTimer.Enabled = true;
PlayListTimer.Start();
}
else
{
message = "Veuillez alimenter les panneaux";
result = MessageBox.Show(message, caption, buttons);
}
public void PlayListTimer_Tick(object sender, EventArgs e)
{
Label lblAcessorio4 =
(Label)playListLbl.Controls.Find("playLbl4",
true).FirstOrDefault();
if (lblAcessorio4 != null)
{
lblAcessorio4.Text = "Test lblAcessorio4";
}
else
{
message = "Label is null";
result = MessageBox.Show(message, caption, buttons);
}
Set the Tag property of your button with something which will help you decide later on which song to play:
playListGrp = addplayListGrp(i, startPos, endPos);
playListLbl = addLabel(i);
Label playListLblTime = addLabelTime(i);
Button PlayBtn = addPlayBtn(i);
// You can do this
PlayBtn.Tag = playListGrp; // or anything else
Then in the button click handler, get the value of the Tag and make a decision based on that. Just keep in mind that whatever you set the Tag to, you will need to cast it back to that type. For example, in the above I set it GroupBox so I will cast it to a GroupBox:
private void playBtn1_Click(object sender, EventArgs e)
{
GroupBox gb = ((Button)(sender)).Tag as GroupBox;
// Now make the decision
if(gb.Name == "whatever you need to put here"){ // do whatever }
}
I would put the lisbox and then get the selected item and play that.
Related
I have a ComboBox whose DropdownStyle is DropDown. So, I can select from the list or can type own data.
When I click on the clear button, text typed in the ComboxBox should be removed.
I have already tried "object.text = string.Empty;" but not working.
public ComboBox cmb_Identifier(int x, int y)
{
cntrlObjList.comboBoxIdentifier.Items.Clear();
cntrlObjList.comboBoxIdentifier.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
cntrlObjList.comboBoxIdentifier.FormattingEnabled = true;
cntrlObjList.comboBoxIdentifier.Location = new System.Drawing.Point(x, y);
cntrlObjList.comboBoxIdentifier.Name = "cmbIdentifier";
cntrlObjList.comboBoxIdentifier.Size = new System.Drawing.Size(151, 21);
cntrlObjList.comboBoxIdentifier.TabIndex = 6;
cntrlObjList.comboBoxIdentifier.Visible = true;
cntrlObjList.comboBoxIdentifier.Items.Add("Identifier 1");
cntrlObjList.comboBoxIdentifier.Items.Add("Identifier 2");
return cntrlObjList.comboBoxIdentifier;
}
public void btnClear_Click(object sender, EventArgs e)
{
cntrlObjListMain.comboBoxIdentifier.SelectedText = string.Empty(); //not working
}
I found the solution and it is working in my case.
cntrlObjList.comboBoxIdentifier.Text = null;
The following should work for you
cntrlObjList.comboBoxIdentifier.Text = String.Empty;
or
cntrlObjList.comboBoxIdentifier.Text = "";
I have a windows form (parent) that takes a value from textbox and then opens child form which then uses that value to select an image from a directory. When multiple images are found for the particular value, I have the form modified to display a couple of buttons to navigate (Next & Previous) to display the different images. Upon first opening the parent form, entering a value and then using form.show() to display the child form – everything works as expected. But if another value is entered into parent form (child form can still be open or exited (hidden)) and the ‘Next’ button is clicked the code in the click event is running over again for however many number of images are in the List(imagesFound). Say I have 3 images in the List(imagesFound) and I step through the code in debug mode the btnNext click event fires 3 times in a row. This of course runs GetMultiImages method which causes sequence of displaying the images to be all of. And again, this doesn’t happen the first time a value is entered into parent form. I’ve made sure the list and other variables are cleared out in GetImage method. I’m stumped…any ideas?
Form1:
public partial class Form1 : Form
{
private string parcelID;
Form2 viewer = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
parcelID = txtParID.Text;
ShowViewer();
}
private void ShowViewer()
{
viewer.GetImage(parcelID);
if (viewer.NoImageFound == true)
{
viewer.Show();
viewer.Focus();
}
else if (viewer.NoImageFound == false)
{
viewer.Hide();
}
}
}
Child Form:
public partial class Form2 : Form
{
public Button btnNext = new Button();
public Button btnPrev = new Button();
private List<string> imagesFound = new List<string>();
private string Path;
private string parcel;
private int increment;
private int maxNum;
public bool NoImageFound;
//multi image members
private string firstMultiItem;
private string selectMultiImage;
Image parMultiImage;
public Form2()
{
InitializeComponent();
}
public void GetImage(string ParcelID)
{
NoImageFound = true;
parcel = ParcelID;
increment = 0;
maxNum = 0;
firstMultiItem = null;
selectMultiImage = null;
parMultiImage = null;
imagesFound.Clear();
Path = "........\\Images\\";
try
{
if (!string.IsNullOrEmpty(parcel))
{
string parcelTrim = parcel.Substring(0, 6);
Path = Path + parcelTrim + "\\";
foreach (string s in Directory.GetFiles(Path, parcel + "_" + "*"))
{
string trimString = s.Replace(Path, "");
imagesFound.Add(trimString);
}
if ((imagesFound.Count == 0))
{
MessageBox.Show("No images found for ParcelID: " + parcel);
picBox.Image = null;
this.Text = "";
NoImageFound = false;
}
else
{
if (imagesFound.Count == 1)
{
string firstItem = imagesFound[0].ToString();
string selectImage = Path + firstItem;
Image parImage = Image.FromFile(selectImage);
//in order to access the picture box control you have to change it's
//access modifier (Modifier) from private to public. Defaults to private
picBox.Image = parImage;
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.Text = parcel;
SingleForm();
}
else if (imagesFound.Count > 1)
{
firstMultiItem = imagesFound[0].ToString();
maxNum = imagesFound.Count;
selectMultiImage = Path + firstMultiItem;
parMultiImage = Image.FromFile(selectMultiImage);
picBox.Image = parMultiImage;
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.Text = parcel;
MultiImageForm();
}
}
}
else
{
MessageBox.Show("No ParcelID");
}
}
catch (DirectoryNotFoundException)
{
string text = parcel;
MessageBox.Show("ParcelID: " + text + " could not be found. The directory may be missing.", "There's a problem locating the image.",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SingleForm()
{
this.Height = 400;
btnNext.Visible = false;
btnPrev.Visible = false;
}
private void MultiImageForm()
{
//set form properties
this.Text = parcel;
this.Height = 432;
//set btnNext properties
btnNext.Location = new Point(307, 375);
btnNext.Size = new Size(75, 25);
btnNext.Font = new Font("Maiandra GD", 10, FontStyle.Bold);
btnNext.Text = ">>";
//add btnNext to form
this.Controls.Add(btnNext);
btnNext.Visible = true;
btnNext.Enabled = true;
//creating event handler for btnNext
btnNext.Click += new EventHandler(btnNext_Click);
//set btnPrev properties
btnPrev.Location = new Point(12, 375);
btnPrev.Size = new Size(75, 25);
btnPrev.Font = new Font("Maiandra GD", 10, FontStyle.Bold);
btnPrev.Text = "<<";
//add btnPrev to form
this.Controls.Add(btnPrev);
btnPrev.Visible = true;
btnPrev.Enabled = false;
//creating event handler for btnPrev
btnPrev.Click += new EventHandler(btnPrev_Click);
}
private void GetMultiImages()
{
try
{
firstMultiItem = imagesFound[increment].ToString();
selectMultiImage = Path + firstMultiItem;
parMultiImage = Image.FromFile(selectMultiImage);
picBox.Image = parMultiImage;
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("Index was out of range.");
}
}
private void btnNext_Click(object sender, System.EventArgs e)
{
if (increment != maxNum - 1)
{
increment++;
GetMultiImages();
}
EnableButtons();
}
private void btnPrev_Click(object sender, System.EventArgs e)
{
if (increment > 0)
{
increment--;
GetMultiImages();
}
EnableButtons();
}
private void EnableButtons()
{
if (increment == 0)
{
btnPrev.Enabled = false;
btnNext.Enabled = true;
}
else if (increment > 0 & increment != maxNum - 1)
{
btnPrev.Enabled = true;
btnNext.Enabled = true;
}
else if (increment == maxNum - 1)
{
btnPrev.Enabled = true;
btnNext.Enabled = false;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//overriding closing event
this.Hide();
e.Cancel = true;
}
}
//creating event handler for btnNext
btnNext.Click += new EventHandler(btnNext_Click);
That's a bug. You keep adding a Click event handler for the button, each time you call MultiImageForm(). So the event handler runs multiple times for a single click.
Only add event handlers in the form constructor so you can be sure it is only done once.
I am using C# in VS 2010. I created a custom panel and would like to add this custom panel 9 times so I created a loop to add a copy of the panel 9 times at equal distance from each other. Each panel will have its own text and image. All I'm getting though is a single panel. Any insight would be appreciated
public partial class Form1 : Form
{
int index = 0;
List<CutePanel.CustomPanel> MenuItems = new List<CutePanel.CustomPanel>();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 9; i++)
{
this.cpTest.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.cpTest.LabelText = "My super click text";
this.cpTest.Location = new System.Drawing.Point(12, 12+(64*i));
this.cpTest.Name = "cpTest";
this.cpTest.Size = new System.Drawing.Size(344, 58);
this.cpTest.SuperClick = null;
this.cpTest.TabIndex = 6;
}
cpTest.MouseClick += new MouseEventHandler(cpTest_MouseClick);
cpTest.SuperClick += new EventHandler(cpTest_SuperClick);
cpTest.LabelText = "This is my text.";
MenuItems.Add(cpTest);
}
void cpTest_SuperClick(object sender, EventArgs e)
{
tcTest.SelectedIndex = index++ % 2;
}
void cpTest_MouseClick(object sender, MouseEventArgs e)
{
tcTest.SelectedIndex = index++ % 2;
}
private void customPanel3_MouseClick(object sender, MouseEventArgs e)
{
tcTest.SelectedIndex = index++ % 2;
}
}
Thanks.
You have to make a distinction between your panel class and panel objects, also called instances of this class. Think of the class as a template that serves in creating objects. These objects are created with the new keyword:
for (int i = 0; i < 9; i++)
{
var cp = new CutePanel.CustomPanel();
cp.BackColor = System.Drawing.SystemColors.ActiveCaption;
cp.LabelText = "My super click text";
cp.Location = new System.Drawing.Point(12, 12+(64*i));
cp.Name = "cpTest" + i;
cp.Size = new System.Drawing.Size(344, 58);
cp.SuperClick = null;
cp.TabIndex = 6;
cp.MouseClick += new MouseEventHandler(cpTest_MouseClick);
cp.SuperClick += new EventHandler(cpTest_SuperClick);
cp.LabelText = "This is my text.";
MenuItems.Add(cp);
}
You can also assign it values from the existing panel:
cp.BackColor = cpTest.BackColor;
cp.Size = cpTest.Size;
...
An elegant way of making a duplicate is to include a Clone method in your panel class
public class CustomPanel
{
...
public CustomPanel Clone()
{
var cp = (CustomPanel)this.MemberwiseClone();
cp.Parent = null; // It has not yet been added to the form.
return cp;
}
}
Then
for (int i = 0; i < 9; i++)
{
CustomPanel cp = cpTest.Clone();
// Now only change the differing properties
cp.Location = new System.Drawing.Point(12, 12+(64*i));
cp.Name = "cpTest" + i;
cp.TabIndex += i + 1;
MenuItems.Add(cp);
}
but attention: If the cloned control is a container control containing other controls, you must clones those recursively as well. I.e., you must perform a deep clone! Creating new controls as shown in my first code snippet is safer.
Using Windows Forms, two link labels are created dynamically. When the user clicks on anyone of links labels, one dynamic form is created. In that form I created one data grid, a text box and a button placed dynamically (in that dynamic form). Now I want to access the dynamic data grid in the dynamic button click event. How can I do that?
private void Users_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Usp_Get_Employees", con);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string somecode = i.ToString() + ds.Tables[0].Rows[i]["eid"].ToString();
LinkLabel lbluser = new LinkLabel();
lbluser.Name = ds.Tables[0].Rows[i]["eid"].ToString();
lbluser.Text = ds.Tables[0].Rows[i]["ename"].ToString();
lbluser.Location = new System.Drawing.Point(40, i * 40);
lbluser.Size = new System.Drawing.Size(50, 30);
Controls.Add(lbluser);
lbluser.Click += new EventHandler(lbluser_Click);
}
}
}
void lbluser_Click(object sender, EventArgs e)
{
LinkLabel lnkClis = (LinkLabel)sender;
Form frm = new Form();
frm.Name = lnkClis.Name;
frm.Text = lnkClis.Text;
frm.Show();
DataGrid dtgrd = new DataGrid();
dtgrd.Location = new System.Drawing.Point(10, 1 * 40);
dtgrd.Name = lnkClis.Name;
names = lnkClis.Name;
TextBox tx = new TextBox();
tx.Location = new System.Drawing.Point(10, 5 * 40);
tx.Size = new Size(80, 30);
tx.Multiline = true;
tx.LostFocus += new EventHandler(tx_LostFocus);
Button btn = new Button();
btn.Location = new System.Drawing.Point(10, 7 * 40);
btn.Size = new System.Drawing.Size(50, 30);
btn.Name = lnkClis.Name;
btn.Click += new EventHandler(btn_Click);
frm.Controls.Add(dtgrd);
frm.Controls.Add(tx);
frm.Controls.Add(btn);
}
// Now I am trying to access the data grid in the btn_click event
void btn_Click(object sender, EventArgs e)
{
Button btsave = (Button)sender;
string eid = btsave.Name;
object grd = btsave.Parent.Controls.Find("dtgrd", true).FirstOrDefault();
((DataGrid)grd).DataSource = ds.Tables[0];
}
Now I am getting an error object set of instances of an object at:
((DataGrid)grd).DataSource = ds.Tables[0];
The exception message you have written:
Now I am getting an error object set of instances of an object at
makes no sense, but it looks like
Object reference not set to an instance of an object
If this is the case, I think the error lays in Find method call. According to documentation:
Searches for controls by their Name property and builds an array of all the controls that match.
In your button click handler you assume that grid is called dtgrd, but while you create a grid you name it like this:
dtgrd.Name = lnkClis.Name;
it will suffice if you change this line to:
dtgrd.Name = "dtgrd";
Having said that, you should consider using an anonymous method for the button click handler. It will eliminate need for calling the Find method in the first place.
void lbluser_Click(object sender, EventArgs e)
{
//...
DataGrid dtgrd = new DataGrid();
//...
Button btn = new Button();
//...
btn.Click += (sender,args)=> dtgrd.DataSource = ds.Tables[0];
Try the following code
public Form1()
{
Form f1 = new Form();
f1.Text = "New Form";
TextBox t1 = new TextBox();
t1.Top = 0;
t1.Name = "t1";
t1.Visible = true;
f1.Controls.Add(t1);
Button b1 = new Button();
b1.Top = 30;
b1.Name = "b1";
b1.Text = "Click";
b1.Click += b1_Click;
f1.Controls.Add(b1);
f1.Show();
}
public void b1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
object txt = btn.Parent.Controls.Find("t1", false).First();
((TextBox)txt).Text = "Hi, you have clicked me.";
}
I modified Nitesh's code a bit. Just capture the textbox in the click handler using a lambda:
public Form1()
{
Form f1 = new Form();
f1.Text = "New Form";
TextBox t1 = new TextBox();
t1.Top = 0;
t1.Name = "t1";
t1.Visible = true;
f1.Controls.Add(t1);
Button b1 = new Button();
b1.Top = 30;
b1.Name = "b1";
b1.Text = "Click";
b1.Click += (sender, args) => MessageBox.Show("The text is: " + t1.Text);
f1.Controls.Add(b1);
f1.Show();
}
The error you are getting is from the statement (as the grd object is null):
((DataGrid)grd).DataSource = ds.Tables[0];
Since you are trying to catch hold of a dynamic control, it's good have a proper null checks, type checks, and error handling. Something like this:
if(grd != null && grd is DataGrid)
((DataGrid)grd).DataSource = ds.Tables[0];
I'm trying to fade in a new control to my application's "app" area which is programmatically added after the existing controls are removed. My code looks like this:
void settingsButton_Clicked(object sender, EventArgs e)
{
ContentCanvas.Children.Clear();
// Fade in settings panel
NameScope.SetNameScope(this, new NameScope());
SettingsPane s = new SettingsPane();
s.Name = "settingsPane";
this.RegisterName(s.Name, s);
this.Resources.Add(s.Name, s);
Storyboard sb = new Storyboard();
DoubleAnimation settingsFade = new DoubleAnimation();
settingsFade.From = 0;
settingsFade.To = 1;
settingsFade.Duration = new Duration(TimeSpan.FromSeconds(0.33));
settingsFade.RepeatBehavior = new RepeatBehavior(1);
Storyboard.SetTargetName(settingsFade, s.Name);
Storyboard.SetTargetProperty(settingsFade, new PropertyPath(UserControl.OpacityProperty));
ContentCanvas.Children.Add(s);
sb.Children.Add(settingsFade);
sb.Begin();
}
However, when I run this code, I get the error "No applicable name scope exists to resolve the name 'settingsPane'."
What am I possibly doing wrong? I'm pretty sure I've registered everything properly :(
I wouldn't hassle with the NameScopes etc. and would rather use Storyboard.SetTarget instead.
var b = new Button() { Content = "abcd" };
stack.Children.Add(b);
var fade = new DoubleAnimation()
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(5),
};
Storyboard.SetTarget(fade, b);
Storyboard.SetTargetProperty(fade, new PropertyPath(Button.OpacityProperty));
var sb = new Storyboard();
sb.Children.Add(fade);
sb.Begin();
I solved the problem using this as parameter in the begin method, try:
sb.Begin(this);
Because the name is registered in the window.
I agree, the namescopes are probably the wrong thing to use for this scenario. Much simpler and easier to use SetTarget rather than SetTargetName.
In case it helps anyone else, here's what I used to highlight a particular cell in a table with a highlight that decays to nothing. It's a little like the StackOverflow highlight when you add a new answer.
TableCell cell = table.RowGroups[0].Rows[row].Cells[col];
// The cell contains just one paragraph; it is the first block
Paragraph p = (Paragraph)cell.Blocks.FirstBlock;
// Animate the paragraph: fade the background from Yellow to White,
// once, through a span of 6 seconds.
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
p.Background = brush;
ColorAnimation ca1 = new ColorAnimation()
{
From = Colors.Yellow,
To = Colors.White,
Duration = new Duration(TimeSpan.FromSeconds(6.0)),
RepeatBehavior = new RepeatBehavior(1),
AutoReverse = false,
};
brush.BeginAnimation(SolidColorBrush.ColorProperty, ca1);
It is possible odd thing but my solution is to use both methods:
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
sb.Begin(this);
In this case there is no error.
Have a look at the code where I have used it.
int n = 0;
bool isWorking;
Storyboard sb;
string myObjectName;
UIElement myElement;
int idx = 0;
void timer_Tick(object sender, EventArgs e)
{
if (isWorking == false)
{
isWorking = true;
try
{
myElement = stackObj.Children[idx];
var possibleIDX = idx + 1;
if (possibleIDX == stackObj.Children.Count)
idx = 0;
else
idx++;
var myRect = (Rectangle)myElement;
// Debug.WriteLine("TICK: " + myRect.Name);
var dur = TimeSpan.FromMilliseconds(2000);
var f = CreateVisibility(dur, myElement, false);
sb.Children.Add(f);
Duration d = TimeSpan.FromSeconds(2);
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
sb.Children.Add(DA);
myObjectName = myRect.Name;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + DateTime.Now.TimeOfDay);
}
isWorking = false;
}
}