I want to write some calendar program. I need buttons in a number of days in a chosen month. I have container called "calendarPanel" and used this method to add the buttons
public void CreateCalendar(DateTime dt)
{
int numberOfDays = DateTime.DaysInMonth(dt.Year, dt.Month);
for (int i = 0; i < numberOfDays; i++)
{
System.Windows.Controls.Button btn = new Button();
calendarPanel.Children.Add(btn);
}
}
It works only added to MainWindow.xaml.cs, but I want to use that method from an instance of my class CalendarManager.cs. My problem is that I don't know how (is it possible anyway?) to reference to my panel in MainWindow from outer class (but still in the same namespace). I cannot use
calendarPanel.Children.Add(btn);
Because it doesn't see/know about "calendarPanel" element.
Related
I need to use a User Control many times in the same page.aspx
I am working with visual studio 2012 and asp.net 4.0 I trying to do many graph that receives different parameter each one.
I have an user control.ascx that has code behind control.ascx.cs with a function
public void myTask(string myParameter)
I have a page.aspx with a panel
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
I had a code behind page.aspx.cs with
protected void Page_Load(object sender, EventArgs e)
{
UserControl cuc = (UserControl)LoadControl("~/cmmon/userCntrol/myusercontrol.ascx");
Panel1.Controls.Add(cuc);
My code shows nice for just one graphic.
I am willing to create many instances for my user Control and call each function from the page.aspx int the behind code page.aspx.cs like
myTask(myParameter);
with different parameter for each one. How do I do?
You can create multiple in a loop. Make sure to give each an unique ID.
for (int i = 0; i < 5; i++)
{
WebUserControl1 cuc = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
cuc.ID = "Control" + i;
Panel1.Controls.Add(cuc);
}
Then you can locate the control somewhere else in the code based on that ID.
WebUserControl1 uc = Panel1.FindControl("Control3") as WebUserControl1;
I found and resolve two issues: 1) how to send parameters to a control object. and 2) how to reference a control in another c# page.
1) To send parameters to a control object I have to define variables to accept the values of the parameters, it is done in the object control by:
using System.Web.UI.WebControls;
public partial class myControl : System.Web.UI.UserControl
{
public string variable { get; set; }
public void start(string parameter)
{
variable = parameter;
}
}
2) To reference a control in another c# page. in the page directive should be:
<%# Reference Control="~/cmmon/userCntrol/myControl.ascx" %>
Then, as VDWWD answer, in your code behind you can include an object like:
for (int i = 0; i < 5; i++)
{
WebUserControl1 cuc = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
cuc.ID = "Control" + i;
Panel1.Controls.Add(cuc);
}
And call the function as I needed:
cuc.myTask(myParameter);
Then:
for (int i = 0; i < 5; i++)
{
WebUserControl1 cuc = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
cuc.ID = "Control" + i;
Panel1.Controls.Add(cuc);
cuc.myTask(myParameter);
}
I think it is a small problem but I can't find my mistake.
I create a Form called Inventurbeleg which contains a ComboBox called cbProduktBox.
With a Controller-Class I create an Object of the Form. Now I want to add Items with the create-Methode.
public static void buttonCreate()
{
inventurbeleg = new Inventurbeleg();
create();
inventurbeleg.Show();
}
My ComboBox gets Items from an array:
public static void create()
{
inventurbeleg.cbProduktBox = new ComboBox();
for (int j = 0; j < Program.arrayMatNr.GetLength(0); j++)
{
String item = Program.arrayMatNr[j, 1];
inventurbeleg.cbProduktBox.Items.Add(item);
}
}
This works correctly, cbProduktBox contains all Items. My Problem is, that the Items arn't shown at my Form. There is an empty comboBox.
You can't do it like that, take a look at this line:
inventurbeleg.cbProduktBox = new ComboBox();
You're creating a new combobox, and when the form loads, the cbProduktBox will initialize again and the changes will be gone
Maybe you can move the create method inside the new form, so when the form loads, call the create method.
Im making a program with many many pages... and in my design, the buttons will eventually get stacked up, so its getting harder and harder to work the more there are.
This question is a clone of this topic.
However, i didn't really get the answer since they were talking about xaml and wpf.
I've also tried to make multiple windows forms, hide and show them to split it up.
But when i hide and show a wndow, its very very easily to see the GUI fading in and out which looks ugly.
I want an instant hide/show function so it looks like its just 1 program with 1 window and now switching.
So what are the tecniques to make a big windowsforms program more managable?
You may create a couple of UserControls, organize and separate your logic on them. Then you may use String Array to store your control names and iterate througth items to display apropriate view. The form may have Panel as container and Dock Style defined as Fill. The simplefied code will look like:
public class MyContainer:Control
{
public MyContainer(string szControlName, UserControl nControl)
{
UserControlName = szControlName; MyControl = nControl;
}
public string UserControlName { get; set; }
public UserControl MyControl { get; set; }
}
In main form:
public List<MyContainer> MyNavigationArray;
public void InitArray()
{
MyNavigationArray = new List<MyContainer>(MyFormPageCount);
for (int i = 0; i < MyFormPageCount; i++)
{
MyNavigationArray.Add(new MyContainer(TheNameOfUserControl, new PredefinedUserControl()));
}
}
private void NextButton_click(object sender, EventArgs e)
{
MyContainer mYcn = this.panel1.Controls[0] as MyContainer;
int nCurPos = MyNavigationArray.IndexOf(mYcn);
if (nCurPos < MyNavigationArray.Count)
{
panel1.Controls.Clear();
MyContainer c = MyNavigationArray[nCurPos + 1];
panel1.Controls.Add(c);
c.Dock = DockStyle.Fill;
}
}
All information stored inside user controls will be in your array and can be used later.
Hope this helped.
I have a windows form application with a ComboBox on it and I have some strings in the box. I need to know how when I select one of the strings and press my create button, how can i make that name show up on another windows form application in the panel I created.
Here is the code for adding a customer
public partial class AddOrderForm : Form
{
private SalesForm parent;
public AddOrderForm(SalesForm s)
{
InitializeComponent();
parent = s;
Customer[] allCusts = parent.data.getAllCustomers();
for (int i = 0; i < allCusts.Length; i++)
{
Text = allCusts[i].getName();
newCustomerDropDown.Items.Add(Text);
newCustomerDropDown.Text = Text;
newCustomerDropDown.SelectedIndex = 0;
}
now when i click the create order button I want the information above to be labeled on my other windows form application.
private void newOrderButton_Click(object sender, EventArgs e)
{
//get the info from the text boxes
int Index = newCustomerDropDown.SelectedIndex;
Customer newCustomer = parent.data.getCustomerAtIndex(Index);
//make a new order that holds that info
Order brandSpankingNewOrder = new Order(newCustomer);
//add the order to the data manager
parent.data.addOrder(brandSpankingNewOrder);
//tell daddy to reload his orders
parent.loadOrders();
//close myself
this.Dispose();
}
The context is not very clear to me, but if I got it right, you open an instance of AddOrderForm from an instance of SalesForm, and when you click newOrderButton you want to update something on SalesForm with data from AddOrderForm.
If this is the case, there are many ways to obtain it, but maybe the one that requires the fewer changes to your code is this one (even if I don't like it too much).
Make the controls you need to modify in SalesForm public or at least internal (look at the Modifiers property in the Design section of the properties for the controls). This will allow you to write something like this (supposing customerTxt is a TextBox in SalesForm):
parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;
I have a TabControl that starts with three TabPages in it. On the first tab there is a NumericUpDown (spinner) which displays the number of tabs and allows a user to add up to 10 extra tabs. Once they add more than about 5 or 6 it goes beyond the width of the form and the rest of the tabs are accessible by a couple of left/right arrows at the top. When going all the way to the right and then using the spinner to go back down to 0 (removing all the extra tabs and leave the starting three) it removes all tabs from the top of the pane and only by setting the spinner back to 1 does it refresh and display all 4 (3 from the start plus the 1 from the spinner).
I have tried several commbinations of
Application.DoEvents()
this.Refresh()
this.Invalidate()
this.Update()
but nothing seems to work. can anybody suggest a reason why it is not updating/refreshing?
public partial class Form1 : Form
{
TabPage[] tabs;
public Form1()
{
InitializeComponent();
tabs = new TabPage[tabControl1.Controls.Count];
tabs[0] = tabPage1;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int numTabs = tabControl1.Controls.Count;
decimal spinnerValue = numericUpDown1.Value;
if (numTabs < spinnerValue) //add a tab
{
TabPage[] newTabs = new TabPage[(int)spinnerValue];
for (int i = 0; i < numTabs; i++)
{
newTabs[i] = tabs[i];
}
TabPage tab = new TabPage("Tab " + numTabs);
newTabs[(int)spinnerValue-1] = tab;
tabControl1.Controls.Add(tab);
tabs = newTabs;
}
else //remove a tab
{
TabPage[] newTabs = new TabPage[(int)spinnerValue];
for (int i = 0; i < spinnerValue; i++)
{
newTabs[i] = tabs[i];
}
tabControl1.Controls.Remove(tabs[(int)spinnerValue]);
tabs = newTabs;
}
}
}
Without seeing any code or knowing what type of project this is winforms, WPF, ASP.NET etc..
it's hard to give a definite answer, I am going to assume that this is WinForms
I'm not sure if you can. The following is a quote from MSDN:
"Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."
However, instead of having the update code get the values from the controls directly, maybe you could create a class that could hold the Data you use to populate the controls and then when the update code is called it asks the class for the value and the class checks if the control is loaded and otherwise it gets the value from the Data instead.