I have a code in WindowAfterLogin.xaml:
<TextBlock x:Name="In_Time" Foreground="#FF55534F"
FontSize="71.312" FontFamily="HelveticaNeueCyr"
Height="79.45" LineStackingStrategy="BlockLineHeight"
Canvas.Left="2.724" LineHeight="71.312"
TextAlignment="Center" TextWrapping="Wrap"
Canvas.Top="69.985" Width="231.581" Text="09:00" />
And then I want to change the value of that TextBlock in WindowAfterLogin.xaml.cs, so I've done this :
MainWindow objMainWindow = new MainWindow();
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objMainWindow.Show();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close();
But, when I press F5 button (Compile), it didn't change. Where is the problem here?
Are you creating a separate instance of WindowAfterLogin ? If you want to do that, you will have to Show() the new instance. Try this:
WindowAfterLogin objAfterLogin = new WindowAfterLogin();
objAfterLoginShow();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
//Close(); //Close any other window that needs to be closed..
If you wanted the In_Time.Text to change for the current instance, (assuming that has been showed or is visible currrently), you can try this perhaps in the constructor or your method that Initializes the WindowAfterLogin :
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
As a simplification to bit answer, try to write something like this instead of your code
MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close(); //Not sure if You need it. Try to comment this line to be able to notice textblock text changes
Also I think You need
DateTime.Now.ToString("hh:mm")
instead of
DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()
Related
I'm trying to get the contents of dynamically created TextBlock and dynamically created RichTextboxes (they sit side by side) into the clipboard in c# + wpf, however, I'm not able to do so. I've search all over google to no avail, the latest code I came up with is
StringBuilder clipboard = new StringBuilder();
String rtb = scrlPanel.Children.OfType<RichTextBox>().ToString();
//List<RichTextBox> rtb = scrlPanel.Children.OfType<RichTextBox>().;
foreach(TextBlock txtb in scrlPanel.Children.OfType<TextBlock>())
{
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtb.ToString() + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
but it doesn't work, the codes copies the TextBlocks just fine but the RichTextBoxes content display" "System.Linq.Enumerable+d__aa`1[System.Windows.Controls.RichTextBox]"
Any help or pointers is greatly appreciated.
Thanks,
You are copying the from the ToString() - method, which by default shows the typename.
You should do it like this:
StringBuilder clipboard = new StringBuilder();
List<RichTextBox> rtbs = scrlPanel.Children.OfType<RichTextBox>().ToList();
List<TextBlock> texts = scrlPanel.Children.OfType<TextBlock>().ToList();
foreach(TextBlock txtb in texts)
{
RichTextBox rtb = rtbs[texts.indexOf(txtb)];
string rtbtext = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtbtext + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
That is, if you just as many TextBoxes as RichTextBoxes and they have the same order.
PD: There's probably better ways of doing this, but this would be a quick fix.
I am trying to set my ComboBoxes so that the user can either choose from the list or set their own value (the combo box is for a custom resolution so there will be default values or they can give their own).
I am trying to make it so that if their value is incorrect (below 0 or not an Int) then it shows a tooltip and prevents it from losing focus. Here is my code:
private void cmbX_Graphics_Width_LostFocus(object sender, EventArgs e)
{
int i = 0, width = 0;
TLQAShared._debug("Lost Focus Fired");
for (i = 0; i < cmbX_Graphics_Width.Items.Count; i++)
{
if (cmbX_Graphics_Width.Text.Equals(cmbX_Graphics_Width.Items[i].ToString()))
{
Properties.X.Default.Graphics_Width = int.Parse(cmbX_Graphics_Width.Items[i].ToString());
TLQAShared._debug("FOUND!");
return;
}
TLQAShared._debug("FOR: " + i.ToString() + "/" + (cmbX_Graphics_Width.Items.Count - 1).ToString() + ": " + cmbX_Graphics_Width.SelectedText + ":" + cmbX_Graphics_Width.Items[i].ToString());
}
TLQAShared._debug("Not true: '" + cmbX_Graphics_Width.Text + "'");
if (int.TryParse(cmbX_Graphics_Width.Text.ToString(), out width))
{
TLQAShared._debug("TryParse: true");
Properties.X.Default.Graphics_Width = width;
}
else
{
tt.SetToolTip(cmbX_Graphics_Width, "You must supply a valid integer");
this.ActiveControl = cmbX_Graphics_Width;
TLQAShared._debug("TryParse invalid.");
}
}
However if the control loses focus, this code gets executed twice, first time it stops at this part:
TLQAShared._debug("Not true: '" + cmbX_Graphics_Width.Text + "'");
Then does it again but executes the entire code, but does not prevent the control from losing focus.
Two questions I have:
Firstly: Is this the best practice and if not what should I do?
Secondly: If it is best practice, how would I fix it?
use combobox1.Select(); for focus in combobox.
I don't think this is a good practice. I would do it like this:
Create a function to check if the input is valid (int > 0)
Call this function when the user attempts to enter the input
If the input isn't valid call combobox.focus()
I am using VS2010 (C#) and I want to create a button that creates a new tab. I already have a tabControl container created. How would I do this?
Moved here from Brandon's solution in the question:
string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
How to open a new window not in the same new window?
The following code can open a new window However after pressing button again
the content will be replaced. How to open the next or next next content in a new window not in the same new window
string strPop = "<script language='javascript'>" +
"window.open('viewpage.aspx?type=3&start=" + txtStartDate.Text + "&end=" + txtEndDate.Text + "','Report');" +
"</script>";
Page.RegisterStartupScript("Pop", strPop);
Replace the window name 'Report' with '_blank' or generate a unique window name instead of the static 'Report' name.
Always give the new windows new names and they'll open in separate windows
I have to add details of my file into a multiline textbox. But all the details are getting added in a single line in the text box and not in a vertical sequence. I used Environment.NewLine and also used "\r\n", but it's not of any help. I have ticked the multiline text box in a Windows Forms form and also set it to true but to no avail.
My line of code is like this:
m_Txt.Multiline = true;
m_Txt.Text = fileInfo.m_Title + "\r\n" +
fileInfo.m_Identifier + Environment.NewLine +
fileInfo.m_TotalTime;
A cleaner answer is:
Assuming txtStatus is a textbox:
txtStatus.Multiline = True;
txtStatus.Clear();
txtStatus.Text += "Line 1" + Environment.NewLine;
txtStatus.Text += "Line 2" + Environment.NewLine;
Using the built in enumeration means cleaner code.
Shift+Enter
In the Visual Studio resource editor, you can hit "Shift + Enter"
to create a new line, as doing something like "\r\n" will get escaped
out. You will also need to increase the cell height to see both
lines as it does not auto-size.
If you're doing it programatically, append the new line to m_Txt.Lines, which is a string[].
m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime};
Not sure why your code would not work unless something else is going on.
I just created a WinForms project using C#, added a textbox, set it multiline and added the following code - works a charm.
textBox1.Text = "a\r\nb";
I just wrote this code, seems to be working fine.
public void setComments(String comments)
{
String[] aux;
if(comments.Contains('\n')) //Multiple lines comments
{
aux = comments.Split('\n');
for (int i = 0; i < aux.Length; i++)
this.textBoxComments.Text += aux[i] + Environment.NewLine;
}
else //One line comments
this.textBoxComments.Text = comments;
}