I don't know if I'm just not understanding what I've found so far, or if it really is too complex to apply to such a simple idea as it seems. I'm trying to bind a button's height and width to variables that are stored in user settings. I can't seem to get this to work, or rather I simply don't how to, as in what commands to use. The issue lies in not knowing what to put in the Binding field of the xaml. If anyone could point to a guide that involves just this, could explain what to do I would be very appreciative.
Edit: I've solved the problem of binding the variable, it now saves to the User setting file when it should. Now I'm having an issue with the value stored in user setting beig overwritten every time the program loads with the default value. I am running this through VS debug menu selection, so I suppose the issue could lie there, but I've tried publishing it and running and still getting the same results. Any ideas?
Assuming by 'User Settings' you mean the built-in Settings not a custom implementation:
See http://blogs.windowsclient.net/bragi/archive/2008/07/03/using-settings-in-wpf-or-how-to-store-retrieve-window-pos-and-loc.aspx for an example of this - essentially you want to set up TwoWay bindings to Properties.Settings.Default: note that you have to define the settings in advance using the Settings UI, and you have to call Properties.Settings.Default.Save() when the app exits to persist the settings.
I'm posting this answer so that hopefully somebody else can read it and avoid such a ridiculous problem. First off, as far as the initial question, Staurt answered it quite nicely. But my edit above brought up a new but related problem. I ended up fixing it on accident.
The whole purpose of this was that I have a slider bar that adjusts the size of a shortcut button dock. The slider worked, but as I said above it would reset itself every time I reloaded. The issue in this case was that I have the buttons set to resize as the slider moves, so I used the slider_ValueChanged event as you can see here:
private void iconSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
try
{
Properties.Settings.Default.iconHeight = Convert.ToInt32(iconSizeSlider.Value);
Properties.Settings.Default.iconWidth = Convert.ToInt32(iconSizeSlider.Value * 1.3);
Properties.Settings.Default.Save();
//iconWidth.Text = buttonWidth.ToString();
//ButtonRefresh();
}
catch (FormatException)
{
}
}
While trying to use the Run To Cursor part of VS2010, I got tired of having to F11 through a multitude of loading steps, so as a debugging tool I added a bool fullyInitialized flag. This solved the problem completely. Apparently (which I didn't realize before), when the slider was first initialized it considered the value to have changed, so when it ran through the ValueChanged method, it reset everything to default. So adding a simple conditional around the try-catch to check for the fullyInitialized flag solved everything. Hopefully this helps somebody else.
Related
My issue is in the .NET framework using C# to create a simple form application that contains a rich text box (RTB) control.
Briefly the issue I am experiencing is that when trying to clear the contents (.Text) of the RTB, the scroll bar doesn't go away. I would like to know if there is anything inherently wrong with the way I am using the RTB. I apologize, but the site will not allow me to post images yet. So if there is a misunderstanding regarding what "doesn't go away" means, please ask!
So first, I write data to the box using the following code snippet:
// append the new message
this.rtb_receive_0.Text += message;
this.rtb_receive_0.SelectionStart = this.rtb_receive_0.Text.Length;
this.rtb_receive_0.ScrollToCaret();
Later on, I clear the RTB contents (RTB.Text) with the following code:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Refresh();
In the above code I have attempted to fix my problem with the, "Refresh," method. However it does not seem to be doing the job.
When I clear the RTB contents, the scroll bar does not go away... I noticed that if I grab another window and drag it over the top of the application, that the frozen scroll bar disappears. Also, I can minimize the application, then maximize it again and the bar will disappear. There has to be a way to prevent this frozen scroll bar from happening in the first place though.
Per the answer, here was the fix to stop the bar from freezing up:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Clear();
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.None;
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.Vertical;
this.rtb_receive_0.Refresh();
Have you tried simply just programatically setting the Scrollbars property on the RTB?
myRichTextBox.ScrollBars = RichTextBoxScrollBars.None;
Edit: I think I misinterpreted what you needed. Searching around, I found this similar post on another forum: http://www.vbforums.com/showthread.php?793671-RESOLVED-RichTextBox-Visual-Bug
This user is setting the value of an RTB based on a selection in a list view. When a new value is set and does not require a scrollbar it doesn't re-draw and still shows the bar.
It seems like adding myRichTextBox.Clear(); myRichTextBox.Refresh(); should help. In this case that user is also programatically setting the ScrollBars property as well.
Also, are you able to determine how many lines of text can fit in the RichTextBox before a scrollbar is needed? I suppose that might vary based on system settings on the machine, but you might just be able to programatically check myrtb.Scrollbars = (myrtb.Lines.Length > X) ? Vertical : None; (excuse the psuedo code syntax)
What helped for me was just calling the refresh() method twice. Very ugly, but it does the job.
Hmm, after more thorough testing this ugly fix proved to be not so much of a fix afterall. It helps, but still some glitches.
refresh();
update();
seems like a better solution.
I was having this same problem. I solved it by calling the Invalidate() method which forces the control to repaint.
Me.RichTextBox.Clear()
'Call Invalidate in order to force the RichTextBox to repaint. I do this so that any
'Visible Scroll bars are removed after clearing the Text
Me.RichTextBox.Invalidate()
I tried with Refresh(); Update(); Invalidate();,but it didn't worked for me.
I solved this problem using following three lines :-
RitchTextBox.Clear(); //Clearing text in RichTextBox
RitchTextBox.ScrollBars = RichTextBoxScrollBars.None; //Remove scroll
RitchTextBox.ScrollBars = RichTextBoxScrollBars.Vertical; //Again add scroll
Try those above three lines. It will work 100%.
I have a combo box which I need to mirror in another tab page in a C# winforms based application.
I have perfectly working code for when you select a different item from the drop down list. Unfortunately, however, when I change the Text of a tab that has not been clicked on yet nothing actually happens.
If I first click each tab then everything works as expected.
Now I'm putting this down to some form of lack of initialisation happening first. So I've tried to select each tab in my constructor.
tabControlDataSource.SelectedIndex = 0;
tabControlDataSource.SelectedIndex = 1;
// etc
But this doesn't work.
I've also tried calling tabControlDataSource.SelectTab( 1 ) and still it doesn't work.
Does anyone know how I can force the tab to "initialise"?
Ok, typically I post the question after struggling for an hour and shortly afterwards find the solution.
TabPages are lazily initialised. So they don't fully initialise until they are made visible for the first time.
So i added this code to my constructor:
tabControlDataSource.TabPages[0].Show();
tabControlDataSource.TabPages[1].Show();
tabControlDataSource.TabPages[2].Show();
but this didn't work :(
It occurred to me, however, that the constructor might not be the best place. So I created an event handler for Shown as follows:
private void MainForm_Shown( object sender, EventArgs e )
{
tabControlDataSource.TabPages[0].Show();
tabControlDataSource.TabPages[1].Show();
tabControlDataSource.TabPages[2].Show();
}
And now everything is working!
Perhaps you could also use sort of a "lazy" synchronization (initialization) in this case. Quick robust ideas: polling timer to update content (which will update it once you see tab page), no dependses within second tab (no Changed events for combobox to update second tab content, use original combobox from first tab or rather have it's content underlying in accessable for both comboboxes class, etc), "reinitialization" when tab become visible (at which moment you also init your second combobox)...
Can't be a hour, no way =D
I would like to hide several textboxes, a label and a button as soon as a button is clicked... however, for some reason, my code doesn't seem to cause this effect. Nothing appears to happen. I'm using WPF.
Here is my code:
private void doSomething_Click(object sender, RoutedEventArgs e)
{
Name.Visibility = Visibility.Hidden;
}
This code doesn't seem to work.. any ideas?
I believe Visibility.Collapsed is what you need and not Visibility.Hidden.
EDIT: Did you try follow up this code with UpdateLayout() method of parent element/component?
Your code seems to work fine, the "Signing in..." label appears after everything else disappear. I suggest you to just copy all your code from the .xaml.cs file and the .xaml file into a new project, but make sure you don't copy the first line"<Window x:Class="..." because it could generate an error if the class name isn't the same in the new project.
For the xaml code I suggest you not think the same as you design windows forms applications. WPF has the layout system, which re-orientates or re-sizes its elements when re-sizing the window. So you should not specify exact numbers in the margin property as if they where coordinates. Create a grid, create rows or columns for each element and then just set the horizontal or vertical alignment or margins. Think different than the old windows forms way.
I've run your code... and it's working great for me. I've not changed anything (except the variable names) so I guess it's a bug from VS.
As said nikolamm94 try to add this.UpdateLayout(); at the end of connect_Click it might help. I tried and it is still working fine. Or maybe create a new VS projet, it already worked for me a few times.
Sorry my answer is not the most helpful, I wanted to put a comment instead but I don't have enough reputation :/
Please refer: https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx
Set to Visible: tb1.Visibility = System.Windows.Visibility.Visible;
Set to Hide: tb1.Visibility = System.Windows.Visibility.Hidden;
You can hide a textbox by going to properties->appearance->visibility, then setting it to "hidden"
I recently started getting acquainted with Visual Studio 2010 and C# for an internship. C# doesn't include a built-in InputBox function, so I made my own form, with a text box, two buttons and a simple label.
I have a function set up to allow the programmer to call the form in regular format (where the user enters input via the textbox) or yes/no format (where the form just displays a question and the yes and no buttons).
When I switch over to yes/no format, I want to center the label programmatically. I've been using the code:
labelNote.Left = inputBox.Left + (inputBox.Width / 2) - (labelNote.Width / 2);
This should put the center of the note in the center of the form. However, if the contents of the label change (making the new label longer or shorter) the properties don't update to reflect the new size. It won't center unless it includes the original text. Is there some way to force an update? I foresee this becoming a problem with positioning objects for scalability in the future.
Thank you for your time
I'm assuming you have the sizing within an actionlistener. Specifically the forms' resize action listener. Then whenever the form is resized it is called and all of you code is recalled. Then to force an update from somewhere else you just have to call the actionlistener.
Actionlistener:
Private Sub formName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Resize
Calls actionlistener:
formName_Resize(sender, e)
Well, you could attach an event to Label.TextChanged. Frankly it would be better to change the TextAlign or something like that though: try to perform the layout in a declarative fashion instead of doing it explicitly through code. That tends to make things work rather better.
I've found the [TableLayoutPanel]1 control to be reasonably easy to work with - most of the time (and occasionally a complete pain).
It turns out that I made a stupid mistake (a common theme for me in debugging. The really small stuff goes unnoticed for the longest amount of time).
The label resizing was not the issue. The issue was the order in which I changed the contents of the label and then called the function to calculate its new location. I was calling the location calculation first, so it found where to center the label based on the old contents. I didn't notice for so long, because the text was changing properly. I took it for granted that the functions were being called in the correct order.
So, when it doubt, check the order in which you're writing your code. Thanks for you help anyway, everyone. I ended up finding out some neat things that could be applicable to other scenarios (such as the MeasureString function in the Graphics class).
I have subclassed a Treeview and on instantiation it loads a new ImageList (and the associated Images).
Whenever I switch to the designer view, it's also trying to run this code, however the images aren't in the designer's path, so it crashes. I ended up putting in a hack to see if the current directory is "Visual Studio", then do nothing... but that's so ugly.
I find this happening for other things. If a control is trying to use objects during load/initalization that are only available while the program is running, then the Design View cannot bring up the control.
But is there a way to get around this?
I guess what I'm hoping for is having a try/catch for the Designer (only) with the ability to ignore a few errors I know will be happening (like FileNotFoundException, etc.).
Thanks
Everything that inherits from System.Windows.Forms.Control has a DesignMode property that returns a boolean indicating if you are in design mode or not. You could use this to determine when to/when not to load external resources.
Usually it is better to move the loading of these resources to an override of OnLoad as they are rarely required directly at construction. This fixes the issue you are seeing and means that only trees which get displayed at least once will perform these additional resource loading steps.
Otherwise, you can just exclude these steps during design time by checking the DesignMode property and acting accordingly.
This is a fine pattern to use if you're making a control library with a sample of images when shown in the designer or hook ins to other designer features but as a pattern for development I'm not sure it's very effective.
I would suggest shifting your "business logic" (in this case your loading of certain images into a treeview) outside of the bounds of your treeview control. In your case I would place the logic within the Load event of the form that the control is inside:
public void Load(object sender, EventArgs e)
{
string path = "c:\somePath\toAwesome\Images";
myFunkyTreeView.AddImages(path);
}
For larger apps I personally think you want to shift the logic even out of the forms themselves, but this is debatable measure as it requires additional plumbing as a trade-off for the flexibility this provides.
Thanks for pointing me in the right directioon guys.
I had tried registering to the OnLoad event, but that event is triggered when the Design View comes up, so that didn't quite work for me (am I doing something wrong?).
Anyway, I looked a bit more into the DesignMode property. It can only work for Controls, and sometimes your object may not even be a control.
So here's the answer I prefer:
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) {
// design-time stuff
} else {
// run-time stuff
}
Found it here.