I have Two buttons in my Wpf UI named as Add,Remove. My requirement is, if i click add button it has to Add new row with two columns. If i click remove button, it has to remove the Last Inserted Row. I am using Grid for this.
I want to remove the Last Row from the Grid. I am able to add rows with 2 columns. I have the code for Remove. But it says some error. I donno how to remove the last inserted row entirely if i click Remove button. I have the following code.
public int count = 1;
private void btn_add_Click(object sender, RoutedEventArgs e)
{
//Creating Rows..
RowDefinition row0 = new RowDefinition();
row0.Height = new GridLength(30);
grid1.RowDefinitions.Add(row0);
//Creating columns..
ColumnDefinition col0 = new ColumnDefinition();
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
col0.Width = new GridLength(100);
col1.Width = new GridLength(100);
grid1.ColumnDefinitions.Add(col0);
grid1.ColumnDefinitions.Add(col1);
int i = count;
ComboBox cmb = new ComboBox();
cmb.Items.Add("add");
cmb.Items.Add("remove");
Grid.SetRow(cmb, i);
Grid.SetColumn(cmb, 0);
grid1.Children.Add(cmb);
TextBox txt = new TextBox();
Grid.SetRow(txt, i);
Grid.SetColumn(txt, 1);
grid1.Children.Add(txt);
count++;
}
private void btn_remove_Click(object sender, RoutedEventArgs e)
{
grid1.RowDefinitions.RemoveAt(count);
count--;
}
You normally don't need an additional counter for removing the last row, just use:
grid1.RowDefinitions.RemoveAt(grid1.RowDefinitions.Count - 1);
Related
I want to get all items from CheckedBoxList and add it to a new Form so for every checked item I want a new Label with checkedItem name and a TextBox. So far I'm doing that but when I open the form I got no results at all. I don't know how to get the checked item name and I'm doing this:
labels[i].Text = i.ToString();
private void Button4_Click(object sender, EventArgs e)
{
testForm = new Test();
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;
for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
{
txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
labels = new Label[checkedListBox1.CheckedIndices.Count];
labels[i] = new Label();
labels[i].Text = i.ToString();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);
txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}
Controls.Add(tableLayoutPanel);
testForm.ShowDialog();
}
Any suggestions?
Thank you, for the invested time.
The solution was to change the code to:
testForm.Controls.Add(tableLayoutPanel);
I'm trying to generate a grid of data programmatically.
private void WorkPackageSearchButton_Click(object sender, RoutedEventArgs e)
{
EWP[] workPackages = SqlDataLayer.getSearchedWorkPackages(WorkPackageSearchBox.Text);
//declare variables
RowDefinition RowDef;
StackPanel StackP;
TextBlock TextB;
TextBlock TextC;
for (int i = 0; i < workPackages.Length; i++)
{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(30);
//Add row definition to Grid
WorkPackageResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
TextB = new TextBlock();
TextB.Text = workPackages[i].EWPStatus;
//TextB.Width = 75;
TextC = new TextBlock();
TextC.Text = workPackages[i].EWPCode;
//TextC.Width = 175;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
StackP.SetValue(Grid.RowProperty, i);
//add your control to the stackpanel
StackP.Children.Add(TextB);
StackP.Children.Add(TextC);
//add the stackpanel to the grid
WorkPackageResults.Children.Add(StackP);
}
}
The result is the data being added on top of each other:
How do I get each of the textblocks per iteration of the loop to be next to each other in a single row?
You have to set the Orientation of the StackPanel you're using to Horizontal
StackP = new StackPanel();
StackP.Orientation = Orientation.Horizontal;
the default Orientation of a StackPanel is Vertical, so it displays the items rows based.
Msdn Link and Examples
I am working in silverlight and under a very strange situation that i need to implement my own SelectionChangedEventHandler handler function .The reason to do so is:
I have my situation like this :
private static Grid GenerateList(Parameter param, int LoopCount, Grid g)
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
scrollViewer.Content = lines;
Grid.SetColumn(scrollViewer, 1);
Grid.SetRow(scrollViewer, LoopCount);
childGrid.Children.Add(scrollViewer);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
// lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged(lines, txtblk1ShowStatus));
lines.SelectedIndex = lines.Items.Count - 1;
Grid.SetColumn(txtblk1ShowStatus, 2);
Grid.SetRow(txtblk1ShowStatus, LoopCount);
childGrid.Children.Add(txtblk1ShowStatus);
g.Children.Add(childGrid);
return (g);
}
static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("clist _SelectionChanged1");
//The problem is line below because i am "lines" and "txtblk1ShowStatus" are not in the
//scope of this function and i cannot declare them globally.
txtblk1ShowStatus.Text = lines[(sender as ListBox).SelectedIndex];
}
Here you can see that i have no access of of "lines" and "txtblk1ShowStatus" in the List_SelectionChanged(object sender, SelectionChangedEventArgs e) function. And i cannot declate the button and list globally because this function GenerateList(...) will be reused and it's just coded in c# (no xaml used).Please let me know how to do that and also explain how to do that if you have another way to do but please explain your code with detail
I think a lambda expression will help you solve your issues (you might need to edit it):
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.
lines.SelectionChanged += (o,e) => {
MessageBox.Show("clist _SelectionChanged1");
txtblk1ShowStatus.Text = lines.SelectedItem.ToString();
};
lines.SelectedIndex = lines.Items.Count - 1;
I have a Button in wpf. if i click the button, it will create a row with 3 columns in grid. 1st column has ComboBox with 3 items. Item1,Item2 and Item3. 2nd column has no Control.3rd column has Textbox control.
If combobox item value checked is Equal to item3, then i need to add a Textbox control to the 2nd column of the grid to the same row. i have the code to add those controls. but i dont know how to add the Textbox for the particular checkboxItem3 row in run time.
note: checkbox value can check at anytime. even if i check at run time, it has to create new textbox for the respective row's 2nd column.
this is my code.
public int count = 1;
private void btn_add_Click(object sender, RoutedEventArgs e)
{
//Creating Rows..
RowDefinition row0 = new RowDefinition();
row0.Height = new GridLength(30);
grid2.RowDefinitions.Add(row0);
//Creating columns..
ColumnDefinition col0 = new ColumnDefinition();
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
col0.Width = new GridLength(100);
col1.Width = new GridLength(100);
col2.Width = new GridLength(100);
grid2.ColumnDefinitions.Add(col0);
grid2.ColumnDefinitions.Add(col1);
grid2.ColumnDefinitions.Add(col2);
int i = count;
//1st Column Combobox
ComboBox cmb = new ComboBox();
cmb.Items.Add("item1");
cmb.Items.Add("item2");
cmb.Items.Add("item3");
Grid.SetRow(cmb, i);
Grid.SetColumn(cmb, 0);
grid2.Children.Add(cmb);
//3rd column Textbox
TextBox txt = new TextBox();
Grid.SetRow(txt, i);
Grid.SetColumn(txt, 2);
grid2.Children.Add(txt);
count++;
}
You can use ComboBox's Tag property to store row information :
ComboBox cmb = new ComboBox();
......
......
//give the combobox a tag so you can identify...
//the row number of this combobox later
cmb.Tag = i;
//attach event handler
cmb.SelectionChanged += cmb_SelectionChanged;
Then in the event handler method, you can easily add new TextBox in the correct Grid row, for example :
void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems[0].ToString() == "item3")
{
var txt = new TextBox();
var row = (int)((ComboBox)sender).Tag;
Grid.SetRow(txt, i);
Grid.SetColumn(txt, 1);
grid2.Children.Add(txt);
}
}
this is my very first program in WP7, so i have some issues.
I'm trying to populate a grid with an list of objects that has been bought by another method. Here what i made so far:
public partial class MainPage : PhoneApplicationPage
{
private List<Row> lsResult;
private Grid myGrid = new Grid();
private int i = 0;
// Constructor
public MainPage()
{
InitializeComponent();
ColumnDefinition colData = new ColumnDefinition();
ColumnDefinition colOcorrencia = new ColumnDefinition();
ColumnDefinition colSituacao = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colData);
myGrid.ColumnDefinitions.Add(colOcorrencia);
myGrid.ColumnDefinitions.Add(colSituacao);
myGrid.ShowGridLines = true;
SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
myGrid.Background = myBrush;
gridResult = myGrid;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
i = 0;
lsResult = null;
lsResult = Rastrear.Busca(txtNumber.Text);
foreach (Row r in lsResult)
{
RowDefinition rNewRow = new RowDefinition();
myGrid.RowDefinitions.Add(rNewRow);
TextBlock lblData = new TextBlock();
lblData.Text = r.Data.ToString();
lblData.HorizontalAlignment = HorizontalAlignment.Center;
lblData.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumnSpan(lblData, 1);
Grid.SetRow(lblData, i);
TextBlock lblOcorrencia = new TextBlock();
lblOcorrencia.Text = r.Ocorrencia.ToString() ;
Grid.SetColumnSpan(lblOcorrencia, 2);
Grid.SetRow(lblOcorrencia, i);
TextBlock lblSituacao = new TextBlock();
lblSituacao.Text = r.Situacao.ToString();
Grid.SetColumnSpan(lblSituacao, 3);
Grid.SetRow(lblSituacao, i);
i++;
myGrid.Children.Add(lblData);
myGrid.Children.Add(lblOcorrencia);
myGrid.Children.Add(lblSituacao);
}
gridResult = myGrid;
}
}
The method Buscar() is returning the list as it should, but when i click on the button it doesn't do anything, not even the paint to white on the constructor happens actually.
thanks in advance
You're doing some pretty shady stuff in your code behind that i'm used to seeing and should be done in xaml. Change this
gridResult = myGrid;
to
gridResult.Children.Add(myGrid);
I don't think you can just change references of static UI elements like that.