As a newbie I have tried several Google searches and have found a few confusing answers. What I am trying to achieve is:
click on a button (one of many),
extract that button's text value, then
use that value to make the relevant placeholder visible.
So far I have done the first 2 steps but how do I accomplish step 3? My code so far, which works if I click on the Asia button, is:
protected void btnArea_Click(object sender, EventArgs e)
{
string ar = (sender as Button).Text;
//ar = "Asia";
phdasia.Visible = true;
}
In simple, newbie friendly terms, what do I have to insert in place of phdasia?
If your placeholder controls share the same name format you may be able to reach them by name:
protected void btnArea_Click(object sender, EventArgs e)
{
string ar = (sender as Button).Text;
//ar = "Asia";
string name = "phd" + ar.ToLower(); // The naming format comes here
Control[] controls = this.Controls.Find(name, true); //find the control(s) by name
foreach(Control control in controls) // mow loop and make them visible
control.Visible = true;
//phdasia.Visible = true;
}
Edit: alternatively you can use FindControl method to locate a control with an ID property of "phdasia" on the containing page:
Control control = FindControl(name);
if(control!=null)
control.Visible = true;
Related
OK so I have to work with lots of toggle button and textbox, so I named them in such way that adding letter "Q" to button's name will give name of the textbox that the button corresponds to. I have link all the button to the same method on checked event, and using this naming mechanism I, hope to manipulate the textbox corresponding the button.
What I have done till now is, generate an name of the textbox in string format. But i don't know to how to use this string to manipulate the textbox.
private void OnbuttonClick(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if(tb.IsChecked==true)
{
string tbnam = ((ToggleButton)sender).Name;
string name2 = tbnam + "Q";
?????? (what happens from this point onward)
}
}
"name2" is the name of textbox that corresponds to the toggle button of name "tbnam".
I hope the I have made the problem clear.
In WPF, a much better way to do this would to expose a collection of objects in the viewmodel, and display in the UI with an ItemsControl with an ItemTemplate that would dynamically create as many iterations of ToggleButton/TextBox pairs as needed. I'd be happy to go into how to do that, if you're interested.
But for now, you can get your code working using the FindName(string) method:
private void Button_Click(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if (tb.IsChecked == true)
{
string name2 = tb.Name + "Q";
TextBox tbox = FindName(name2) as TextBox;
// Check to see if tbox is null, do stuff with it
}
}
I have set all my TextBoxs.readOnly = true; in the form by default.
But when an Edit Button is clicked.
I want it to make all the TextBoxs.readOnly = false;
I have tried;
private void btnEdit_Click(object sender, EventArgs e)
{
foreach (var textBox in this.Controls.OfType<TextBox>())
textBox.ReadOnly = false;
}
Each TextBox has a unique name.
The easiest way to do it would be this, below. But I don't want to do it that way.
txtName.ReadOnly = false;
txtAddress.ReadOnly = false;
...
With this approach you can set just TextBoxes's ReadOnly that are placed directly in your form not those that are inside another container like GroupBox or Panel. You need a recursive method for this purpose if you have more than one container (example1, example2). But Since all your TextBoxes are inside one GroupBox then simply replace this with your GroupBox's Name like this:
foreach (var textBox in groupBox1.Controls.OfType<TextBox>())
textBox.ReadOnly = false;
This question already has answers here:
Find control by name from Windows Forms controls
(3 answers)
Closed 8 years ago.
I am creating new controls and putting there names in a list box, how do i use the name selected in the list box to change the controls properties.
//creating the label
LabelNumber++;
label label=new Label();
label.BackColor=Color.Transparent;
label.Location=new System.Drawing.Point(1,
1);
label.Name="Label" + LabelNumber;
label.Text=LabelNumber.ToString();
label.Size=new System.Drawing.Size(20,
20);
label.TextAlign=ContentAlignment.MiddleCenter;
ProjectPanel.Controls.Add(label);
ControlBox1.Items.Add(label.Name);
The question suggests that the OP is using a ListBox, so my code makes that assumption.
Essentially what you need to do is as follows: Get the selected text from the ListBox, find the control that has the same Name (we will assume this is always unique), then change the properties of that control.
The following code will meet those requirements:
// Get the selected text from the ListBox.
string name = ControlBox1.GetItemText(ControlBox1.SelectedItem);
// Find the control that matches that Name. Assumes there is only ever 1 single match.
Control control = ProjectPanel.Controls.Find(name, true).FirstOrDefault();
// Set properties of the Control.
control.Name = "new name";
// If you know it's a Label, you can cast to Label and use Label specific properties.
Label label = control as Label;
label.Text = "some new text";
Yo can use the Label Name under ControlBox1_SelectedIndexChanged event and get the value of selected indexed Label Name.
You can create a simple 'listboxitem' structure and use it like this:
struct lbo
{
// make the structure immutable
public readonly Control ctl;
// a simple constructor
public lbo(Control ctl_) { ctl = ctl_; }
// make it show the Name in the ListBox
public override string ToString() { return ctl.Name; }
}
private void button1_Click(object sender, EventArgs e)
{
// add a control:
listBox1.Items.Add(new lbo(button1));
}
private void button2_Click(object sender, EventArgs e)
{
// to just change the _Name (or Text or other properties present in all Controls)
((lbo)listBox1.SelectedItem).ctl.Text = button2.Text;
// to use it as a certain Control you need to cast it to the correct control type!!
((Button)((lbo)listBox1.SelectedItem).ctl).FlatStyle = yourStyle;
// to make the cast safe you can use as
Button btn = ((lbo)listBox1.SelectedItem).ctl as Button;
if (btn != null) btn.FlatStyle = FlatStyle.Flat;
}
No checks here for the correct type or that you have selected an item..but you get the idea: put something more useful than a naked object or a mere string into the ListBox!
You could instead loop over all controls and compare the names but that's less efficient and actually not safe as the Name property is not guaranteed to be unique..
I have a field that currently changes text on a mouse click. This field is on multiple pages of my report, but clicking it only changes the text for that page. Here is my code for that:
const string sShowDetail2 = "Show Cumulative";
const string sHideDetail2 = "Hide Cumulative";
ArrayList expandedValues2 = new ArrayList();
// This function returns a value indicating whether a certain
// category's details should be expanded.
bool ShouldShowDetail2(int CategoryID)
{
return expandedValues2.Contains(CategoryID);
}
private void lbShowHide2_BeforePrint(object sender, PrintEventArgs e)
{
XRLabel label = (XRLabel)sender;
if (ShouldShowDetail2((int)label.Tag))
{
label.Text = sHideDetail2;
}
else
{
label.Text = sShowDetail2;
}
}
private void lbShowHide2_PreviewClick(object sender, PreviewMouseEventArgs e)
{
// Obtain the category's ID stored in the label's Tag property.
int index = (int)e.Brick.Value;
// Determine whether the current category's details are shown.
bool showDetail2 = ShouldShowDetail2(index);
// Toggle the visibility of the category's details.
if (showDetail2)
{
expandedValues2.Remove(index);
}
else
{
expandedValues2.Add(index);
}
Now I have another field that I need to simply toggle all instances of the field(every page) on click. I do not need it to read the tag of the particular one I clicked because it will be changing all of them at the same time. My problem is that I don't know how to get them all to change on mouse click. How could I modify the code above to change all instances of the label?
Here is something you could try
private void ChangeXRLabels(Control control)
{
foreach(Control childControl in control.Controls)
{
XRLabel label = childControl as XRLabel;
if(label != string.Empty)
label.Text = "Your Text Value goes Here";
else if(childControl.Controls.Count > 0)
ChangeXRLabels(childControl);
}
}
I have a List<Appointment> where an Appointment is.
public class Appointment
{
public string Title { get; set; }
public string Start { get; set; }
public string End { get; set; }
}
I want to dynamically add each list item on a separate line on the form like so:
item.Title + " between" + item.Start + " and " + item.End;
I want to be able to click each item (the text), then with each click it can toggle the colour of the text between red and black (that is, if black it turns red, if red it turns black when you click).
I come from a web background, but I am just struggling with Windows Forms data binding. I've tried with table layout panel but just don't know where to begin with changing the color of an item on click.
PS: If it helps, the number of items in the list will probably not be more than 10.
I've gotten a bit further as per Jamie Ide's comment:
var appts = GetAllCalendarItems();
foreach (var item in appts)
{
Label label = new Label();
label.Text = item.Title + " between" + item.Start + " and " + item.End;
label.Click += new EventHandler(label_Click);
flowLayoutPanel1.Controls.Add(label);
}
...
private void label_Click(object sender, EventArgs e)
{
// This is wrong - what goes here??
((Label)sender).ForeColor = Color.Red;
}
Dynamically laying out Windows Forms is a huge pain. I don't have time to code this but the steps are:
Add FlowLayoutPanel to form as a container
Look through your Appointments and create label controls for each
Add the label controls to the panel's Controls collection
Assign an OnClick handler to each label control to toggle the color
Don't bother with databinding for this.
If you haven't changed the label's initial color from the default, this will toggle it:
private void label1_Click(object sender, EventArgs e)
{
var lbl = (Label)sender;
var toggle = lbl.ForeColor == SystemColors.ControlText;
lbl.ForeColor = toggle ? Color.Red : SystemColors.ControlText;
}
You could add each text field as a member of a Label or List view item. Then handle the "OnClick" or "SelectedIndexChanged" event. To create an an OnClick event handler double click on the control in the design view. Edit the handler like this:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.SelectedItems[0].ForeColor = Color.Red;
}
If you are unsure about event handlers, don't be put off they are quite easy, just look them up here perhaps. If the list view is not what you are looking for, try the same approach on a different control.
I guess I can't comment on Jamie's answer, but raklos you can programmatically add the OnClick method by doing:
label.Click += new EventHandler(label_Click);
Visual Studio should auto-generate the stubs for you when you start typing that out.
Something like this could get you started:
private void label_Click(object sender, EventArgs e) { ToggleTextColor((Label)sender); }
private void ToggleTextColor(Control control)
{
var currentColor = control.ForeColor;
control.ForeColor = currentColor == Color.Red ? Color.Black : Color.Red;
}
You can cheat and make create it in a WebBrowserControl.
You will be in familiar ground.
Use ObjectforScripting for WeBbrowser <=> winforms communication.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx