When I currently save, it saves the checkboxes (but only the color and it's contents, but not if it was checked for some reason), but of course, since I only have 1 setting for each of the properties of the check box (like color, if it is checked(which doesn't work), and it's content), the last checkbox to be added by the user overwrites every other checkbox.
The other problem is that it only creates 1 checkbox, and I don't know how to create the exact number of checkboxes the user has created, with code.
Yesterday I almost spent the whole day looking for solutions, and today I have only spent some hours, maybe two.
Properties.Settings.Default.CheckBoxes is a bool value, to tell it if it's checked or unchecked. Properties.Settings.Default.CheckBoxText
I started to code in c# some days ago, so my code is probably sloppy. Here is my code:
private void DoneBtn_Click(object sender, RoutedEventArgs e)
{
playSimpleSound();
TextRange descText = new TextRange(descTextBox.Document.ContentStart, descTextBox.Document.ContentEnd);
CheckBox c = new CheckBox();
c.Name = "CheckBoxs";
c.IsChecked = false;
c.Content = TitleTextBox.Text + "\n" + descText.Text;
c.Foreground = new SolidColorBrush(Colors.SkyBlue);
checkBoxPanel.Children.Add(c);
checkBoxScroll.Content = checkBoxPanel;
CreateTaskPanel.Visibility = Visibility.Collapsed;
Properties.Settings.Default.CheckBoxText = (string)c.Content;
}
private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
{
if (Properties.Settings.Default.Value == true)
{
CheckBox c = new CheckBox();
c.IsChecked = Properties.Settings.Default.CheckBoxes;
c.Content = Properties.Settings.Default.CheckBoxText;
c.Foreground = Properties.Settings.Default.CheckBoxColor;
checkBoxPanel.Children.Add(c);
}
Properties.Settings.Default.Value = true;
}
private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
Related
I have a question!
I'm currently making a notepad app and everything was going well until now.
The way it's set up:
There's a panel with the menu buttons (File, Edit, Themes, & Format) and a panel at the bottom with all of the options under each tab. Both the menu button and sub-buttons are labels.
When you click a menu button, the bottom panel will show the buttons categorized under that section by making all labels there invisible, and then making the appropriate ones visible.
Everything seems to work fine except for the 2 buttons under Format (Font & Font Color). They show up, but do not do anything when clicked. Below is the code for it.
private void FontButton_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if(fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fd.Font;
}
}
private void FontColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if(cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = cd.Color;
}
}
private void Label4_Click(object sender, EventArgs e)
{
Config();
FontButton.Visible = true;
FontColor.Visible = true;
}
private void Config()
{
New.Visible = false;
Open.Visible = false;
Save.Visible = false;
Light.Visible = false;
Dark.Visible = false;
FontButton.Visible = false;
FontColor.Visible = false;
Undo.Visible = false;
Cut.Visible = false;
Copy.Visible = false;
Paste.Visible = false;
}
Is the code wrong? Is it because the labels are stacked?
If someone could lend a helping hand, that'd be great! Thanks in advance.
I have WPF application that holds some buttons. One button is named ChangeTime so when clicked, dialog opens and there is hh/mm/ss what you can change. I want to store picked time in variable.
My problem is that I only want store that new time if user really changed time. Currently when user presses button, event triggers and new value is stored. Also it shouldn't store new value when dialog is closed without touching Time field.
Essentially I want to change BtnWasClicked to true when time is actually changed. Poor choice of variable name indeed.
Any way to prevent it?
private DateTimePicker _timePortionDateTimePicker;
private DateTime _pickedTime;
private bool btnWasClicked = false;
private bool timeChanged = false;
private void TimeIsChanged(object sender, EventArgs e)
{
timeChanged = true;
if (timeChanged)
{
btnWasClicked = true;
}
}
private void BtnChangeTime_OnClick(object sender, EventArgs e)
{
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
_timePortionDateTimePicker.ValueChanged += new
EventHandler(TimeIsChanged);
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
_pickedTime = _timePortionDateTimePicker.Value;
}
You don't actually need TimeIsChanged. You can just check whether _timePortionDateTimePicker.Value is different from _pickedTime in BtnChangeTime_OnClick.
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
// start with the previous chosen time
_timePortionDateTimePicker.Value = _pickedTime;
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
if (_pickedTime != _timePortionDateTimePicker.Value) {
// time has changed, assign to _pickedTime and other variables
_pickedTime != _timePortionDateTimePicker.Value;
timeChanged = true;
btnWasClicked = true;
} // otherwise do nothing
I have to show the grid selected row value into textboxes.I'm using this code, but it's not working. Any help will be appreciated.
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.SelectedRows[0].Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.SelectedRows[1].Cells[1].Value.ToString();
txtBoxName.Text = CRUD.SelectedRows[2].Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.SelectedRows[3].Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.SelectedRows[4].Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.SelectedRows[5].Cells[5].Value.ToString();
}
You're indexing your selected rows. if you have less than 6 selected rows, then you will go out of bounds. You probably want to fetch data from only one row. Check if only one row is selected and then use index 0. Make sure you set CRUD.MultiSelect = false
Alternatively use CRUD.CurrentRow which will only ever get you one row.
Form.Designer.cs:
this.CRUD.SelectionChanged += new System.EventHandler(this.CRUD_SelectionChanged);
Form.cs:
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.CurrentRow.Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.CurrentRow.Cells[1].Value.ToString();
txtBoxName.Text = CRUD.CurrentRow.Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.CurrentRow.Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.CurrentRow.Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.CurrentRow.Cells[5].Value.ToString();
}
I am trying to build an app, where user can select category and according to it displays its sub categories , these sub categories are buttons, which are dynamically created.
Now, as buttons are dynamically created so I am confuse how to write code under button_click event as I dont know how many subcategories are there.
So is there any way I can execute click event of a particular button , so that I can execute certain commands?
EDITED
This is the code that i tried
Button btnDynamicButton = new Button();
private void btnclick_Click(object sender, EventArgs e)
{
label2.Text = btnDynamicButton.Text;
}
private void btnappetizer_Click(object sender, EventArgs e)
{
groupBox2.Visible =false;
DataTable dt = new DataTable();
dt = itemmasterbl.SelectallrecordFromtblItem(btnappetizer.Text);
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = "Appetizer" + DynamicButtonCount;
Button btnDynamicButton1 = new Button();
btnDynamicButton1.Name = name;
btnDynamicButton1.Text = name;
btnDynamicButton1.Size =
new System.Drawing.Size(150, 30);
btnDynamicButton1.Location =
new System.Drawing.Point(180, DynamicButtonCount * 30);
btnDynamicButton1.Click +=new EventHandler(btnclick_Click);<br>
Controls.Add(btnDynamicButton1);
DynamicButtonCount++;
btnDynamicButton = btnDynamicButton1;
}
}
Once I do this it creates three buttons according to number of values in itemmaster DB under appetizer, but once I click on any of the three buttons the label displays only last buttons text,because in last line I have :
btnDynamicButton = btnDynamicButton1;
Which will last buttons infos,but rather I want which ever button I press, label should display respective text. How can I achieve this.
you can put all your logic into one handler:
System.Windows.Forms.Button b = new System.Windows.Forms.Button();
b.Click += new EventHandler(b_Click);
//finally insert the button where it needs to be inserted.
...
void b_Click(object sender, EventArgs e)
{
MessageBox.Show(((System.Windows.Forms.Button)sender).Name + " clicked");
}
To your edit:
You are storing the reference for your button(s) inside the Field btnDynamicButton. Hence it always gets overwritten with the latest button you have created. You should not reference the button by using a field. The sender parameter of the click-handler contains the button element that has been clicked. See the code above: Simple cast sender to Button and you know which button has been clicked:
private void btnclick_Click(object sender, EventArgs e)
{
Button btn = (Button)sender
label2.Text = btn.Text;
}
Problem:
I have a value in a database table. This value can either contain a number, or null. If its null I would like to show one group of controls. If its not null I would like to show another group of controls.
Previous Attempts:
I have tried creating the controls in the code behind depending on the value of the database. This worked. However, on postback I get a null reference exception. The control doesn't exist on postback because the page is stateless. I'm building the controls in the page_load handler (depending on the value of the table column). Since I'm creating the controls in the page_load shouldn't they exist on postback?
I also tried recreating the controls in the event handler for the button. I get a "theres already a control with this id" exception (presumably because I already created it in the page_load method).
I read a few posts about how I have to store the controls in a session. This seems like more work than it should be.
Questions:
Am I going about this the wrong way? This seems like it should have been simple but is turning into a mess.
If this is the correct way to do this, Where do I add the session information? I've been reading other posts and I'm kind of lost
Code:
int bookId;
string empName;
protected void Page_Load(object sender, EventArgs e)
{
if(int.TryParse(Request.QueryString["id"], out bookId))
{
//This is where the value in the database comes into play. If its null Book.GetCopyOwner
// returns a string with length 0
empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
if (empName.Trim().Length > 0)
{
CreateReturnControls();
}
else
{
CreateCheckoutControls();
}
}
}
protected void ReturnButton_Click(object sender, EventArgs e)
{
}
protected void CheckOut_Click(object sender, EventArgs e)
{
int bookId;
if (int.TryParse(Request.QueryString["id"], out bookId))
{
TextBox userId = (TextBox)this.Page.FindControl("UserId");
//WHEN I TRY TO USE THE TEXTBOX userId HERE, I GET NULL REFERENCE EXCEPTION
BookCopyStatusNode.Controls.Clear();
CreateReturnControls();
}
}
protected void CopyUpdate_Click(object sender, EventArgs e)
{
}
private void CreateCheckoutControls()
{
TextBox userId = new TextBox();
//userId.Text = "Enter Employee Number";
//userId.Attributes.Add("onclick", "this.value=''; this.onclick=null");
userId.ID = "UserId";
Button checkOut = new Button();
checkOut.Text = "Check Out";
checkOut.Click += new EventHandler(CheckOut_Click);
TableCell firstCell = new TableCell();
firstCell.Controls.Add(userId);
TableCell secondCell = new TableCell();
secondCell.Controls.Add(checkOut);
BookCopyStatusNode.Controls.Add(firstCell);
BookCopyStatusNode.Controls.Add(secondCell);
}
private void CreateReturnControls()
{
Label userMessage = new Label();
userMessage.Text = empName + " has this book checked out.";
Button returnButton = new Button();
returnButton.Text = "Return it";
returnButton.Click += new EventHandler(ReturnButton_Click);
TableCell firstCell = new TableCell();
firstCell.Controls.Add(userMessage);
TableCell secondCell = new TableCell();
secondCell.Controls.Add(returnButton);
BookCopyStatusNode.Controls.Add(firstCell);
BookCopyStatusNode.Controls.Add(secondCell);
}
It looks like you're creating a static set of controls based on the database value. Why not simply have 2 Panels that contain the controls you want and simply set their visibility to true or false:
if (!Page.IsPostBack)
{
if (int.TryParse(Request.QueryString["id"], out bookId))
{
empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
var display = (empName.Trim().Length > 0);
panelReturnControls.Visible = display;
panelCheckoutControls.Visible = !display;
}
}